source: trunk/sources/HeuristicLab.Optimization.Operators/3.3/UnidirectionalRingMigrator.cs @ 13078

Last change on this file since 13078 was 13078, checked in by pfleck, 7 years ago

#2495

  • Added ClockwiseMigrationDirection Parameter.
  • Added backwards compatability hook that use the old counterclockwise migration.
File size: 4.4 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Optimization.Operators {
31  /// <summary>
32  /// Operator that migrates the selected sub scopes in each subscope in an unidirectional ring.
33  /// </summary>
34  [Item("UnidirectionalRingMigrator", "Migrates the selected sub scopes in each subscope in an unidirectional ring.")]
35  [StorableClass]
36  public class UnidirectionalRingMigrator : SingleSuccessorOperator, IMigrator {
37    public IValueLookupParameter<BoolValue> ClockwiseMigrationDirectionParameter {
38      get { return (IValueLookupParameter<BoolValue>)Parameters["ClockwiseMigrationDirection"]; }
39    }
40
41    [StorableConstructor]
42    protected UnidirectionalRingMigrator(bool deserializing) : base(deserializing) { }
43    protected UnidirectionalRingMigrator(UnidirectionalRingMigrator original, Cloner cloner) : base(original, cloner) { }
44
45    public UnidirectionalRingMigrator()
46      : base() {
47      Parameters.Add(new ValueLookupParameter<BoolValue>("ClockwiseMigrationDirection", "True to migrate individuals clockwise, otherwise migrate individuals counterclockwise.", new BoolValue(true)));
48    }
49
50
51    [StorableHook(HookType.AfterDeserialization)]
52    private void AfterDeserialization() {
53      // BackwardsCompatibility3.3
54      #region Backwards compatible code (remove with 3.4)
55      if (!Parameters.ContainsKey("ClockwiseMigrationDirection")) {
56        Parameters.Add(new ValueLookupParameter<BoolValue>("ClockwiseMigrationDirection", "True to migrate individuals clockwise, otherwise migrate individuals counterclockwise.", new BoolValue(false)));
57      }
58      #endregion
59    }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new UnidirectionalRingMigrator(this, cloner);
62    }
63
64    /// <summary>
65    /// Migrates every first sub scope of each child to its left neighbour (like a ring).
66    /// <pre>                                                               
67    ///          __ scope __              __ scope __
68    ///         /     |     \            /     |     \
69    ///       Pop1   Pop2   Pop3  =>   Pop1   Pop2   Pop3
70    ///       / \    / \    / \        / \    / \    / \
71    ///      R   S  R   S  R   S      R   S  R   S  R   S
72    ///     /|\  | /|\  | /|\  |     /|\  | /|\  | /|\  |
73    ///     ABC  A DEF  D GHI  G     ABC  G DEF  A GHI  D
74    /// </pre>
75    /// </summary>
76    /// <returns>The next operation.</returns>
77    public override IOperation Apply() {
78      bool clockwise = ClockwiseMigrationDirectionParameter.ActualValue.Value;
79      IScope scope = ExecutionContext.Scope;
80      List<IScope> emigrantsList = new List<IScope>();
81
82      for (int i = 0; i < scope.SubScopes.Count; i++) {
83        IScope emigrants = scope.SubScopes[i].SubScopes[1];
84        scope.SubScopes[i].SubScopes.Remove(emigrants);
85        emigrantsList.Add(emigrants);
86      }
87
88      if (clockwise) {
89        // shift last emigrants to start of list
90        emigrantsList.Insert(0, emigrantsList[emigrantsList.Count - 1]);
91        emigrantsList.RemoveAt(emigrantsList.Count - 1);
92      } else {
93        // shift first emigrants to end of list
94        emigrantsList.Add(emigrantsList[0]);
95        emigrantsList.RemoveAt(0);
96      }
97
98      for (int i = 0; i < scope.SubScopes.Count; i++)
99        scope.SubScopes[i].SubScopes.Add(emigrantsList[i]);
100
101      return base.Apply();
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.