Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13067 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 13.1 KB
RevLine 
[3093]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3093]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
[4722]22using HeuristicLab.Common;
[3093]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    }
[3142]47    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
48      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
49    }
[3093]50    public LookupParameter<DoubleValue> MoveQualityParameter {
51      get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
52    }
[8016]53    public ILookupParameter<DoubleValue> TemperatureParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters["Temperature"]; }
55    }
[3093]56    public ValueLookupParameter<DoubleValue> StartTemperatureParameter {
57      get { return (ValueLookupParameter<DoubleValue>)Parameters["StartTemperature"]; }
58    }
59    public ValueLookupParameter<DoubleValue> EndTemperatureParameter {
60      get { return (ValueLookupParameter<DoubleValue>)Parameters["EndTemperature"]; }
61    }
[3101]62    public ValueLookupParameter<IntValue> InnerIterationsParameter {
63      get { return (ValueLookupParameter<IntValue>)Parameters["InnerIterations"]; }
64    }
[5753]65    public LookupParameter<IntValue> IterationsParameter {
[6042]66      get { return (LookupParameter<IntValue>)Parameters["Iterations"]; }
[5753]67    }
[3093]68    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
69      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
70    }
71    public ValueLookupParameter<IOperator> MoveGeneratorParameter {
72      get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
73    }
74    public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
75      get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
76    }
77    public ValueLookupParameter<IOperator> MoveMakerParameter {
78      get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
79    }
80    public ValueLookupParameter<IOperator> AnnealingOperatorParameter {
81      get { return (ValueLookupParameter<IOperator>)Parameters["AnnealingOperator"]; }
82    }
[3622]83    public ValueLookupParameter<IOperator> AnalyzerParameter {
84      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
[3093]85    }
[3142]86    public ValueLookupParameter<VariableCollection> ResultsParameter {
87      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
88    }
[5356]89    public LookupParameter<IntValue> EvaluatedMovesParameter {
90      get { return (LookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }
91    }
[3093]92    #endregion
93
94    [StorableConstructor]
[4722]95    private SimulatedAnnealingMainLoop(bool deserializing) : base(deserializing) { }
96    private SimulatedAnnealingMainLoop(SimulatedAnnealingMainLoop original, Cloner cloner)
97      : base(original, cloner) {
98    }
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new SimulatedAnnealingMainLoop(this, cloner);
101    }
[3093]102    public SimulatedAnnealingMainLoop()
103      : base() {
104      Initialize();
105    }
106
107    private void Initialize() {
108      #region Create parameters
109      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
110      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
111      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[3142]112      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
[3093]113      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
[8016]114      Parameters.Add(new LookupParameter<DoubleValue>("Temperature", "The current temperature."));
[3094]115      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartTemperature", "The initial temperature."));
116      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndTemperature", "The end temperature."));
[3101]117      Parameters.Add(new ValueLookupParameter<IntValue>("InnerIterations", "The amount of inner iterations (number of moves before temperature is adjusted again)."));
[6042]118      Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations."));
[3093]119      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed."));
[4068]120
[3093]121      Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));
122      Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));
123      Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));
124      Parameters.Add(new ValueLookupParameter<IOperator>("AnnealingOperator", "The operator that modifies the temperature."));
125
[3622]126      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
[3142]127      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
[5356]128      Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));
[3093]129      #endregion
130
131      #region Create operators
[8016]132      Assigner temperatureInitializer = new Assigner();
[3622]133      ResultsCollector resultsCollector1 = new ResultsCollector();
[3626]134      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
[3622]135      Placeholder analyzer1 = new Placeholder();
[3193]136      SubScopesProcessor sssp = new SubScopesProcessor();
[3094]137      ResultsCollector resultsCollector = new ResultsCollector();
138      Placeholder annealingOperator = new Placeholder();
[3193]139      UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
[3094]140      Placeholder moveGenerator = new Placeholder();
[3193]141      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
[3094]142      Placeholder moveEvaluator = new Placeholder();
[5356]143      SubScopesCounter subScopesCounter = new SubScopesCounter();
[3094]144      ProbabilisticQualityComparator qualityComparator = new ProbabilisticQualityComparator();
145      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
146      Placeholder moveMaker = new Placeholder();
147      SubScopesRemover subScopesRemover = new SubScopesRemover();
148      IntCounter iterationsCounter = new IntCounter();
149      Comparator iterationsComparator = new Comparator();
[3626]150      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
[3622]151      Placeholder analyzer2 = new Placeholder();
[3094]152      ConditionalBranch iterationsTermination = new ConditionalBranch();
[3750]153
[8016]154      temperatureInitializer.LeftSideParameter.ActualName = TemperatureParameter.ActualName;
155      temperatureInitializer.RightSideParameter.ActualName = StartTemperatureParameter.Name;
[3094]156
[5753]157      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name));
[3622]158      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
[3142]159
[3622]160      analyzer1.Name = "Analyzer (placeholder)";
161      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3142]162
[3094]163      annealingOperator.Name = "Annealing operator (placeholder)";
[3142]164      annealingOperator.OperatorParameter.ActualName = AnnealingOperatorParameter.Name;
[3094]165
166      moveGenerator.Name = "Move generator (placeholder)";
[3142]167      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
[3094]168
169      moveEvaluator.Name = "Move evaluator (placeholder)";
[3142]170      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
[3094]171
[5356]172      subScopesCounter.Name = "Increment EvaluatedMoves";
173      subScopesCounter.ValueParameter.ActualName = EvaluatedMovesParameter.Name;
174
[3142]175      qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
176      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
[3094]177      qualityComparator.ResultParameter.ActualName = "IsBetter";
178      qualityComparator.DampeningParameter.ActualName = "Temperature";
179
180      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
181
182      moveMaker.Name = "Move maker (placeholder)";
[3142]183      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
[3094]184
185      subScopesRemover.RemoveAllSubScopes = true;
186
187      iterationsCounter.Name = "Increment Iterations";
188      iterationsCounter.Increment = new IntValue(1);
[5753]189      iterationsCounter.ValueParameter.ActualName = IterationsParameter.Name;
[3094]190
191      iterationsComparator.Name = "Iterations >= MaximumIterations";
[5753]192      iterationsComparator.LeftSideParameter.ActualName = IterationsParameter.Name;
[3142]193      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
194      iterationsComparator.ResultParameter.ActualName = "Terminate";
[3094]195      iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
196
[3622]197      analyzer2.Name = "Analyzer (placeholder)";
198      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3142]199
[3094]200      iterationsTermination.Name = "Iterations termination condition";
[3142]201      iterationsTermination.ConditionParameter.ActualName = "Terminate";
[3093]202      #endregion
203
204      #region Create operator graph
[8016]205      OperatorGraph.InitialOperator = temperatureInitializer;
206      temperatureInitializer.Successor = resultsCollector1;
[3626]207      resultsCollector1.Successor = subScopesProcessor0;
208      subScopesProcessor0.Operators.Add(analyzer1);
209      subScopesProcessor0.Successor = sssp;
[5356]210      analyzer1.Successor = null;
[3094]211      sssp.Operators.Add(resultsCollector);
[5356]212      sssp.Successor = annealingOperator;
[3142]213      resultsCollector.Successor = null;
[3094]214      annealingOperator.Successor = mainProcessor;
[3622]215      mainProcessor.Operator = moveGenerator;
[3101]216      mainProcessor.Successor = iterationsCounter;
[3094]217      moveGenerator.Successor = moveEvaluationProcessor;
[3101]218      moveEvaluationProcessor.Operator = moveEvaluator;
[5356]219      moveEvaluationProcessor.Successor = subScopesCounter;
[3094]220      moveEvaluator.Successor = qualityComparator;
221      qualityComparator.Successor = improvesQualityBranch;
222      improvesQualityBranch.TrueBranch = moveMaker;
[5356]223      improvesQualityBranch.FalseBranch = null;
224      improvesQualityBranch.Successor = null;
225      moveMaker.Successor = null;
226      subScopesCounter.Successor = subScopesRemover;
227      subScopesRemover.Successor = null;
[3094]228      iterationsCounter.Successor = iterationsComparator;
[5356]229      iterationsComparator.Successor = subScopesProcessor1;
[3626]230      subScopesProcessor1.Operators.Add(analyzer2);
231      subScopesProcessor1.Successor = iterationsTermination;
[3142]232      iterationsTermination.TrueBranch = null;
233      iterationsTermination.FalseBranch = annealingOperator;
[3093]234      #endregion
235    }
[3715]236
[6042]237    [StorableHook(HookType.AfterDeserialization)]
238    private void AfterDeserialization() {
239      // BackwardsCompatibility3.3
240      #region Backwards compatible code (remove with 3.4)
241      if (!Parameters.ContainsKey("Iterations"))
242        Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations."));
[8016]243      if (!Parameters.ContainsKey("Temperature"))
244        Parameters.Add(new LookupParameter<DoubleValue>("Temperature", "The current temperature."));
[6042]245      #endregion
246    }
247
[3715]248    public override IOperation Apply() {
249      if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
250        return null;
251      return base.Apply();
252    }
[3093]253  }
254}
Note: See TracBrowser for help on using the repository browser.