Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3622 was 3622, checked in by abeham, 14 years ago

Changed SA to use Analyzer #999

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