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
RevLine 
[3093]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    }
[3142]46    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
47      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
48    }
[3093]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    }
[3101]58    public ValueLookupParameter<IntValue> InnerIterationsParameter {
59      get { return (ValueLookupParameter<IntValue>)Parameters["InnerIterations"]; }
60    }
[3093]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    }
[3622]76    public ValueLookupParameter<IOperator> AnalyzerParameter {
77      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
[3093]78    }
[3142]79    public ValueLookupParameter<VariableCollection> ResultsParameter {
80      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
81    }
[3093]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."));
[3142]96      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
[3093]97      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
[3094]98      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartTemperature", "The initial temperature."));
99      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndTemperature", "The end temperature."));
[3101]100      Parameters.Add(new ValueLookupParameter<IntValue>("InnerIterations", "The amount of inner iterations (number of moves before temperature is adjusted again)."));
[3093]101      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed."));
[4068]102
[3093]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
[3622]108      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
[3142]109      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
[3093]110      #endregion
111
112      #region Create operators
[3094]113      VariableCreator variableCreator = new VariableCreator();
[3622]114      ResultsCollector resultsCollector1 = new ResultsCollector();
[3626]115      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
[3622]116      Placeholder analyzer1 = new Placeholder();
[3193]117      SubScopesProcessor sssp = new SubScopesProcessor();
[3094]118      ResultsCollector resultsCollector = new ResultsCollector();
119      Placeholder annealingOperator = new Placeholder();
[3193]120      UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
[3094]121      Placeholder moveGenerator = new Placeholder();
[3193]122      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
[3094]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();
[3622]130      ResultsCollector resultsCollector2 = new ResultsCollector();
[3626]131      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
[3622]132      Placeholder analyzer2 = new Placeholder();
[3094]133      ConditionalBranch iterationsTermination = new ConditionalBranch();
[3750]134
135      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class SimulatedAnnealing expects this to be called Iterations
[3094]136      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Temperature", new DoubleValue(double.MaxValue)));
137
[3622]138      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
139      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
140      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
[3142]141
[3622]142      analyzer1.Name = "Analyzer (placeholder)";
143      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3142]144
[3094]145      annealingOperator.Name = "Annealing operator (placeholder)";
[3142]146      annealingOperator.OperatorParameter.ActualName = AnnealingOperatorParameter.Name;
[3094]147
148      moveGenerator.Name = "Move generator (placeholder)";
[3142]149      moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
[3094]150
151      moveEvaluator.Name = "Move evaluator (placeholder)";
[3142]152      moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
[3094]153
[3142]154      qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
155      qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
[3094]156      qualityComparator.ResultParameter.ActualName = "IsBetter";
157      qualityComparator.DampeningParameter.ActualName = "Temperature";
158
159      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
160
161      moveMaker.Name = "Move maker (placeholder)";
[3142]162      moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
[3094]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";
[3142]172      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
173      iterationsComparator.ResultParameter.ActualName = "Terminate";
[3094]174      iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
175
[3622]176      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
177      resultsCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
178      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
[3142]179
[3622]180      analyzer2.Name = "Analyzer (placeholder)";
181      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3142]182
[3094]183      iterationsTermination.Name = "Iterations termination condition";
[3142]184      iterationsTermination.ConditionParameter.ActualName = "Terminate";
[3093]185      #endregion
186
187      #region Create operator graph
[3094]188      OperatorGraph.InitialOperator = variableCreator;
[3622]189      variableCreator.Successor = resultsCollector1;
[3626]190      resultsCollector1.Successor = subScopesProcessor0;
191      subScopesProcessor0.Operators.Add(analyzer1);
192      subScopesProcessor0.Successor = sssp;
[3094]193      sssp.Operators.Add(resultsCollector);
[3142]194      resultsCollector.Successor = null;
195      sssp.Successor = annealingOperator;
[3094]196      annealingOperator.Successor = mainProcessor;
[3622]197      mainProcessor.Operator = moveGenerator;
[3101]198      mainProcessor.Successor = iterationsCounter;
[3094]199      moveGenerator.Successor = moveEvaluationProcessor;
[3101]200      moveEvaluationProcessor.Operator = moveEvaluator;
[3094]201      moveEvaluationProcessor.Successor = subScopesRemover;
202      moveEvaluator.Successor = qualityComparator;
203      qualityComparator.Successor = improvesQualityBranch;
204      improvesQualityBranch.TrueBranch = moveMaker;
205      iterationsCounter.Successor = iterationsComparator;
[3622]206      iterationsComparator.Successor = resultsCollector2;
[3626]207      resultsCollector2.Successor = subScopesProcessor1;
208      subScopesProcessor1.Operators.Add(analyzer2);
209      subScopesProcessor1.Successor = iterationsTermination;
[3142]210      iterationsTermination.TrueBranch = null;
211      iterationsTermination.FalseBranch = annealingOperator;
[3093]212      #endregion
213    }
[3715]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    }
[3093]220  }
221}
Note: See TracBrowser for help on using the repository browser.