Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14622 was 14555, checked in by jschiess, 8 years ago

#1836 SA reheating strategies: Refactoring, Added Connolly Reheater Operator

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