Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4695 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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