Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12224 was 12224, checked in by pfleck, 8 years ago

#2350 Fixed a bug when selecting an individual for replacement during move-up recursion.

File size: 7.8 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    #region Parameter Properties
37    private ILookupParameter<IntValue> LayerParameter {
38      get { return (ILookupParameter<IntValue>)Parameters["Layer"]; }
39    }
40    private ILookupParameter<IntValue> TargetIndexParameter {
41      get { return (ILookupParameter<IntValue>)Parameters["TargetIndex"]; }
42    }
43    private ILookupParameter<IntValue> LayerSizeParameter {
44      get { return (ILookupParameter<IntValue>)Parameters["LayerSize"]; }
45    }
46    private ILookupParameter<IntValue> NumberOfLayersParameter {
47      get { return (ILookupParameter<IntValue>)Parameters["NumberOfLayers"]; }
48    }
49    private ILookupParameter<IScope> WorkingScopeParameter {
50      get { return (ILookupParameter<IScope>)Parameters["WorkingScope"]; }
51    }
52    private ILookupParameter<IScope> LayersScopeParameter {
53      get { return (ILookupParameter<IScope>)Parameters["LayersScope"]; }
54    }
55    public ILookupParameter<IRandom> RandomParameter {
56      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
57    }
58    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
59      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
60    }
61    public ValueLookupParameter<BoolValue> MaximizationParameter {
62      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
63    }
64    public IScopeTreeLookupParameter<IntValue> EvalsCreatedParameter {
65      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["EvalsCreated"]; }
66    }
67    public IScopeTreeLookupParameter<IntValue> EvalsMovedQualityParameter {
68      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["EvalsMoved"]; }
69    }
70    private ILookupParameter<IntValue> EvaluatedSolutionsParameter {
71      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
72    }
73    private ILookupParameter<IntArray> AgeLimitsParameter {
74      get { return (ILookupParameter<IntArray>)Parameters["AgeLimits"]; }
75    }
76    private ILookupParameter<IntValue> ElitesParameter {
77      get { return (ILookupParameter<IntValue>)Parameters["Elites"]; }
78    }
79    #endregion
80
81    [StorableConstructor]
82    private AlpsSsMover(bool deserializing)
83      : base(deserializing) { }
84    private AlpsSsMover(AlpsSsMover original, Cloner cloner)
85      : base(original, cloner) { }
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new AlpsSsMover(this, cloner);
88    }
89
90    public AlpsSsMover()
91      : base() {
92      Parameters.Add(new LookupParameter<IntValue>("Layer"));
93      Parameters.Add(new LookupParameter<IntValue>("TargetIndex"));
94      Parameters.Add(new LookupParameter<IntValue>("LayerSize"));
95      Parameters.Add(new LookupParameter<IntValue>("NumberOfLayers"));
96      Parameters.Add(new LookupParameter<IScope>("WorkingScope"));
97      Parameters.Add(new LookupParameter<IScope>("LayersScope"));
98      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
99      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality"));
100      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
101      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("EvalsCreated"));
102      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("EvalsMoved"));
103      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
104      Parameters.Add(new LookupParameter<IntArray>("AgeLimits"));
105      Parameters.Add(new LookupParameter<IntValue>("Elites"));
106    }
107
108    private int n;
109    private int m;
110    private int evals;
111    private int popSize;
112    private IScope layers;
113    private IRandom rand;
114    private string qualityVariableName;
115    private IntArray ageLimits;
116    private bool maximization;
117    private int elites;
118    public override IOperation Apply() {
119      int i = LayerParameter.ActualValue.Value;
120      int j = TargetIndexParameter.ActualValue.Value;
121      n = NumberOfLayersParameter.ActualValue.Value;
122      m = LayerSizeParameter.ActualValue.Value;
123      evals = EvaluatedSolutionsParameter.ActualValue.Value;
124      popSize = n * m;
125      rand = RandomParameter.ActualValue;
126      qualityVariableName = QualityParameter.TranslatedName;
127      ageLimits = AgeLimitsParameter.ActualValue;
128      maximization = MaximizationParameter.ActualValue.Value;
129      elites = ElitesParameter.ActualValue.Value;
130
131      layers = LayersScopeParameter.ActualValue;
132      var newIndividual = (IScope)WorkingScopeParameter.ActualValue.Clone();
133      newIndividual.Name = j.ToString();
134
135      TryMoveUp(i, j);
136      var currentLayer = layers.SubScopes[i];
137      currentLayer.SubScopes[j] = newIndividual;
138
139      return base.Apply();
140    }
141    private void TryMoveUp(int i, int j) {
142      var currentLayer = layers.SubScopes[i];
143      var currentIndividual = currentLayer.SubScopes[j];
144      if (i < n - 1) {
145        var nextLayer = layers.SubScopes[i + 1];
146        int? k = FindReplaceable(nextLayer, currentIndividual);
147
148        if (k.HasValue) {
149          TryMoveUp(i + 1, k.Value);
150          nextLayer.SubScopes[k.Value] = currentIndividual;
151        }
152      }
153    }
154    private int? FindReplaceable(IScope layer, IScope individualToReplace) {
155      int layerNumber = ((IntValue)layer.Variables["Layer"].Value).Value;
156
157      var individuals = (
158        from individual in layer.SubScopes
159        let quality = ((DoubleValue)individual.Variables[qualityVariableName].Value).Value
160        let evalsCreated = ((IntValue)individual.Variables["EvalsCreated"].Value).Value
161        let age = (evals - evalsCreated) / popSize
162        select new { individual, quality, age }
163        ).Select((x, index) => new { index, x.individual, x.quality, x.age })
164        .ToList();
165
166      var ageLimit = layerNumber < n - 1 ? ageLimits[layerNumber] : int.MaxValue;
167
168      var toOldIndividual = individuals.FirstOrDefault(x => x.age >= ageLimit);
169      if (toOldIndividual != null)
170        return toOldIndividual.index;
171
172      // Take last, because it is the worst quality (sorted already before choosing TargetIndex)
173      var worstIndividual = individuals.LastOrDefault();
174      double replaceQuality = ((DoubleValue)individualToReplace.Variables[qualityVariableName].Value).Value;
175      if (worstIndividual != null && (maximization ? worstIndividual.quality < replaceQuality : worstIndividual.quality > replaceQuality))
176        return worstIndividual.index;
177
178      return rand.Next(elites, layer.SubScopes.Count);
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.