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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.SimulatedAnnealing {
|
---|
31 | /// <summary>
|
---|
32 | /// An operator which represents a simulated annealing.
|
---|
33 | /// </summary>
|
---|
34 | [Item("SimulatedAnnealingMainLoop", "An operator which represents the main loop of a simulated annealing algorithm.")]
|
---|
35 | [StorableClass("63BA7A09-33C0-43B4-9E4E-4936F87918D6")]
|
---|
36 | public sealed class SimulatedAnnealingMainLoop : AlgorithmOperator {
|
---|
37 | #region Parameter properties
|
---|
38 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
39 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
40 | }
|
---|
41 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
42 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
43 | }
|
---|
44 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
45 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
46 | }
|
---|
47 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
48 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
49 | }
|
---|
50 | public LookupParameter<DoubleValue> MoveQualityParameter {
|
---|
51 | get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
52 | }
|
---|
53 | public ILookupParameter<DoubleValue> TemperatureParameter {
|
---|
54 | get { return (ILookupParameter<DoubleValue>)Parameters["Temperature"]; }
|
---|
55 | }
|
---|
56 | public ValueLookupParameter<DoubleValue> StartTemperatureParameter {
|
---|
57 | get { return (ValueLookupParameter<DoubleValue>)Parameters["StartTemperature"]; }
|
---|
58 | }
|
---|
59 | public ValueLookupParameter<DoubleValue> EndTemperatureParameter {
|
---|
60 | get { return (ValueLookupParameter<DoubleValue>)Parameters["EndTemperature"]; }
|
---|
61 | }
|
---|
62 | public ValueLookupParameter<IntValue> InnerIterationsParameter {
|
---|
63 | get { return (ValueLookupParameter<IntValue>)Parameters["InnerIterations"]; }
|
---|
64 | }
|
---|
65 | public LookupParameter<IntValue> IterationsParameter {
|
---|
66 | get { return (LookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
67 | }
|
---|
68 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
69 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
70 | }
|
---|
71 | public ValueLookupParameter<IOperator> MoveGeneratorParameter {
|
---|
72 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
|
---|
73 | }
|
---|
74 | public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
|
---|
75 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
|
---|
76 | }
|
---|
77 | public ValueLookupParameter<IOperator> MoveMakerParameter {
|
---|
78 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
|
---|
79 | }
|
---|
80 | public ValueLookupParameter<IOperator> AnnealingOperatorParameter {
|
---|
81 | get { return (ValueLookupParameter<IOperator>)Parameters["AnnealingOperator"]; }
|
---|
82 | }
|
---|
83 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
84 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
85 | }
|
---|
86 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
87 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
88 | }
|
---|
89 | public LookupParameter<IntValue> EvaluatedMovesParameter {
|
---|
90 | get { return (LookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | [StorableConstructor]
|
---|
95 | private SimulatedAnnealingMainLoop(bool deserializing) : base(deserializing) { }
|
---|
96 | private SimulatedAnnealingMainLoop(SimulatedAnnealingMainLoop original, Cloner cloner)
|
---|
97 | : base(original, cloner) {
|
---|
98 | }
|
---|
99 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
100 | return new SimulatedAnnealingMainLoop(this, cloner);
|
---|
101 | }
|
---|
102 | public SimulatedAnnealingMainLoop()
|
---|
103 | : base() {
|
---|
104 | Initialize();
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void Initialize() {
|
---|
108 | #region Create parameters
|
---|
109 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
110 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
111 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
112 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
113 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
|
---|
114 | Parameters.Add(new LookupParameter<DoubleValue>("Temperature", "The current temperature."));
|
---|
115 | Parameters.Add(new ValueLookupParameter<DoubleValue>("StartTemperature", "The initial temperature."));
|
---|
116 | Parameters.Add(new ValueLookupParameter<DoubleValue>("EndTemperature", "The end temperature."));
|
---|
117 | Parameters.Add(new ValueLookupParameter<IntValue>("InnerIterations", "The amount of inner iterations (number of moves before temperature is adjusted again)."));
|
---|
118 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations."));
|
---|
119 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed."));
|
---|
120 |
|
---|
121 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
|
---|
122 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
|
---|
123 | Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
|
---|
124 | Parameters.Add(new ValueLookupParameter<IOperator>("AnnealingOperator", "The operator that modifies the temperature."));
|
---|
125 |
|
---|
126 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
127 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
128 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | #region Create operators
|
---|
132 | Assigner temperatureInitializer = new Assigner();
|
---|
133 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
134 | SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
|
---|
135 | Placeholder analyzer1 = new Placeholder();
|
---|
136 | SubScopesProcessor sssp = new SubScopesProcessor();
|
---|
137 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
138 | Placeholder annealingOperator = new Placeholder();
|
---|
139 | UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
|
---|
140 | Placeholder moveGenerator = new Placeholder();
|
---|
141 | UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
|
---|
142 | Placeholder moveEvaluator = new Placeholder();
|
---|
143 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
144 | ProbabilisticQualityComparator qualityComparator = new ProbabilisticQualityComparator();
|
---|
145 | ConditionalBranch improvesQualityBranch = new ConditionalBranch();
|
---|
146 | Placeholder moveMaker = new Placeholder();
|
---|
147 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
148 | IntCounter iterationsCounter = new IntCounter();
|
---|
149 | Comparator iterationsComparator = new Comparator();
|
---|
150 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
151 | Placeholder analyzer2 = new Placeholder();
|
---|
152 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
153 |
|
---|
154 | temperatureInitializer.LeftSideParameter.ActualName = TemperatureParameter.ActualName;
|
---|
155 | temperatureInitializer.RightSideParameter.ActualName = StartTemperatureParameter.Name;
|
---|
156 |
|
---|
157 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name));
|
---|
158 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
159 |
|
---|
160 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
161 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
162 |
|
---|
163 | annealingOperator.Name = "Annealing operator (placeholder)";
|
---|
164 | annealingOperator.OperatorParameter.ActualName = AnnealingOperatorParameter.Name;
|
---|
165 |
|
---|
166 | moveGenerator.Name = "Move generator (placeholder)";
|
---|
167 | moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
168 |
|
---|
169 | moveEvaluator.Name = "Move evaluator (placeholder)";
|
---|
170 | moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
171 |
|
---|
172 | subScopesCounter.Name = "Increment EvaluatedMoves";
|
---|
173 | subScopesCounter.ValueParameter.ActualName = EvaluatedMovesParameter.Name;
|
---|
174 |
|
---|
175 | qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
|
---|
176 | qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
177 | qualityComparator.ResultParameter.ActualName = "IsBetter";
|
---|
178 | qualityComparator.DampeningParameter.ActualName = "Temperature";
|
---|
179 |
|
---|
180 | improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
|
---|
181 |
|
---|
182 | moveMaker.Name = "Move maker (placeholder)";
|
---|
183 | moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
|
---|
184 |
|
---|
185 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
186 |
|
---|
187 | iterationsCounter.Name = "Increment Iterations";
|
---|
188 | iterationsCounter.Increment = new IntValue(1);
|
---|
189 | iterationsCounter.ValueParameter.ActualName = IterationsParameter.Name;
|
---|
190 |
|
---|
191 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
192 | iterationsComparator.LeftSideParameter.ActualName = IterationsParameter.Name;
|
---|
193 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
194 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
195 | iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
|
---|
196 |
|
---|
197 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
198 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
199 |
|
---|
200 | iterationsTermination.Name = "Iterations termination condition";
|
---|
201 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
202 | #endregion
|
---|
203 |
|
---|
204 | #region Create operator graph
|
---|
205 | OperatorGraph.InitialOperator = temperatureInitializer;
|
---|
206 | temperatureInitializer.Successor = resultsCollector1;
|
---|
207 | resultsCollector1.Successor = subScopesProcessor0;
|
---|
208 | subScopesProcessor0.Operators.Add(analyzer1);
|
---|
209 | subScopesProcessor0.Successor = sssp;
|
---|
210 | analyzer1.Successor = null;
|
---|
211 | sssp.Operators.Add(resultsCollector);
|
---|
212 | sssp.Successor = annealingOperator;
|
---|
213 | resultsCollector.Successor = null;
|
---|
214 | annealingOperator.Successor = mainProcessor;
|
---|
215 | mainProcessor.Operator = moveGenerator;
|
---|
216 | mainProcessor.Successor = iterationsCounter;
|
---|
217 | moveGenerator.Successor = moveEvaluationProcessor;
|
---|
218 | moveEvaluationProcessor.Operator = moveEvaluator;
|
---|
219 | moveEvaluationProcessor.Successor = subScopesCounter;
|
---|
220 | moveEvaluator.Successor = qualityComparator;
|
---|
221 | qualityComparator.Successor = improvesQualityBranch;
|
---|
222 | improvesQualityBranch.TrueBranch = moveMaker;
|
---|
223 | improvesQualityBranch.FalseBranch = null;
|
---|
224 | improvesQualityBranch.Successor = null;
|
---|
225 | moveMaker.Successor = null;
|
---|
226 | subScopesCounter.Successor = subScopesRemover;
|
---|
227 | subScopesRemover.Successor = null;
|
---|
228 | iterationsCounter.Successor = iterationsComparator;
|
---|
229 | iterationsComparator.Successor = subScopesProcessor1;
|
---|
230 | subScopesProcessor1.Operators.Add(analyzer2);
|
---|
231 | subScopesProcessor1.Successor = iterationsTermination;
|
---|
232 | iterationsTermination.TrueBranch = null;
|
---|
233 | iterationsTermination.FalseBranch = annealingOperator;
|
---|
234 | #endregion
|
---|
235 | }
|
---|
236 |
|
---|
237 | [StorableHook(HookType.AfterDeserialization)]
|
---|
238 | private void AfterDeserialization() {
|
---|
239 | // BackwardsCompatibility3.3
|
---|
240 | #region Backwards compatible code (remove with 3.4)
|
---|
241 | if (!Parameters.ContainsKey("Iterations"))
|
---|
242 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations."));
|
---|
243 | if (!Parameters.ContainsKey("Temperature"))
|
---|
244 | Parameters.Add(new LookupParameter<DoubleValue>("Temperature", "The current temperature."));
|
---|
245 | #endregion
|
---|
246 | }
|
---|
247 |
|
---|
248 | public override IOperation Apply() {
|
---|
249 | if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
|
---|
250 | return null;
|
---|
251 | return base.Apply();
|
---|
252 | }
|
---|
253 | }
|
---|
254 | }
|
---|