Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3659 was 3404, checked in by abeham, 14 years ago

Added gender specific selection and ported the necessary operators RightChildReducer and SubScopesMixer #976

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Operators {
29  /// <summary>
30  /// Mixes the sub scopes of a specified scope according to a specified number of partitions.
31  /// </summary>
32  [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.")]
33  [StorableClass]
34  public class SubScopesMixer : SingleSuccessorOperator {
35    public ValueParameter<IntValue> PartitionsParameter {
36      get { return (ValueParameter<IntValue>)Parameters["Partitions"]; }
37    }
38
39    public IntValue Partitions {
40      get { return PartitionsParameter.Value; }
41      set { PartitionsParameter.Value = value; }
42    }
43
44    /// <summary>
45    /// Initializes a new instance of <see cref="SubScopesMixer"/> with one variable infos
46    /// (<c>Partitions</c>) and the <c>Local</c> flag set to <c>true</c>.
47    /// </summary>
48    public SubScopesMixer()
49      : base() {
50      Parameters.Add(new ValueParameter<IntValue>("Partitions", "The number of equal-sized partitions.", new IntValue(2)));
51    }
52
53    /// <summary>
54    /// Mixes the sub-scopes of the scope this operator is applied on.
55    /// </summary>
56    /// <remarks>Mixing of sub-scopes is based on the number of partitions.
57    /// <example>12 sub-scopes and 3 partitions:<br/>
58    /// Partition 1 contains scopes 1-4, partition 2 scopes 5-8 and partition 3 scopes 9-12. <br/>
59    /// Mixing is realized by selecting at the beginning the first scope from partition one, then the
60    /// first scope from partition 2, afterwards first scope from partition 3,
61    /// then the second scope from the first partition and so on. <br/>
62    /// In the end the new sorting of the sub scopes is 1-5-9-2-6-10-3-7-11-4-8-12.
63    /// </example>
64    /// </remarks>
65    /// <exception cref="ArgumentException">Thrown when the number of sub scopes cannot be divided by
66    /// the number of partitions without remainder.</exception>
67    /// <param name="scope">The scope whose sub scopes should be mixed.</param>
68    /// <returns><c>null</c>.</returns>
69    public override IOperation Apply() {
70      int partitions = Partitions.Value;
71      IScope scope = ExecutionContext.Scope;
72      int count = scope.SubScopes.Count;
73      if ((count % partitions) != 0)
74        throw new ArgumentException(Name + ": The number of sub-scopes is not divisible by the number of partitions without remainder.");
75      int partitionSize = count / partitions;
76
77      IScope[] reorderedSubScopes = new IScope[count];
78
79      // mix sub-scopes -> alternately take one sub-scope from each partition
80      for (int i = 0; i < partitionSize; i++) {
81        for (int j = 0; j < partitions; j++) {
82          reorderedSubScopes[i * partitions + j] = scope.SubScopes[j * partitionSize + i];
83        }
84      }
85      scope.SubScopes.Clear();
86      scope.SubScopes.AddRange(reorderedSubScopes);
87
88      return base.Apply();
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.