Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/SimulatedAnnealingMainLoop.cs @ 3164

Last change on this file since 3164 was 3142, checked in by abeham, 15 years ago

Updated simulated annealing with reporting and visualization #923

File size: 15.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Analysis;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.SimulatedAnnealing {
32  /// <summary>
33  /// An operator which represents a simulated annealing.
34  /// </summary>
35  [Item("SimulatedAnnealingMainLoop", "An operator which represents the main loop of a simulated annealing algorithm.")]
36  [StorableClass]
37  public sealed class SimulatedAnnealingMainLoop : AlgorithmOperator {
38    #region Parameter properties
39    public ValueLookupParameter<IRandom> RandomParameter {
40      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
41    }
42    public ValueLookupParameter<BoolValue> MaximizationParameter {
43      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
44    }
45    public LookupParameter<DoubleValue> QualityParameter {
46      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
47    }
48    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
49      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
50    }
51    public LookupParameter<DoubleValue> MoveQualityParameter {
52      get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
53    }
54    public ValueLookupParameter<DoubleValue> StartTemperatureParameter {
55      get { return (ValueLookupParameter<DoubleValue>)Parameters["StartTemperature"]; }
56    }
57    public ValueLookupParameter<DoubleValue> EndTemperatureParameter {
58      get { return (ValueLookupParameter<DoubleValue>)Parameters["EndTemperature"]; }
59    }
60    public ValueLookupParameter<IntValue> InnerIterationsParameter {
61      get { return (ValueLookupParameter<IntValue>)Parameters["InnerIterations"]; }
62    }
63    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
64      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
65    }
66    public ValueLookupParameter<IOperator> MoveGeneratorParameter {
67      get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
68    }
69    public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
70      get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
71    }
72    public ValueLookupParameter<IOperator> MoveMakerParameter {
73      get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
74    }
75    public ValueLookupParameter<IOperator> AnnealingOperatorParameter {
76      get { return (ValueLookupParameter<IOperator>)Parameters["AnnealingOperator"]; }
77    }
78    public ValueLookupParameter<IOperator> VisualizerParameter {
79      get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
80    }
81    public LookupParameter<IItem> VisualizationParameter {
82      get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
83    }
84    public ValueLookupParameter<VariableCollection> ResultsParameter {
85      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
86    }
87    #endregion
88
89    [StorableConstructor]
90    private SimulatedAnnealingMainLoop(bool deserializing) : base() { }
91    public SimulatedAnnealingMainLoop()
92      : base() {
93      Initialize();
94    }
95
96    private void Initialize() {
97      #region Create parameters
98      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
99      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
100      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
101      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
102      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
103      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartTemperature", "The initial temperature."));
104      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndTemperature", "The end temperature."));
105      Parameters.Add(new ValueLookupParameter<IntValue>("InnerIterations", "The amount of inner iterations (number of moves before temperature is adjusted again)."));
106      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed."));
107     
108      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
109      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
110      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
111      Parameters.Add(new ValueLookupParameter<IOperator>("AnnealingOperator", "The operator that modifies the temperature."));
112
113      Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
114      Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
115      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
116      #endregion
117
118      #region Create operators
119      VariableCreator variableCreator = new VariableCreator();
120      BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
121      BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
122      QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
123      Placeholder visualizer1 = new Placeholder();
124      SequentialSubScopesProcessor sssp = new SequentialSubScopesProcessor();
125      ResultsCollector resultsCollector = new ResultsCollector();
126      Placeholder annealingOperator = new Placeholder();
127      UniformSequentialSubScopesProcessor mainProcessor = new UniformSequentialSubScopesProcessor();
128      DataTableValuesCollector valuesCollector = new DataTableValuesCollector();
129      Placeholder moveGenerator = new Placeholder();
130      UniformSequentialSubScopesProcessor moveEvaluationProcessor = new UniformSequentialSubScopesProcessor();
131      Placeholder moveEvaluator = new Placeholder();
132      ProbabilisticQualityComparator qualityComparator = new ProbabilisticQualityComparator();
133      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
134      Placeholder moveMaker = new Placeholder();
135      SubScopesRemover subScopesRemover = new SubScopesRemover();
136      IntCounter iterationsCounter = new IntCounter();
137      Comparator iterationsComparator = new Comparator();
138      BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
139      BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
140      QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
141      Placeholder visualizer2 = new Placeholder();
142      ConditionalBranch iterationsTermination = new ConditionalBranch();
143     
144      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
145      variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("Qualities", new DataTable("Qualities")));
146      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Temperature", new DoubleValue(double.MaxValue)));
147
148      bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
149      bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
150      bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
151
152      bestQualityMemorizer2.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
153      bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
154      bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
155
156      qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
157      qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
158      qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
159      qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
160
161      visualizer1.Name = "Visualizer (placeholder)";
162      visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
163
164      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
165      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(QualityParameter.Name));
166      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality") { ActualName = "BestQuality" });
167      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
168      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
169      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
170      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
171      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
172      resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, VisualizationParameter.Name));
173     
174      annealingOperator.Name = "Annealing operator (placeholder)";
175      annealingOperator.OperatorParameter.ActualName = AnnealingOperatorParameter.Name;
176
177      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(QualityParameter.Name));
178      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality"));
179      valuesCollector.DataTableParameter.ActualName = "Qualities";
180
181      moveGenerator.Name = "Move generator (placeholder)";
182      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
183
184      moveEvaluator.Name = "Move evaluator (placeholder)";
185      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
186
187      qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
188      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
189      qualityComparator.ResultParameter.ActualName = "IsBetter";
190      qualityComparator.DampeningParameter.ActualName = "Temperature";
191
192      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
193
194      moveMaker.Name = "Move maker (placeholder)";
195      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
196
197      subScopesRemover.RemoveAllSubScopes = true;
198
199      iterationsCounter.Name = "Increment Iterations";
200      iterationsCounter.Increment = new IntValue(1);
201      iterationsCounter.ValueParameter.ActualName = "Iterations";
202
203      iterationsComparator.Name = "Iterations >= MaximumIterations";
204      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
205      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
206      iterationsComparator.ResultParameter.ActualName = "Terminate";
207      iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
208
209      bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
210      bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
211      bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
212
213      bestQualityMemorizer4.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
214      bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
215      bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
216
217      qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
218      qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
219      qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
220      qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
221
222      visualizer2.Name = "Visualizer (placeholder)";
223      visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
224
225      iterationsTermination.Name = "Iterations termination condition";
226      iterationsTermination.ConditionParameter.ActualName = "Terminate";
227      #endregion
228
229      #region Create operator graph
230      OperatorGraph.InitialOperator = variableCreator;
231      variableCreator.Successor = bestQualityMemorizer1;
232      bestQualityMemorizer1.Successor = bestQualityMemorizer2;
233      bestQualityMemorizer2.Successor = qualityDifferenceCalculator1;
234      qualityDifferenceCalculator1.Successor = visualizer1;
235      visualizer1.Successor = sssp;
236      sssp.Operators.Add(resultsCollector);
237      resultsCollector.Successor = null;
238      sssp.Successor = annealingOperator;
239      annealingOperator.Successor = mainProcessor;
240      mainProcessor.Operator = valuesCollector;
241      mainProcessor.Successor = iterationsCounter;
242      valuesCollector.Successor = moveGenerator;
243      moveGenerator.Successor = moveEvaluationProcessor;
244      moveEvaluationProcessor.Operator = moveEvaluator;
245      moveEvaluationProcessor.Successor = subScopesRemover;
246      moveEvaluator.Successor = qualityComparator;
247      qualityComparator.Successor = improvesQualityBranch;
248      improvesQualityBranch.TrueBranch = moveMaker;
249      iterationsCounter.Successor = iterationsComparator;
250      iterationsComparator.Successor = bestQualityMemorizer3;
251      bestQualityMemorizer3.Successor = bestQualityMemorizer4;
252      bestQualityMemorizer4.Successor = qualityDifferenceCalculator2;
253      qualityDifferenceCalculator2.Successor = visualizer2;
254      visualizer2.Successor = iterationsTermination;
255      iterationsTermination.TrueBranch = null;
256      iterationsTermination.FalseBranch = annealingOperator;
257      #endregion
258    }
259  }
260}
Note: See TracBrowser for help on using the repository browser.