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 | #endregion
|
---|
77 |
|
---|
78 | [StorableConstructor]
|
---|
79 | private AlpsSsMover(bool deserializing)
|
---|
80 | : base(deserializing) { }
|
---|
81 | private AlpsSsMover(AlpsSsMover original, Cloner cloner)
|
---|
82 | : base(original, cloner) { }
|
---|
83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
84 | return new AlpsSsMover(this, cloner);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public AlpsSsMover()
|
---|
88 | : base() {
|
---|
89 | Parameters.Add(new LookupParameter<IntValue>("Layer"));
|
---|
90 | Parameters.Add(new LookupParameter<IntValue>("TargetIndex"));
|
---|
91 | Parameters.Add(new LookupParameter<IntValue>("LayerSize"));
|
---|
92 | Parameters.Add(new LookupParameter<IntValue>("NumberOfLayers"));
|
---|
93 | Parameters.Add(new LookupParameter<IScope>("WorkingScope"));
|
---|
94 | Parameters.Add(new LookupParameter<IScope>("LayersScope"));
|
---|
95 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
96 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality"));
|
---|
97 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
98 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("EvalsCreated"));
|
---|
99 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("EvalsMoved"));
|
---|
100 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
|
---|
101 | Parameters.Add(new LookupParameter<IntArray>("AgeLimits"));
|
---|
102 | }
|
---|
103 |
|
---|
104 | private int n;
|
---|
105 | private int m;
|
---|
106 | private int evals;
|
---|
107 | private int popSize;
|
---|
108 | private IScope layers;
|
---|
109 | private IRandom rand;
|
---|
110 | private string qualityVariableName;
|
---|
111 | private IntArray ageLimits;
|
---|
112 | private bool maximization;
|
---|
113 | public override IOperation Apply() {
|
---|
114 | int i = LayerParameter.ActualValue.Value;
|
---|
115 | int j = TargetIndexParameter.ActualValue.Value;
|
---|
116 | n = NumberOfLayersParameter.ActualValue.Value;
|
---|
117 | m = LayerSizeParameter.ActualValue.Value;
|
---|
118 | evals = EvaluatedSolutionsParameter.ActualValue.Value;
|
---|
119 | popSize = n * m;
|
---|
120 | rand = RandomParameter.ActualValue;
|
---|
121 | qualityVariableName = QualityParameter.TranslatedName;
|
---|
122 | ageLimits = AgeLimitsParameter.ActualValue;
|
---|
123 | maximization = MaximizationParameter.ActualValue.Value;
|
---|
124 |
|
---|
125 | layers = LayersScopeParameter.ActualValue;
|
---|
126 | var newIndividual = (IScope)WorkingScopeParameter.ActualValue.Clone();
|
---|
127 | newIndividual.Name = j.ToString();
|
---|
128 |
|
---|
129 | TryMoveUp(i, j);
|
---|
130 | var currentLayer = layers.SubScopes[i];
|
---|
131 | currentLayer.SubScopes[j] = newIndividual;
|
---|
132 |
|
---|
133 | return base.Apply();
|
---|
134 | }
|
---|
135 | private void TryMoveUp(int i, int j) {
|
---|
136 | var currentLayer = layers.SubScopes[i];
|
---|
137 | var currentIndividual = currentLayer.SubScopes[j];
|
---|
138 | if (i < n - 1) {
|
---|
139 | var nextLayer = layers.SubScopes[i + 1];
|
---|
140 | int? k = FindReplaceable(nextLayer, currentIndividual);
|
---|
141 |
|
---|
142 | if (k.HasValue) {
|
---|
143 | TryMoveUp(i + 1, k.Value);
|
---|
144 | nextLayer.SubScopes[k.Value] = currentIndividual;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | private int? FindReplaceable(IScope layer, IScope individualToReplace) {
|
---|
149 | int layerNumber = ((IntValue)layer.Variables["Layer"].Value).Value;
|
---|
150 |
|
---|
151 | var individuals = (
|
---|
152 | from individual in layer.SubScopes
|
---|
153 | let quality = ((DoubleValue)individual.Variables[qualityVariableName].Value).Value
|
---|
154 | let evalsCreated = ((IntValue)individual.Variables["EvalsCreated"].Value).Value
|
---|
155 | let age = (evals - evalsCreated) / popSize
|
---|
156 | select new { individual, quality, age }
|
---|
157 | ).Select((x, index) => new { index, x.individual, x.quality, x.age })
|
---|
158 | .ToList();
|
---|
159 |
|
---|
160 | var ageLimit = layerNumber < n - 1 ? ageLimits[layerNumber] : int.MaxValue;
|
---|
161 |
|
---|
162 | var toOldIndividual = individuals.FirstOrDefault(x => x.age >= ageLimit);
|
---|
163 | if (toOldIndividual != null)
|
---|
164 | return toOldIndividual.index;
|
---|
165 |
|
---|
166 | double replaceQuality = ((DoubleValue)individualToReplace.Variables[qualityVariableName].Value).Value;
|
---|
167 | var sortedIndividuals = maximization ? individuals.OrderBy(x => x.quality) : individuals.OrderByDescending(x => x.quality);
|
---|
168 | var worstIndividual = sortedIndividuals.FirstOrDefault(x => maximization ? x.quality < replaceQuality : x.quality > replaceQuality);
|
---|
169 | if (worstIndividual != null)
|
---|
170 | return worstIndividual.index;
|
---|
171 |
|
---|
172 | return Enumerable.Range(0, layer.SubScopes.Count).SampleRandom(rand);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | } |
---|