Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3098 was 3094, checked in by abeham, 15 years ago

Completed main loop of simulated annealing
Added a ProbabilisticQualityComparator (derived from QualityComparator)
Added an exception in ExponentialDiscreteDoubleValueModifier when startValue, endValue, or both are <= 0.
#923

File size: 10.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.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 LookupParameter<DoubleValue> MoveQualityParameter {
49      get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
50    }
51    public ValueLookupParameter<DoubleValue> StartTemperatureParameter {
52      get { return (ValueLookupParameter<DoubleValue>)Parameters["StartTemperature"]; }
53    }
54    public ValueLookupParameter<DoubleValue> EndTemperatureParameter {
55      get { return (ValueLookupParameter<DoubleValue>)Parameters["EndTemperature"]; }
56    }
57    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
58      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
59    }
60    public ValueLookupParameter<VariableCollection> ResultsParameter {
61      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
62    }
63    public ValueLookupParameter<IOperator> MoveGeneratorParameter {
64      get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
65    }
66    public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
67      get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
68    }
69    public ValueLookupParameter<IOperator> MoveMakerParameter {
70      get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
71    }
72    public ValueLookupParameter<IOperator> AnnealingOperatorParameter {
73      get { return (ValueLookupParameter<IOperator>)Parameters["AnnealingOperator"]; }
74    }
75
76    private ScopeParameter CurrentScopeParameter {
77      get { return (ScopeParameter)Parameters["CurrentScope"]; }
78    }
79    public IScope CurrentScope {
80      get { return CurrentScopeParameter.ActualValue; }
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 LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
97      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartTemperature", "The initial temperature."));
98      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndTemperature", "The end temperature."));
99      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed."));
100      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
101
102      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
103      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
104      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
105      Parameters.Add(new ValueLookupParameter<IOperator>("AnnealingOperator", "The operator that modifies the temperature."));
106
107      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the TS should be applied."));
108      #endregion
109
110      #region Create operators
111      VariableCreator variableCreator = new VariableCreator();
112      BestQualityMemorizer initializeBestQuality = new BestQualityMemorizer();
113      SequentialSubScopesProcessor sssp = new SequentialSubScopesProcessor();
114      ResultsCollector resultsCollector = new ResultsCollector();
115      BestQualityMemorizer bestQualityMemorizer = new BestQualityMemorizer();
116      Placeholder annealingOperator = new Placeholder();
117      UniformSequentialSubScopesProcessor mainProcessor = new UniformSequentialSubScopesProcessor();
118      Placeholder moveGenerator = new Placeholder();
119      SequentialSubScopesProcessor moveEvaluationProcessor = new SequentialSubScopesProcessor();
120      Placeholder moveEvaluator = new Placeholder();
121      ProbabilisticQualityComparator qualityComparator = new ProbabilisticQualityComparator();
122      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
123      Placeholder moveMaker = new Placeholder();
124      SubScopesRemover subScopesRemover = new SubScopesRemover();
125      DataTableValuesCollector valuesCollector = new DataTableValuesCollector();
126      IntCounter iterationsCounter = new IntCounter();
127      Comparator iterationsComparator = new Comparator();
128      ConditionalBranch iterationsTermination = new ConditionalBranch();
129      EmptyOperator finished = new EmptyOperator();
130
131      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
132      variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("Qualities", new DataTable("Qualities")));
133      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Temperature", new DoubleValue(double.MaxValue)));
134
135      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
136      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Quality"));
137      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality") { ActualName = "BestQuality" });
138      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
139      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
140
141      annealingOperator.Name = "Annealing operator (placeholder)";
142      annealingOperator.OperatorParameter.ActualName = "AnnealingOperator";
143
144      moveGenerator.Name = "Move generator (placeholder)";
145      moveGenerator.OperatorParameter.ActualName = "MoveGenerator";
146
147      moveEvaluator.Name = "Move evaluator (placeholder)";
148      moveEvaluator.OperatorParameter.ActualName = "MoveEvaluator";
149
150      qualityComparator.LeftSideParameter.ActualName = "MoveQuality";
151      qualityComparator.RightSideParameter.ActualName = "Quality";
152      qualityComparator.ResultParameter.ActualName = "IsBetter";
153      qualityComparator.DampeningParameter.ActualName = "Temperature";
154
155      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
156
157      moveMaker.Name = "Move maker (placeholder)";
158      moveMaker.OperatorParameter.ActualName = "MoveMaker";
159
160      subScopesRemover.RemoveAllSubScopes = true;
161
162      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Quality"));
163      valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("BestQuality"));
164      valuesCollector.DataTableParameter.ActualName = "Qualities";
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 = "MaximumIterations";
173      iterationsComparator.ResultParameter.ActualName = "IterationsCondition";
174      iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
175
176      iterationsTermination.Name = "Iterations termination condition";
177      iterationsTermination.ConditionParameter.ActualName = "IterationsCondition";
178
179      finished.Name = "Finished";
180      #endregion
181
182      #region Create operator graph
183      OperatorGraph.InitialOperator = variableCreator;
184      variableCreator.Successor = initializeBestQuality;
185      initializeBestQuality.Successor = sssp;
186      sssp.Operators.Add(resultsCollector);
187      sssp.Successor = bestQualityMemorizer;
188      bestQualityMemorizer.Successor = annealingOperator;
189      annealingOperator.Successor = mainProcessor;
190      mainProcessor.Operator = moveGenerator;
191      mainProcessor.Successor = valuesCollector;
192      moveGenerator.Successor = moveEvaluationProcessor;
193      moveEvaluationProcessor.Operators.Add(moveEvaluator);
194      moveEvaluationProcessor.Successor = subScopesRemover;
195      moveEvaluator.Successor = qualityComparator;
196      qualityComparator.Successor = improvesQualityBranch;
197      improvesQualityBranch.TrueBranch = moveMaker;
198      valuesCollector.Successor = iterationsCounter;
199      iterationsCounter.Successor = iterationsComparator;
200      iterationsComparator.Successor = iterationsTermination;
201      iterationsTermination.TrueBranch = finished;
202      iterationsTermination.FalseBranch = bestQualityMemorizer;
203      #endregion
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.