Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2350 Fixed Bugs in Steady-State ALPS OperatorGraph and Mover.

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