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