Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17398 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
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  [StorableType("A6642E0A-2FA1-49F3-9BA8-94EA370FE2CF")]
36  public class UnidirectionalRingMigrator : SingleSuccessorOperator, IMigrator {
37    public IValueLookupParameter<BoolValue> ClockwiseMigrationParameter {
38      get { return (IValueLookupParameter<BoolValue>)Parameters["ClockwiseMigration"]; }
39    }
40
41    [StorableConstructor]
42    protected UnidirectionalRingMigrator(StorableConstructorFlag _) : base(_) { }
43    protected UnidirectionalRingMigrator(UnidirectionalRingMigrator original, Cloner cloner) : base(original, cloner) { }
44
45    public UnidirectionalRingMigrator()
46      : base() {
47      Parameters.Add(new ValueLookupParameter<BoolValue>("ClockwiseMigration", "True to migrate individuals clockwise, false to migrate individuals counterclockwise.", new BoolValue(true)));
48    }
49
50    [StorableHook(HookType.AfterDeserialization)]
51    private void AfterDeserialization() {
52      // BackwardsCompatibility3.3
53      #region Backwards compatible code (remove with 3.4)
54      if (!Parameters.ContainsKey("ClockwiseMigration")) {
55        Parameters.Add(new ValueLookupParameter<BoolValue>("ClockwiseMigration", "True to migrate individuals clockwise, false to migrate individuals counterclockwise.", new BoolValue(false)));
56      }
57      #endregion
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new UnidirectionalRingMigrator(this, cloner);
61    }
62
63    /// <summary>
64    /// Migrates every first sub scope of each child to its right or left neighbour (like a ring).
65    /// If clockwise migration (default) is used the selected scopes A D G becomes G A D, contrary to counterclockwise where A D G becomes D G A.
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 = ClockwiseMigrationParameter.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.