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
RevLine 
[2]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
22using System.Collections.Generic;
[4722]23using HeuristicLab.Common;
[2]24using HeuristicLab.Core;
[13078]25using HeuristicLab.Data;
[3356]26using HeuristicLab.Operators;
[13078]27using HeuristicLab.Parameters;
[3356]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]29
[3356]30namespace HeuristicLab.Optimization.Operators {
[881]31  /// <summary>
[3356]32  /// Operator that migrates the selected sub scopes in each subscope in an unidirectional ring.
[881]33  /// </summary>
[3356]34  [Item("UnidirectionalRingMigrator", "Migrates the selected sub scopes in each subscope in an unidirectional ring.")]
35  [StorableClass]
36  public class UnidirectionalRingMigrator : SingleSuccessorOperator, IMigrator {
[13078]37    public IValueLookupParameter<BoolValue> ClockwiseMigrationDirectionParameter {
38      get { return (IValueLookupParameter<BoolValue>)Parameters["ClockwiseMigrationDirection"]; }
39    }
40
[13094]41    public BoolValue ClockwiseMigrationDirection
42    {
43      get { return ClockwiseMigrationDirectionParameter.Value; }
44      set { ClockwiseMigrationDirectionParameter.Value = value; }
45    }
46
[4722]47    [StorableConstructor]
48    protected UnidirectionalRingMigrator(bool deserializing) : base(deserializing) { }
49    protected UnidirectionalRingMigrator(UnidirectionalRingMigrator original, Cloner cloner) : base(original, cloner) { }
50
[13078]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    }
[4722]65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new UnidirectionalRingMigrator(this, cloner);
67    }
68
[881]69    /// <summary>
70    /// Migrates every first sub scope of each child to its left neighbour (like a ring).
71    /// <pre>                                                               
[3356]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
[881]79    /// </pre>
80    /// </summary>
[3356]81    /// <returns>The next operation.</returns>
82    public override IOperation Apply() {
[13078]83      bool clockwise = ClockwiseMigrationDirectionParameter.ActualValue.Value;
[3356]84      IScope scope = ExecutionContext.Scope;
85      List<IScope> emigrantsList = new List<IScope>();
[2]86
87      for (int i = 0; i < scope.SubScopes.Count; i++) {
88        IScope emigrants = scope.SubScopes[i].SubScopes[1];
[3356]89        scope.SubScopes[i].SubScopes.Remove(emigrants);
[2]90        emigrantsList.Add(emigrants);
91      }
92
[13078]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      }
[2]102
103      for (int i = 0; i < scope.SubScopes.Count; i++)
[3356]104        scope.SubScopes[i].SubScopes.Add(emigrantsList[i]);
[2]105
[3356]106      return base.Apply();
[2]107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.