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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Random;
|
---|
31 |
|
---|
32 | namespace 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 | ((IntValue)currentIndividual.Variables["LastMove"].Value).Value = evals;
|
---|
145 |
|
---|
146 | if (i < n - 1) {
|
---|
147 | var nextLayer = layers.SubScopes[i + 1];
|
---|
148 | int? k = FindReplaceable(nextLayer, currentIndividual);
|
---|
149 |
|
---|
150 | if (k.HasValue) {
|
---|
151 | TryMoveUp(i + 1, k.Value);
|
---|
152 | nextLayer.SubScopes[k.Value] = currentIndividual;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | private int? FindReplaceable(IScope layer, IScope replacingCandidate) {
|
---|
157 | int layerNumber = ((IntValue)layer.Variables["Layer"].Value).Value;
|
---|
158 |
|
---|
159 | var individuals = (
|
---|
160 | from individual in layer.SubScopes
|
---|
161 | let quality = ((DoubleValue)individual.Variables[qualityVariableName].Value).Value
|
---|
162 | let evalsCreated = ((IntValue)individual.Variables["EvalsCreated"].Value).Value
|
---|
163 | let lastMove = ((IntValue)individual.Variables["LastMove"].Value).Value
|
---|
164 | let age = (evals - evalsCreated) / popSize
|
---|
165 | select new { individual, quality, age, lastMove }
|
---|
166 | ).Select((x, index) => new { index, x.individual, x.quality, x.age, x.lastMove })
|
---|
167 | .ToList();
|
---|
168 |
|
---|
169 | var ageLimit = layerNumber < n - 1 ? ageLimits[layerNumber] : int.MaxValue;
|
---|
170 |
|
---|
171 | // Individuals which are too old are first priority to be replaced
|
---|
172 | var toOldIndividual = individuals.FirstOrDefault(x => x.age >= ageLimit);
|
---|
173 | if (toOldIndividual != null)
|
---|
174 | return toOldIndividual.index;
|
---|
175 |
|
---|
176 | double replacingCandidateQuality = ((DoubleValue)replacingCandidate.Variables[qualityVariableName].Value).Value;
|
---|
177 | var worseIndividuals = individuals.Where(individual =>
|
---|
178 | maximization
|
---|
179 | ? individual.quality < replacingCandidateQuality
|
---|
180 | : individual.quality > replacingCandidateQuality)
|
---|
181 | .ToList();
|
---|
182 | // Then take the worst individual where the last move happed m * n evaluations ago
|
---|
183 | int lastMoveLimit = evals - m * n;
|
---|
184 | var worstIndividual = worseIndividuals.LastOrDefault(individual => individual.lastMove < lastMoveLimit);
|
---|
185 | if (worstIndividual != null)
|
---|
186 | return worstIndividual.index;
|
---|
187 | // If no individual moved n * m evaluations ago, take the worst
|
---|
188 | worstIndividual = worseIndividuals.LastOrDefault();
|
---|
189 | if (worstIndividual != null)
|
---|
190 | return worstIndividual.index;
|
---|
191 |
|
---|
192 | // No individual found for replacement
|
---|
193 | return null;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | } |
---|