Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Operators/3.3/UnidirectionalRingMigrator.cs @ 17181

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

#2875: Merged r17180 from trunk to stable

File size: 4.4 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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;
[13134]25using HeuristicLab.Data;
[3356]26using HeuristicLab.Operators;
[13134]27using HeuristicLab.Parameters;
[17097]28using HEAL.Attic;
[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.")]
[17097]35  [StorableType("A6642E0A-2FA1-49F3-9BA8-94EA370FE2CF")]
[3356]36  public class UnidirectionalRingMigrator : SingleSuccessorOperator, IMigrator {
[13134]37    public IValueLookupParameter<BoolValue> ClockwiseMigrationParameter {
38      get { return (IValueLookupParameter<BoolValue>)Parameters["ClockwiseMigration"]; }
39    }
40
[4722]41    [StorableConstructor]
[17097]42    protected UnidirectionalRingMigrator(StorableConstructorFlag _) : base(_) { }
[4722]43    protected UnidirectionalRingMigrator(UnidirectionalRingMigrator original, Cloner cloner) : base(original, cloner) { }
44
[13134]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    }
[4722]59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new UnidirectionalRingMigrator(this, cloner);
61    }
62
[881]63    /// <summary>
[13134]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>
[3356]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
[881]74    /// </pre>
75    /// </summary>
[3356]76    /// <returns>The next operation.</returns>
77    public override IOperation Apply() {
[13134]78      bool clockwise = ClockwiseMigrationParameter.ActualValue.Value;
[3356]79      IScope scope = ExecutionContext.Scope;
80      List<IScope> emigrantsList = new List<IScope>();
[2]81
82      for (int i = 0; i < scope.SubScopes.Count; i++) {
83        IScope emigrants = scope.SubScopes[i].SubScopes[1];
[3356]84        scope.SubScopes[i].SubScopes.Remove(emigrants);
[2]85        emigrantsList.Add(emigrants);
86      }
87
[13134]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      }
[2]97
98      for (int i = 0; i < scope.SubScopes.Count; i++)
[3356]99        scope.SubScopes[i].SubScopes.Add(emigrantsList[i]);
[2]100
[3356]101      return base.Apply();
[2]102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.