source: branches/jschiess/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/SimulatedAnnealingMainLoop.cs @ 14702

Last change on this file since 14702 was 14702, checked in by jschiess, 5 years ago

#1836 SA reheating strategies: Added basic temperature reset strategy

File size: 15.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.SimulatedAnnealing
31{
32    [Item("SimulatedAnnealingMainLoop", "An operator which represents the main loop of a simulated annealing algorithm.")]
33    [StorableClass]
34    public sealed class SimulatedAnnealingMainLoop : AlgorithmOperator
35    {
36        #region Strings
37        private const string MoveGeneratorName = "MoveGenerator";
38        private const string MoveEvaluatorName = "MoveEvaluator";
39        private const string MoveMakerName = "MoveMaker";
40        private const string AnnealingOperatorName = "AnnealingOperator";
41        private const string ReheatingOperatorName = "ReheatingOperator";
42        private const string MaximumIterationsName = "MaximumIterations";
43        private const string LowerTemperatureName = "LowerTemperature";
44        private const string AnalyzerName = "Analyzer";
45        private const string RandomName = "Random";
46        private const string EvaluatedMovesName = "EvaluatedMoves";
47        private const string IterationsName = "Iterations";
48        private const string TemperatureStartIndexName = "TemperatureStartIndex";
49        private const string CoolingName = "Cooling";
50        private const string StartTemperatureName = "StartTemperature";
51        private const string EndTemperatureName = "EndTemperature";
52        private const string TemperatureName = "Temperature";
53        private const string ResultsName = "Results";
54        private const string MaximizationName = "Maximization";
55        private const string QualityName = "Quality";
56        private const string BestKnownQualityName = "BestKnownQuality";
57        private const string MoveQualityName = "MoveQuality";
58        private const string IsAcceptedName = "IsAccepted";
59        private const string TerminateName = "Terminate";
60        private const string AcceptanceMemoryName = "AcceptanceMemory";
61        private const string ReheatCountName = "ReheatCount";
62        #endregion
63
64        #region Parameter properties
65        public IValueLookupParameter<IRandom> RandomParameter
66        {
67            get { return (IValueLookupParameter<IRandom>)Parameters[RandomName]; }
68        }
69        public IValueLookupParameter<BoolValue> MaximizationParameter
70        {
71            get { return (IValueLookupParameter<BoolValue>)Parameters[MaximizationName]; }
72        }
73        public ILookupParameter<DoubleValue> QualityParameter
74        {
75            get { return (ILookupParameter<DoubleValue>)Parameters[QualityName]; }
76        }
77        public IValueLookupParameter<DoubleValue> BestKnownQualityParameter
78        {
79            get { return (IValueLookupParameter<DoubleValue>)Parameters[BestKnownQualityName]; }
80        }
81        public ILookupParameter<DoubleValue> MoveQualityParameter
82        {
83            get { return (ILookupParameter<DoubleValue>)Parameters[MoveQualityName]; }
84        }
85        public ILookupParameter<DoubleValue> TemperatureParameter
86        {
87            get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; }
88        }
89        public IValueLookupParameter<DoubleValue> LowerTemperatureParameter
90        {
91            get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
92        }
93        public ILookupParameter<IntValue> IterationsParameter
94        {
95            get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; }
96        }
97        public IValueLookupParameter<IntValue> MaximumIterationsParameter
98        {
99            get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsName]; }
100        }
101        public IValueLookupParameter<IOperator> MoveGeneratorParameter
102        {
103            get { return (IValueLookupParameter<IOperator>)Parameters[MoveGeneratorName]; }
104        }
105        public IValueLookupParameter<IOperator> MoveEvaluatorParameter
106        {
107            get { return (IValueLookupParameter<IOperator>)Parameters[MoveEvaluatorName]; }
108        }
109        public IValueLookupParameter<IOperator> MoveMakerParameter
110        {
111            get { return (IValueLookupParameter<IOperator>)Parameters[MoveMakerName]; }
112        }
113        public IValueLookupParameter<IOperator> AnnealingOperatorParameter
114        {
115            get { return (IValueLookupParameter<IOperator>)Parameters[AnnealingOperatorName]; }
116        }
117        public IValueLookupParameter<IOperator> ReheatingOperatorParameter
118        {
119            get { return (IValueLookupParameter<IOperator>)Parameters[ReheatingOperatorName]; }
120        }
121        public IValueLookupParameter<IOperator> AnalyzerParameter
122        {
123            get { return (IValueLookupParameter<IOperator>)Parameters[AnalyzerName]; }
124        }
125        public IValueLookupParameter<VariableCollection> ResultsParameter
126        {
127            get { return (IValueLookupParameter<VariableCollection>)Parameters[ResultsName]; }
128        }
129        public ILookupParameter<IntValue> EvaluatedMovesParameter
130        {
131            get { return (ILookupParameter<IntValue>)Parameters[EvaluatedMovesName]; }
132        }
133        public ILookupParameter<DoubleValue> StartTemperatureParameter
134        {
135            get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; }
136        }
137        public ILookupParameter<DoubleValue> EndTemperatureParameter
138        {
139            get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; }
140        }
141        public ILookupParameter<IntValue> TemperatureStartIndexParameter
142        {
143            get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; }
144        }
145        public ILookupParameter<BoolValue> CoolingParameter
146        {
147            get { return (ILookupParameter<BoolValue>)Parameters[CoolingName]; }
148        }
149        #endregion
150
151        [StorableConstructor]
152        private SimulatedAnnealingMainLoop(bool deserializing) : base(deserializing) { }
153        private SimulatedAnnealingMainLoop(SimulatedAnnealingMainLoop original, Cloner cloner)
154          : base(original, cloner)
155        {
156        }
157        public override IDeepCloneable Clone(Cloner cloner)
158        {
159            return new SimulatedAnnealingMainLoop(this, cloner);
160        }
161        public SimulatedAnnealingMainLoop()
162          : base()
163        {
164            Initialize();
165        }
166
167        private void Initialize()
168        {
169            #region Create parameters
170            Parameters.Add(new ValueLookupParameter<IRandom>(RandomName, "A pseudo random number generator."));
171            Parameters.Add(new ValueLookupParameter<BoolValue>(MaximizationName, "True if the problem is a maximization problem, otherwise false."));
172            Parameters.Add(new LookupParameter<DoubleValue>(QualityName, "The value which represents the quality of a solution."));
173            Parameters.Add(new ValueLookupParameter<DoubleValue>(BestKnownQualityName, "The best known quality value found so far."));
174            Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move."));
175            Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature."));
176            Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature."));
177            Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations."));
178            Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed."));
179
180            Parameters.Add(new ValueLookupParameter<IOperator>(MoveGeneratorName, "The operator that generates the moves."));
181            Parameters.Add(new ValueLookupParameter<IOperator>(MoveEvaluatorName, "The operator that evaluates a move."));
182            Parameters.Add(new ValueLookupParameter<IOperator>(MoveMakerName, "The operator that performs a move and updates the quality."));
183            Parameters.Add(new ValueLookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature."));
184            Parameters.Add(new ValueLookupParameter<IOperator>(ReheatingOperatorName, "The operator that reheats the temperature if necessary."));
185
186            Parameters.Add(new ValueLookupParameter<IOperator>(AnalyzerName, "The operator used to analyze each generation."));
187            Parameters.Add(new ValueLookupParameter<VariableCollection>(ResultsName, "The variable collection where results should be stored."));
188            Parameters.Add(new LookupParameter<IntValue>(EvaluatedMovesName, "The number of evaluated moves."));
189
190            Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed."));
191            Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise."));
192            Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur."));
193            Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled or heated."));
194            #endregion
195
196            #region Create operators
197            var variableCreator = new VariableCreator();
198            var ssp1 = new SubScopesProcessor();
199            var analyzer1 = new Placeholder();
200            var ssp2 = new SubScopesProcessor();
201            var resultsCollector = new ResultsCollector();
202            var mainProcessor = new SubScopesProcessor();
203            var moveGenerator = new Placeholder();
204            var moveEvaluationProcessor = new SubScopesProcessor();
205            var moveEvaluator = new Placeholder();
206            var evaluatedMovesCounter = new IntCounter();
207            var qualityComparator = new ProbabilisticQualityComparator();
208            var acceptsQualityBranch = new ConditionalBranch();
209            var moveMaker = new Placeholder();
210            var temperatureController = new TemperatureController();
211            var subScopesRemover = new SubScopesRemover();
212            var iterationsCounter = new IntCounter();
213            var iterationsComparator = new Comparator();
214            var ssp3 = new SubScopesProcessor();
215            var analyzer2 = new Placeholder();
216            var iterationsTermination = new ConditionalBranch();
217
218            variableCreator.Name = "Initialize Memory";
219            variableCreator.CollectedValues.Add(new ValueParameter<ItemList<BoolValue>>(AcceptanceMemoryName, new ItemList<BoolValue>()));
220            variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(ReheatCountName, new IntValue(0)));
221
222            analyzer1.Name = "Analyzer";
223            analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
224
225            moveGenerator.Name = "Move generator";
226            moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
227
228            moveEvaluator.Name = "Move evaluator";
229            moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
230
231            evaluatedMovesCounter.Name = "EvaluatedMoves++";
232            evaluatedMovesCounter.ValueParameter.ActualName = EvaluatedMovesParameter.Name;
233            evaluatedMovesCounter.Increment = new IntValue(1);
234
235            qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
236            qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
237            qualityComparator.ResultParameter.ActualName = IsAcceptedName;
238            qualityComparator.DampeningParameter.ActualName = TemperatureParameter.Name;
239
240            acceptsQualityBranch.ConditionParameter.ActualName = IsAcceptedName;
241
242            moveMaker.Name = "Move maker";
243            moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
244
245           
246
247            subScopesRemover.RemoveAllSubScopes = true;
248
249            iterationsCounter.Name = "Iterations++";
250            iterationsCounter.Increment = new IntValue(1);
251            iterationsCounter.ValueParameter.ActualName = IterationsParameter.Name;
252
253            iterationsComparator.Name = "Iterations >= MaximumIterations";
254            iterationsComparator.LeftSideParameter.ActualName = IterationsParameter.Name;
255            iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
256            iterationsComparator.ResultParameter.ActualName = TerminateName;
257            iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
258
259            analyzer2.Name = "Analyzer (placeholder)";
260            analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
261
262            iterationsTermination.Name = "Iterations termination condition";
263            iterationsTermination.ConditionParameter.ActualName = TerminateName;
264            #endregion
265
266            #region Create operator graph
267            OperatorGraph.InitialOperator = variableCreator;
268            variableCreator.Successor = ssp1;
269            ssp1.Operators.Add(analyzer1);
270            ssp1.Successor = ssp2;
271            analyzer1.Successor = null;
272            ssp2.Operators.Add(resultsCollector);
273            ssp2.Successor = mainProcessor;
274            resultsCollector.Successor = null;
275            mainProcessor.Operators.Add(moveGenerator);
276            mainProcessor.Successor = iterationsCounter;
277            moveGenerator.Successor = moveEvaluationProcessor;
278            moveEvaluationProcessor.Operators.Add(moveEvaluator);
279            moveEvaluationProcessor.Successor = evaluatedMovesCounter;
280            moveEvaluator.Successor = qualityComparator;
281            qualityComparator.Successor = acceptsQualityBranch;
282            acceptsQualityBranch.TrueBranch = moveMaker;
283            acceptsQualityBranch.FalseBranch = null;
284            acceptsQualityBranch.Successor = temperatureController;
285            moveMaker.Successor = null;
286            temperatureController.Successor = null;
287            evaluatedMovesCounter.Successor = subScopesRemover;
288            subScopesRemover.Successor = null;
289            iterationsCounter.Successor = iterationsComparator;
290            iterationsComparator.Successor = ssp3;
291            ssp3.Operators.Add(analyzer2);
292            ssp3.Successor = iterationsTermination;
293            iterationsTermination.TrueBranch = null;
294            iterationsTermination.FalseBranch = mainProcessor;
295            #endregion
296        }
297
298        public override IOperation Apply()
299        {
300            if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
301                return null;
302            return base.Apply();
303        }
304    }
305}
Note: See TracBrowser for help on using the repository browser.