Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated simulated annealing:

  • Added inner loop to noticeably speed up algorithm
  • Removed exception in StochasticTwoOptMultiMoveGenerator when more moves would be generated than from the exhaustive move generator. The StochasticTwoOptMultiMoveGenerator simply draws from all moves with replacement.

#923

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