Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5656 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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