Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13094 was 13094, checked in by pfleck, 9 years ago

#2495
Added backwards compatibility for IslandGeneticAlgorithm and IslandOffspringSelectionGeneticAlgorithm to still use counterclockwise migration.
The UnidirectionalRingMigrator uses clockwise per default.

File size: 4.6 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    public BoolValue ClockwiseMigrationDirection
42    {
43      get { return ClockwiseMigrationDirectionParameter.Value; }
44      set { ClockwiseMigrationDirectionParameter.Value = value; }
45    }
46
47    [StorableConstructor]
48    protected UnidirectionalRingMigrator(bool deserializing) : base(deserializing) { }
49    protected UnidirectionalRingMigrator(UnidirectionalRingMigrator original, Cloner cloner) : base(original, cloner) { }
50
51    public UnidirectionalRingMigrator()
52      : base() {
53      Parameters.Add(new ValueLookupParameter<BoolValue>("ClockwiseMigrationDirection", "True to migrate individuals clockwise, otherwise migrate individuals counterclockwise.", new BoolValue(true)));
54    }
55
56    [StorableHook(HookType.AfterDeserialization)]
57    private void AfterDeserialization() {
58      // BackwardsCompatibility3.3
59      #region Backwards compatible code (remove with 3.4)
60      if (!Parameters.ContainsKey("ClockwiseMigrationDirection")) {
61        Parameters.Add(new ValueLookupParameter<BoolValue>("ClockwiseMigrationDirection", "True to migrate individuals clockwise, otherwise migrate individuals counterclockwise.", new BoolValue(false)));
62      }
63      #endregion
64    }
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new UnidirectionalRingMigrator(this, cloner);
67    }
68
69    /// <summary>
70    /// Migrates every first sub scope of each child to its left neighbour (like a ring).
71    /// <pre>                                                               
72    ///          __ scope __              __ scope __
73    ///         /     |     \            /     |     \
74    ///       Pop1   Pop2   Pop3  =>   Pop1   Pop2   Pop3
75    ///       / \    / \    / \        / \    / \    / \
76    ///      R   S  R   S  R   S      R   S  R   S  R   S
77    ///     /|\  | /|\  | /|\  |     /|\  | /|\  | /|\  |
78    ///     ABC  A DEF  D GHI  G     ABC  G DEF  A GHI  D
79    /// </pre>
80    /// </summary>
81    /// <returns>The next operation.</returns>
82    public override IOperation Apply() {
83      bool clockwise = ClockwiseMigrationDirectionParameter.ActualValue.Value;
84      IScope scope = ExecutionContext.Scope;
85      List<IScope> emigrantsList = new List<IScope>();
86
87      for (int i = 0; i < scope.SubScopes.Count; i++) {
88        IScope emigrants = scope.SubScopes[i].SubScopes[1];
89        scope.SubScopes[i].SubScopes.Remove(emigrants);
90        emigrantsList.Add(emigrants);
91      }
92
93      if (clockwise) {
94        // shift last emigrants to start of list
95        emigrantsList.Insert(0, emigrantsList[emigrantsList.Count - 1]);
96        emigrantsList.RemoveAt(emigrantsList.Count - 1);
97      } else {
98        // shift first emigrants to end of list
99        emigrantsList.Add(emigrantsList[0]);
100        emigrantsList.RemoveAt(0);
101      }
102
103      for (int i = 0; i < scope.SubScopes.Count; i++)
104        scope.SubScopes[i].SubScopes.Add(emigrantsList[i]);
105
106      return base.Apply();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.