Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5304 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

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