Free cookie consent management tool by TermsFeed Policy Generator

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

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

#893

  • Fixed wiring of iteration based operators like the michalewicz manipulators for real vector encoding
File size: 11.7 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      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
119      Placeholder analyzer1 = new Placeholder();
120      SubScopesProcessor sssp = new SubScopesProcessor();
121      ResultsCollector resultsCollector = new ResultsCollector();
122      Placeholder annealingOperator = new Placeholder();
123      UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
124      Placeholder moveGenerator = new Placeholder();
125      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
126      Placeholder moveEvaluator = new Placeholder();
127      ProbabilisticQualityComparator qualityComparator = new ProbabilisticQualityComparator();
128      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
129      Placeholder moveMaker = new Placeholder();
130      SubScopesRemover subScopesRemover = new SubScopesRemover();
131      IntCounter iterationsCounter = new IntCounter();
132      Comparator iterationsComparator = new Comparator();
133      ResultsCollector resultsCollector2 = new ResultsCollector();
134      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
135      Placeholder analyzer2 = new Placeholder();
136      ConditionalBranch iterationsTermination = new ConditionalBranch();
137
138      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class SimulatedAnnealing expects this to be called Iterations
139      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Temperature", new DoubleValue(double.MaxValue)));
140
141      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
142      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
143      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
144
145      analyzer1.Name = "Analyzer (placeholder)";
146      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
147
148      annealingOperator.Name = "Annealing operator (placeholder)";
149      annealingOperator.OperatorParameter.ActualName = AnnealingOperatorParameter.Name;
150
151      moveGenerator.Name = "Move generator (placeholder)";
152      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
153
154      moveEvaluator.Name = "Move evaluator (placeholder)";
155      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
156
157      qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
158      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
159      qualityComparator.ResultParameter.ActualName = "IsBetter";
160      qualityComparator.DampeningParameter.ActualName = "Temperature";
161
162      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
163
164      moveMaker.Name = "Move maker (placeholder)";
165      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
166
167      subScopesRemover.RemoveAllSubScopes = true;
168
169      iterationsCounter.Name = "Increment Iterations";
170      iterationsCounter.Increment = new IntValue(1);
171      iterationsCounter.ValueParameter.ActualName = "Iterations";
172
173      iterationsComparator.Name = "Iterations >= MaximumIterations";
174      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
175      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
176      iterationsComparator.ResultParameter.ActualName = "Terminate";
177      iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
178
179      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
180      resultsCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
181      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
182
183      analyzer2.Name = "Analyzer (placeholder)";
184      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
185
186      iterationsTermination.Name = "Iterations termination condition";
187      iterationsTermination.ConditionParameter.ActualName = "Terminate";
188      #endregion
189
190      #region Create operator graph
191      OperatorGraph.InitialOperator = variableCreator;
192      variableCreator.Successor = resultsCollector1;
193      resultsCollector1.Successor = subScopesProcessor0;
194      subScopesProcessor0.Operators.Add(analyzer1);
195      subScopesProcessor0.Successor = sssp;
196      sssp.Operators.Add(resultsCollector);
197      resultsCollector.Successor = null;
198      sssp.Successor = annealingOperator;
199      annealingOperator.Successor = mainProcessor;
200      mainProcessor.Operator = moveGenerator;
201      mainProcessor.Successor = iterationsCounter;
202      moveGenerator.Successor = moveEvaluationProcessor;
203      moveEvaluationProcessor.Operator = moveEvaluator;
204      moveEvaluationProcessor.Successor = subScopesRemover;
205      moveEvaluator.Successor = qualityComparator;
206      qualityComparator.Successor = improvesQualityBranch;
207      improvesQualityBranch.TrueBranch = moveMaker;
208      iterationsCounter.Successor = iterationsComparator;
209      iterationsComparator.Successor = resultsCollector2;
210      resultsCollector2.Successor = subScopesProcessor1;
211      subScopesProcessor1.Operators.Add(analyzer2);
212      subScopesProcessor1.Successor = iterationsTermination;
213      iterationsTermination.TrueBranch = null;
214      iterationsTermination.FalseBranch = annealingOperator;
215      #endregion
216    }
217
218    public override IOperation Apply() {
219      if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
220        return null;
221      return base.Apply();
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.