Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/SubScopesMixer.cs @ 14125

Last change on this file since 14125 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Operators {
30  /// <summary>
31  /// Mixes the sub scopes of a specified scope according to a specified number of partitions.
32  /// </summary>
33  [Item("SubScopesMixer", "Changes the order of the sub-scopes by repartitioning the sub-scopes such that each new partition contains one scope from each old partition.")]
34  [StorableClass]
35  public class SubScopesMixer : SingleSuccessorOperator {
36    public ValueParameter<IntValue> PartitionsParameter {
37      get { return (ValueParameter<IntValue>)Parameters["Partitions"]; }
38    }
39
40    public IntValue Partitions {
41      get { return PartitionsParameter.Value; }
42      set { PartitionsParameter.Value = value; }
43    }
44
45    [StorableConstructor]
46    protected SubScopesMixer(bool deserializing) : base(deserializing) { }
47    protected SubScopesMixer(SubScopesMixer original, Cloner cloner)
48      : base(original, cloner) {
49    }
50    /// <summary>
51    /// Initializes a new instance of <see cref="SubScopesMixer"/> with one variable infos
52    /// (<c>Partitions</c>) and the <c>Local</c> flag set to <c>true</c>.
53    /// </summary>
54    public SubScopesMixer()
55      : base() {
56      Parameters.Add(new ValueParameter<IntValue>("Partitions", "The number of equal-sized partitions.", new IntValue(2)));
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new SubScopesMixer(this, cloner);
61    }
62
63    /// <summary>
64    /// Mixes the sub-scopes of the scope this operator is applied on.
65    /// </summary>
66    /// <remarks>Mixing of sub-scopes is based on the number of partitions.
67    /// <example>12 sub-scopes and 3 partitions:<br/>
68    /// Partition 1 contains scopes 1-4, partition 2 scopes 5-8 and partition 3 scopes 9-12. <br/>
69    /// Mixing is realized by selecting at the beginning the first scope from partition one, then the
70    /// first scope from partition 2, afterwards first scope from partition 3,
71    /// then the second scope from the first partition and so on. <br/>
72    /// In the end the new sorting of the sub scopes is 1-5-9-2-6-10-3-7-11-4-8-12.
73    /// </example>
74    /// </remarks>
75    /// <exception cref="ArgumentException">Thrown when the number of sub scopes cannot be divided by
76    /// the number of partitions without remainder.</exception>
77    /// <param name="scope">The scope whose sub scopes should be mixed.</param>
78    /// <returns><c>null</c>.</returns>
79    public override IOperation Apply() {
80      int partitions = Partitions.Value;
81      IScope scope = ExecutionContext.Scope;
82      int count = scope.SubScopes.Count;
83      if ((count % partitions) != 0)
84        throw new ArgumentException(Name + ": The number of sub-scopes is not divisible by the number of partitions without remainder.");
85      int partitionSize = count / partitions;
86
87      IScope[] reorderedSubScopes = new IScope[count];
88
89      // mix sub-scopes -> alternately take one sub-scope from each partition
90      for (int i = 0; i < partitionSize; i++) {
91        for (int j = 0; j < partitions; j++) {
92          reorderedSubScopes[i * partitions + j] = scope.SubScopes[j * partitionSize + i];
93        }
94      }
95      scope.SubScopes.Clear();
96      scope.SubScopes.AddRange(reorderedSubScopes);
97
98      return base.Apply();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.