[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
| 22 | using System;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[3404] | 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Operators {
|
---|
[788] | 30 | /// <summary>
|
---|
| 31 | /// Mixes the sub scopes of a specified scope according to a specified number of partitions.
|
---|
| 32 | /// </summary>
|
---|
[3404] | 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"]; }
|
---|
[2] | 38 | }
|
---|
[3404] | 39 |
|
---|
| 40 | public IntValue Partitions {
|
---|
| 41 | get { return PartitionsParameter.Value; }
|
---|
| 42 | set { PartitionsParameter.Value = value; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[4722] | 45 | [StorableConstructor]
|
---|
| 46 | protected SubScopesMixer(bool deserializing) : base(deserializing) { }
|
---|
| 47 | protected SubScopesMixer(SubScopesMixer original, Cloner cloner)
|
---|
| 48 | : base(original, cloner) {
|
---|
| 49 | }
|
---|
[788] | 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>
|
---|
[2] | 54 | public SubScopesMixer()
|
---|
| 55 | : base() {
|
---|
[3404] | 56 | Parameters.Add(new ValueParameter<IntValue>("Partitions", "The number of equal-sized partitions.", new IntValue(2)));
|
---|
[2] | 57 | }
|
---|
| 58 |
|
---|
[4722] | 59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 60 | return new SubScopesMixer(this, cloner);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[788] | 63 | /// <summary>
|
---|
[3404] | 64 | /// Mixes the sub-scopes of the scope this operator is applied on.
|
---|
[788] | 65 | /// </summary>
|
---|
[3404] | 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/>
|
---|
[788] | 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>
|
---|
[801] | 75 | /// <exception cref="ArgumentException">Thrown when the number of sub scopes cannot be divided by
|
---|
| 76 | /// the number of partitions without remainder.</exception>
|
---|
[788] | 77 | /// <param name="scope">The scope whose sub scopes should be mixed.</param>
|
---|
| 78 | /// <returns><c>null</c>.</returns>
|
---|
[3404] | 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;
|
---|
[2] | 86 |
|
---|
[3404] | 87 | IScope[] reorderedSubScopes = new IScope[count];
|
---|
| 88 |
|
---|
[2] | 89 | // mix sub-scopes -> alternately take one sub-scope from each partition
|
---|
| 90 | for (int i = 0; i < partitionSize; i++) {
|
---|
[3404] | 91 | for (int j = 0; j < partitions; j++) {
|
---|
| 92 | reorderedSubScopes[i * partitions + j] = scope.SubScopes[j * partitionSize + i];
|
---|
| 93 | }
|
---|
[2] | 94 | }
|
---|
[3404] | 95 | scope.SubScopes.Clear();
|
---|
| 96 | scope.SubScopes.AddRange(reorderedSubScopes);
|
---|
[2] | 97 |
|
---|
[3404] | 98 | return base.Apply();
|
---|
[2] | 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|