Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsMover.cs @ 12162

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

#2350 Fixed SteadyState ALPS Mover and MainLoop.

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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Algorithms.ALPS.SteadyState {
33  [Item("AlpsSsMover", "")]
34  [StorableClass]
35  public class AlpsSsMover : SingleSuccessorOperator, IStochasticOperator {
36    private ILookupParameter<IntValue> LayerParameter {
37      get { return (ILookupParameter<IntValue>)Parameters["Layer"]; }
38    }
39    private ILookupParameter<IntValue> TargetIndexParameter {
40      get { return (ILookupParameter<IntValue>)Parameters["TargetIndex"]; }
41    }
42    private ILookupParameter<IntValue> PopulationSizeParameter {
43      get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
44    }
45    private ILookupParameter<IntValue> NumberOfLayersParameter {
46      get { return (ILookupParameter<IntValue>)Parameters["NumberOfLayers"]; }
47    }
48    private ILookupParameter<IScope> WorkingScopeParameter {
49      get { return (ILookupParameter<IScope>)Parameters["WorkingScope"]; }
50    }
51    private ILookupParameter<IScope> LayersParameter {
52      get { return (ILookupParameter<IScope>)Parameters["Layers"]; }
53    }
54    public ILookupParameter<IRandom> RandomParameter {
55      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
56    }
57
58    [StorableConstructor]
59    private AlpsSsMover(bool deserializing)
60      : base(deserializing) { }
61    private AlpsSsMover(AlpsSsMover original, Cloner cloner)
62      : base(original, cloner) { }
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new AlpsSsMover(this, cloner);
65    }
66
67    public AlpsSsMover()
68      : base() {
69      Parameters.Add(new LookupParameter<IntValue>("Layer"));
70      Parameters.Add(new LookupParameter<IntValue>("TargetIndex"));
71      Parameters.Add(new LookupParameter<IntValue>("PopulationSize"));
72      Parameters.Add(new LookupParameter<IntValue>("NumberOfLayers"));
73      Parameters.Add(new LookupParameter<IScope>("WorkingScope"));
74      Parameters.Add(new LookupParameter<IScope>("Layers"));
75      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
76    }
77
78    private int n;
79    private int m;
80    private IScope layers;
81    private IRandom rand;
82    public override IOperation Apply() {
83      int i = LayerParameter.ActualValue.Value;
84      int j = TargetIndexParameter.ActualValue.Value;
85      n = NumberOfLayersParameter.ActualValue.Value;
86      m = PopulationSizeParameter.ActualValue.Value;
87      rand = RandomParameter.ActualValue;
88
89      layers = LayersParameter.ActualValue;
90      var newIndividual = (IScope)WorkingScopeParameter.ActualValue.Clone();
91      newIndividual.Name = j.ToString();
92
93      TryMoveUp(i, j);
94      var currentLayer = layers.SubScopes[i];
95      currentLayer.SubScopes[j] = newIndividual;
96
97      return base.Apply();
98    }
99    private void TryMoveUp(int i, int j) {
100      var currentLayer = layers.SubScopes[i];
101      var currentIndividual = currentLayer.SubScopes[j];
102      if (i < n - 1) {
103        var nextLayer = layers.SubScopes[i + 1];
104        int? k = FindReplaceable(nextLayer, currentIndividual);
105
106        if (k.HasValue) {
107          TryMoveUp(i + 1, k.Value);
108          nextLayer.SubScopes[k.Value] = currentIndividual;
109        }
110      }
111    }
112    private int? FindReplaceable(IScope layer, IScope individual) {
113      return Enumerable.Range(0, layer.SubScopes.Count).SampleRandom(rand);
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.