[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 |
|
---|
| 22 | using HeuristicLab.Analysis;
|
---|
[3376] | 23 | using HeuristicLab.Common;
|
---|
[3093] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Selection;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Algorithms.SimulatedAnnealing {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// An operator which represents a simulated annealing.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("SimulatedAnnealingMainLoop", "An operator which represents the main loop of a simulated annealing algorithm.")]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | public sealed class SimulatedAnnealingMainLoop : AlgorithmOperator {
|
---|
| 39 | #region Parameter properties
|
---|
| 40 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 41 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 42 | }
|
---|
| 43 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 44 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 45 | }
|
---|
| 46 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
| 47 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 48 | }
|
---|
[3142] | 49 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 50 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 51 | }
|
---|
[3093] | 52 | public LookupParameter<DoubleValue> MoveQualityParameter {
|
---|
| 53 | get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
| 54 | }
|
---|
| 55 | public ValueLookupParameter<DoubleValue> StartTemperatureParameter {
|
---|
| 56 | get { return (ValueLookupParameter<DoubleValue>)Parameters["StartTemperature"]; }
|
---|
| 57 | }
|
---|
| 58 | public ValueLookupParameter<DoubleValue> EndTemperatureParameter {
|
---|
| 59 | get { return (ValueLookupParameter<DoubleValue>)Parameters["EndTemperature"]; }
|
---|
| 60 | }
|
---|
[3101] | 61 | public ValueLookupParameter<IntValue> InnerIterationsParameter {
|
---|
| 62 | get { return (ValueLookupParameter<IntValue>)Parameters["InnerIterations"]; }
|
---|
| 63 | }
|
---|
[3093] | 64 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 65 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 66 | }
|
---|
| 67 | public ValueLookupParameter<IOperator> MoveGeneratorParameter {
|
---|
| 68 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }
|
---|
| 69 | }
|
---|
| 70 | public ValueLookupParameter<IOperator> MoveEvaluatorParameter {
|
---|
| 71 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }
|
---|
| 72 | }
|
---|
| 73 | public ValueLookupParameter<IOperator> MoveMakerParameter {
|
---|
| 74 | get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }
|
---|
| 75 | }
|
---|
| 76 | public ValueLookupParameter<IOperator> AnnealingOperatorParameter {
|
---|
| 77 | get { return (ValueLookupParameter<IOperator>)Parameters["AnnealingOperator"]; }
|
---|
| 78 | }
|
---|
[3622] | 79 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 80 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
[3093] | 81 | }
|
---|
[3142] | 82 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 83 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 84 | }
|
---|
[3093] | 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."));
|
---|
[3142] | 99 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
[3093] | 100 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));
|
---|
[3094] | 101 | Parameters.Add(new ValueLookupParameter<DoubleValue>("StartTemperature", "The initial temperature."));
|
---|
| 102 | Parameters.Add(new ValueLookupParameter<DoubleValue>("EndTemperature", "The end temperature."));
|
---|
[3101] | 103 | Parameters.Add(new ValueLookupParameter<IntValue>("InnerIterations", "The amount of inner iterations (number of moves before temperature is adjusted again)."));
|
---|
[3093] | 104 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed."));
|
---|
[3142] | 105 |
|
---|
[3093] | 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 |
|
---|
[3622] | 111 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
[3142] | 112 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
[3093] | 113 | #endregion
|
---|
| 114 |
|
---|
| 115 | #region Create operators
|
---|
[3094] | 116 | VariableCreator variableCreator = new VariableCreator();
|
---|
[3622] | 117 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
[3626] | 118 | SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
|
---|
[3622] | 119 | Placeholder analyzer1 = new Placeholder();
|
---|
[3193] | 120 | SubScopesProcessor sssp = new SubScopesProcessor();
|
---|
[3094] | 121 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
| 122 | Placeholder annealingOperator = new Placeholder();
|
---|
[3193] | 123 | UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
|
---|
[3094] | 124 | Placeholder moveGenerator = new Placeholder();
|
---|
[3193] | 125 | UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
|
---|
[3094] | 126 | Placeholder moveEvaluator = new Placeholder();
|
---|
| 127 | ProbabilisticQualityComparator qualityComparator = new ProbabilisticQualityComparator();
|
---|
| 128 | ConditionalBranch improvesQualityBranch = new ConditionalBranch();
|
---|
| 129 | Placeholder moveMaker = new Placeholder();
|
---|
| 130 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
| 131 | IntCounter iterationsCounter = new IntCounter();
|
---|
| 132 | Comparator iterationsComparator = new Comparator();
|
---|
[3622] | 133 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
[3626] | 134 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
[3622] | 135 | Placeholder analyzer2 = new Placeholder();
|
---|
[3094] | 136 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
[3750] | 137 |
|
---|
| 138 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); // Class SimulatedAnnealing expects this to be called Iterations
|
---|
[3094] | 139 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Temperature", new DoubleValue(double.MaxValue)));
|
---|
| 140 |
|
---|
[3622] | 141 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
| 142 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
|
---|
| 143 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[3142] | 144 |
|
---|
[3622] | 145 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 146 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3142] | 147 |
|
---|
[3094] | 148 | annealingOperator.Name = "Annealing operator (placeholder)";
|
---|
[3142] | 149 | annealingOperator.OperatorParameter.ActualName = AnnealingOperatorParameter.Name;
|
---|
[3094] | 150 |
|
---|
| 151 | moveGenerator.Name = "Move generator (placeholder)";
|
---|
[3142] | 152 | moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
[3094] | 153 |
|
---|
| 154 | moveEvaluator.Name = "Move evaluator (placeholder)";
|
---|
[3142] | 155 | moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
[3094] | 156 |
|
---|
[3142] | 157 | qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;
|
---|
| 158 | qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;
|
---|
[3094] | 159 | qualityComparator.ResultParameter.ActualName = "IsBetter";
|
---|
| 160 | qualityComparator.DampeningParameter.ActualName = "Temperature";
|
---|
| 161 |
|
---|
| 162 | improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
|
---|
| 163 |
|
---|
| 164 | moveMaker.Name = "Move maker (placeholder)";
|
---|
[3142] | 165 | moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;
|
---|
[3094] | 166 |
|
---|
| 167 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 168 |
|
---|
| 169 | iterationsCounter.Name = "Increment Iterations";
|
---|
| 170 | iterationsCounter.Increment = new IntValue(1);
|
---|
| 171 | iterationsCounter.ValueParameter.ActualName = "Iterations";
|
---|
| 172 |
|
---|
| 173 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
| 174 | iterationsComparator.LeftSideParameter.ActualName = "Iterations";
|
---|
[3142] | 175 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 176 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
[3094] | 177 | iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
|
---|
| 178 |
|
---|
[3622] | 179 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
| 180 | resultsCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
|
---|
| 181 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[3142] | 182 |
|
---|
[3622] | 183 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 184 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3142] | 185 |
|
---|
[3094] | 186 | iterationsTermination.Name = "Iterations termination condition";
|
---|
[3142] | 187 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
[3093] | 188 | #endregion
|
---|
| 189 |
|
---|
| 190 | #region Create operator graph
|
---|
[3094] | 191 | OperatorGraph.InitialOperator = variableCreator;
|
---|
[3622] | 192 | variableCreator.Successor = resultsCollector1;
|
---|
[3626] | 193 | resultsCollector1.Successor = subScopesProcessor0;
|
---|
| 194 | subScopesProcessor0.Operators.Add(analyzer1);
|
---|
| 195 | subScopesProcessor0.Successor = sssp;
|
---|
[3094] | 196 | sssp.Operators.Add(resultsCollector);
|
---|
[3142] | 197 | resultsCollector.Successor = null;
|
---|
| 198 | sssp.Successor = annealingOperator;
|
---|
[3094] | 199 | annealingOperator.Successor = mainProcessor;
|
---|
[3622] | 200 | mainProcessor.Operator = moveGenerator;
|
---|
[3101] | 201 | mainProcessor.Successor = iterationsCounter;
|
---|
[3094] | 202 | moveGenerator.Successor = moveEvaluationProcessor;
|
---|
[3101] | 203 | moveEvaluationProcessor.Operator = moveEvaluator;
|
---|
[3094] | 204 | moveEvaluationProcessor.Successor = subScopesRemover;
|
---|
| 205 | moveEvaluator.Successor = qualityComparator;
|
---|
| 206 | qualityComparator.Successor = improvesQualityBranch;
|
---|
| 207 | improvesQualityBranch.TrueBranch = moveMaker;
|
---|
| 208 | iterationsCounter.Successor = iterationsComparator;
|
---|
[3622] | 209 | iterationsComparator.Successor = resultsCollector2;
|
---|
[3626] | 210 | resultsCollector2.Successor = subScopesProcessor1;
|
---|
| 211 | subScopesProcessor1.Operators.Add(analyzer2);
|
---|
| 212 | subScopesProcessor1.Successor = iterationsTermination;
|
---|
[3142] | 213 | iterationsTermination.TrueBranch = null;
|
---|
| 214 | iterationsTermination.FalseBranch = annealingOperator;
|
---|
[3093] | 215 | #endregion
|
---|
| 216 | }
|
---|
[3715] | 217 |
|
---|
| 218 | public override IOperation Apply() {
|
---|
| 219 | if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)
|
---|
| 220 | return null;
|
---|
| 221 | return base.Apply();
|
---|
| 222 | }
|
---|
[3093] | 223 | }
|
---|
| 224 | }
|
---|