[14406] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14452] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[14406] | 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 System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Analysis;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Optimization.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.PluginInfrastructure;
|
---|
| 34 | using HeuristicLab.Random;
|
---|
| 35 |
|
---|
[14452] | 36 | namespace HeuristicLab.Algorithms.SimulatedAnnealing
|
---|
| 37 | {
|
---|
| 38 | [Item("Simulated Annealing", "An advanced simulated annealing algorithm.")]
|
---|
| 39 | [Creatable("Algorithms")]
|
---|
| 40 | [StorableClass]
|
---|
| 41 | public sealed class SimulatedAnnealing : HeuristicOptimizationEngineAlgorithm, IStorableContent
|
---|
| 42 | {
|
---|
| 43 | #region Strings
|
---|
| 44 | private const string SeedName = "Seed";
|
---|
| 45 | private const string SetSeedRandomlyName = "SetSeedRandomly";
|
---|
| 46 | private const string MoveGeneratorName = "MoveGenerator";
|
---|
| 47 | private const string MoveEvaluatorName = "MoveEvaluator";
|
---|
| 48 | private const string MoveMakerName = "MoveMaker";
|
---|
| 49 | private const string AnnealingOperatorName = "AnnealingOperator";
|
---|
[14555] | 50 | private const string ReheatingOperatorName = "ReheatingOperator";
|
---|
[14452] | 51 | private const string MaximumIterationsName = "MaximumIterations";
|
---|
| 52 | private const string InitialTemperatureName = "InitialTemperature";
|
---|
| 53 | private const string LowerTemperatureName = "LowerTemperature";
|
---|
| 54 | private const string AnalyzerName = "Analyzer";
|
---|
| 55 | private const string RandomName = "Random";
|
---|
| 56 | private const string EvaluatedMovesName = "EvaluatedMoves";
|
---|
| 57 | private const string IterationsName = "Iterations";
|
---|
| 58 | private const string TemperatureStartIndexName = "TemperatureStartIndex";
|
---|
| 59 | private const string CoolingName = "Cooling";
|
---|
| 60 | private const string StartTemperatureName = "StartTemperature";
|
---|
| 61 | private const string EndTemperatureName = "EndTemperature";
|
---|
| 62 | private const string TemperatureName = "Temperature";
|
---|
| 63 | private const string ResultsName = "Results";
|
---|
| 64 | private const string TemperatureChartName = "Temperature Chart";
|
---|
| 65 | private const string TemperatureAnalyzerName = "Temperature Analyzer";
|
---|
[15315] | 66 | private const string TemperatureInitializerName = "TemperatureInitializer";
|
---|
[14452] | 67 | #endregion
|
---|
[14406] | 68 |
|
---|
[14452] | 69 | public string Filename { get; set; }
|
---|
[14406] | 70 |
|
---|
[14452] | 71 | #region Problem Properties
|
---|
| 72 | public override Type ProblemType
|
---|
| 73 | {
|
---|
| 74 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
| 75 | }
|
---|
| 76 | public new ISingleObjectiveHeuristicOptimizationProblem Problem
|
---|
| 77 | {
|
---|
| 78 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
| 79 | set { base.Problem = value; }
|
---|
| 80 | }
|
---|
| 81 | #endregion
|
---|
[14406] | 82 |
|
---|
[14452] | 83 | #region Parameter Properties
|
---|
| 84 | private ValueParameter<IntValue> SeedParameter
|
---|
| 85 | {
|
---|
| 86 | get { return (ValueParameter<IntValue>)Parameters[SeedName]; }
|
---|
| 87 | }
|
---|
| 88 | private ValueParameter<BoolValue> SetSeedRandomlyParameter
|
---|
| 89 | {
|
---|
| 90 | get { return (ValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
|
---|
| 91 | }
|
---|
| 92 | public IConstrainedValueParameter<IMultiMoveGenerator> MoveGeneratorParameter
|
---|
| 93 | {
|
---|
| 94 | get { return (IConstrainedValueParameter<IMultiMoveGenerator>)Parameters[MoveGeneratorName]; }
|
---|
| 95 | }
|
---|
| 96 | public IConstrainedValueParameter<IMoveMaker> MoveMakerParameter
|
---|
| 97 | {
|
---|
| 98 | get { return (IConstrainedValueParameter<IMoveMaker>)Parameters[MoveMakerName]; }
|
---|
| 99 | }
|
---|
| 100 | public IConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter
|
---|
| 101 | {
|
---|
| 102 | get { return (IConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters[MoveEvaluatorName]; }
|
---|
| 103 | }
|
---|
| 104 | public IConstrainedValueParameter<IDiscreteDoubleValueModifier> AnnealingOperatorParameter
|
---|
| 105 | {
|
---|
| 106 | get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters[AnnealingOperatorName]; }
|
---|
| 107 | }
|
---|
[14555] | 108 |
|
---|
| 109 | public IConstrainedValueParameter<IReheatingOperator> ReheatingOperatorParameter
|
---|
[14452] | 110 | {
|
---|
[14555] | 111 | get { return (IConstrainedValueParameter<IReheatingOperator>)Parameters[ReheatingOperatorName]; }
|
---|
[14452] | 112 | }
|
---|
[15315] | 113 | public IConstrainedValueParameter<ITemperatureInitializer> TemperatureInitializerParameter
|
---|
| 114 | {
|
---|
| 115 | get { return (IConstrainedValueParameter<ITemperatureInitializer>)Parameters[TemperatureInitializerName]; }
|
---|
| 116 | }
|
---|
[14406] | 117 |
|
---|
[14452] | 118 | private ValueParameter<IntValue> MaximumIterationsParameter
|
---|
| 119 | {
|
---|
| 120 | get { return (ValueParameter<IntValue>)Parameters[MaximumIterationsName]; }
|
---|
| 121 | }
|
---|
| 122 | private ValueParameter<DoubleValue> LowerTemperatureParameter
|
---|
| 123 | {
|
---|
| 124 | get { return (ValueParameter<DoubleValue>)Parameters[LowerTemperatureName]; }
|
---|
| 125 | }
|
---|
| 126 | private ValueParameter<MultiAnalyzer> AnalyzerParameter
|
---|
| 127 | {
|
---|
| 128 | get { return (ValueParameter<MultiAnalyzer>)Parameters[AnalyzerName]; }
|
---|
| 129 | }
|
---|
| 130 | #endregion
|
---|
[14406] | 131 |
|
---|
[14452] | 132 | #region Properties
|
---|
| 133 | public IntValue Seed
|
---|
| 134 | {
|
---|
| 135 | get { return SeedParameter.Value; }
|
---|
| 136 | set { SeedParameter.Value = value; }
|
---|
| 137 | }
|
---|
| 138 | public BoolValue SetSeedRandomly
|
---|
| 139 | {
|
---|
| 140 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 141 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 142 | }
|
---|
| 143 | public IMultiMoveGenerator MoveGenerator
|
---|
| 144 | {
|
---|
| 145 | get { return MoveGeneratorParameter.Value; }
|
---|
| 146 | set { MoveGeneratorParameter.Value = value; }
|
---|
| 147 | }
|
---|
| 148 | public IMoveMaker MoveMaker
|
---|
| 149 | {
|
---|
| 150 | get { return MoveMakerParameter.Value; }
|
---|
| 151 | set { MoveMakerParameter.Value = value; }
|
---|
| 152 | }
|
---|
| 153 | public ISingleObjectiveMoveEvaluator MoveEvaluator
|
---|
| 154 | {
|
---|
| 155 | get { return MoveEvaluatorParameter.Value; }
|
---|
| 156 | set { MoveEvaluatorParameter.Value = value; }
|
---|
| 157 | }
|
---|
| 158 | public IDiscreteDoubleValueModifier AnnealingOperator
|
---|
| 159 | {
|
---|
| 160 | get { return AnnealingOperatorParameter.Value; }
|
---|
| 161 | set { AnnealingOperatorParameter.Value = value; }
|
---|
| 162 | }
|
---|
[14555] | 163 |
|
---|
| 164 | public IReheatingOperator ReheatingOperator
|
---|
[14452] | 165 | {
|
---|
[14555] | 166 | get { return ReheatingOperatorParameter.Value; }
|
---|
| 167 | set { ReheatingOperatorParameter.Value = value; }
|
---|
[14452] | 168 | }
|
---|
[15315] | 169 | public ITemperatureInitializer TemperatureInitializer
|
---|
| 170 | {
|
---|
| 171 | get { return TemperatureInitializerParameter.Value; }
|
---|
| 172 | set { TemperatureInitializerParameter.Value = value; }
|
---|
| 173 | }
|
---|
[14406] | 174 |
|
---|
[14452] | 175 | public IntValue MaximumIterations
|
---|
| 176 | {
|
---|
| 177 | get { return MaximumIterationsParameter.Value; }
|
---|
| 178 | set { MaximumIterationsParameter.Value = value; }
|
---|
| 179 | }
|
---|
| 180 | public DoubleValue LowerTemperature
|
---|
| 181 | {
|
---|
| 182 | get { return LowerTemperatureParameter.Value; }
|
---|
| 183 | set { LowerTemperatureParameter.Value = value; }
|
---|
| 184 | }
|
---|
| 185 | public MultiAnalyzer Analyzer
|
---|
| 186 | {
|
---|
| 187 | get { return AnalyzerParameter.Value; }
|
---|
| 188 | set { AnalyzerParameter.Value = value; }
|
---|
| 189 | }
|
---|
| 190 | private RandomCreator RandomCreator
|
---|
| 191 | {
|
---|
| 192 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 193 | }
|
---|
| 194 | private SolutionsCreator SolutionsCreator
|
---|
| 195 | {
|
---|
| 196 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
| 197 | }
|
---|
| 198 | private SimulatedAnnealingMainLoop MainLoop
|
---|
| 199 | {
|
---|
| 200 | get { return OperatorGraph.Iterate().OfType<SimulatedAnnealingMainLoop>().First(); }
|
---|
| 201 | }
|
---|
| 202 | #endregion
|
---|
[14406] | 203 |
|
---|
[14452] | 204 | [StorableConstructor]
|
---|
| 205 | private SimulatedAnnealing(bool deserializing) : base(deserializing) { }
|
---|
| 206 | private SimulatedAnnealing(SimulatedAnnealing original, Cloner cloner)
|
---|
| 207 | : base(original, cloner)
|
---|
| 208 | {
|
---|
| 209 | RegisterEventHandlers();
|
---|
| 210 | }
|
---|
| 211 | public SimulatedAnnealing()
|
---|
| 212 | : base()
|
---|
| 213 | {
|
---|
| 214 | Parameters.Add(new ValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 215 | Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 216 | Parameters.Add(new ConstrainedValueParameter<IMultiMoveGenerator>(MoveGeneratorName, "The operator used to generate moves to the neighborhood of the current solution."));
|
---|
| 217 | Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>(MoveEvaluatorName, "The operator used to evaluate a move."));
|
---|
| 218 | Parameters.Add(new ConstrainedValueParameter<IMoveMaker>(MoveMakerName, "The operator used to perform a move."));
|
---|
| 219 | Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>(AnnealingOperatorName, "The operator used to cool the temperature."));
|
---|
[14555] | 220 | Parameters.Add(new ConstrainedValueParameter<IReheatingOperator>(ReheatingOperatorName, "The operator used to reheat the temperature, if necessary."));
|
---|
[15315] | 221 | Parameters.Add(new ConstrainedValueParameter<ITemperatureInitializer>(TemperatureInitializerName, "The operator used to initialize the temperature."));
|
---|
[14452] | 222 | Parameters.Add(new ValueParameter<IntValue>(MaximumIterationsName, "The maximum number of generations which should be processed.", new IntValue(10000)));
|
---|
| 223 | Parameters.Add(new ValueParameter<DoubleValue>(LowerTemperatureName, "The lower bound for the temperature.", new DoubleValue(1e-6)));
|
---|
| 224 | Parameters.Add(new ValueParameter<MultiAnalyzer>(AnalyzerName, "The operator used to analyze each iteration.", new MultiAnalyzer()));
|
---|
[14406] | 225 |
|
---|
[14452] | 226 | var randomCreator = new RandomCreator();
|
---|
| 227 | var solutionsCreator = new SolutionsCreator();
|
---|
| 228 | var variableCreator = new VariableCreator();
|
---|
| 229 | var startTemperatureAssigner = new Assigner();
|
---|
| 230 | var endTemperatureAssigner = new Assigner();
|
---|
| 231 | var temperatureAssigner = new Assigner();
|
---|
| 232 | var resultsCollector = new ResultsCollector();
|
---|
| 233 | var mainLoop = new SimulatedAnnealingMainLoop();
|
---|
| 234 | OperatorGraph.InitialOperator = randomCreator;
|
---|
[14406] | 235 |
|
---|
[14452] | 236 | randomCreator.RandomParameter.ActualName = RandomName;
|
---|
| 237 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 238 | randomCreator.SeedParameter.Value = null;
|
---|
| 239 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 240 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 241 | randomCreator.Successor = solutionsCreator;
|
---|
[14406] | 242 |
|
---|
[14452] | 243 | solutionsCreator.NumberOfSolutions = new IntValue(1);
|
---|
| 244 | solutionsCreator.Successor = variableCreator;
|
---|
[14406] | 245 |
|
---|
[14452] | 246 | variableCreator.Name = "Initialize Variables";
|
---|
[15001] | 247 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(InitialTemperatureName, new DoubleValue(Double.PositiveInfinity)));
|
---|
[14452] | 248 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(EvaluatedMovesName, new IntValue()));
|
---|
| 249 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(IterationsName, new IntValue(0)));
|
---|
| 250 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(TemperatureStartIndexName, new IntValue(0)));
|
---|
| 251 | variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>(CoolingName, new BoolValue(true)));
|
---|
[15001] | 252 |
|
---|
[14452] | 253 | variableCreator.Successor = startTemperatureAssigner;
|
---|
[14406] | 254 |
|
---|
[14452] | 255 | startTemperatureAssigner.Name = "Assign Start Temperature";
|
---|
| 256 | startTemperatureAssigner.LeftSideParameter.ActualName = StartTemperatureName;
|
---|
[15001] | 257 | startTemperatureAssigner.RightSideParameter.ActualName = InitialTemperatureName;
|
---|
| 258 | startTemperatureAssigner.Successor = endTemperatureAssigner;
|
---|
[14406] | 259 |
|
---|
[14452] | 260 | endTemperatureAssigner.Name = "Assign End Temperature";
|
---|
| 261 | endTemperatureAssigner.LeftSideParameter.ActualName = EndTemperatureName;
|
---|
| 262 | endTemperatureAssigner.RightSideParameter.ActualName = LowerTemperatureParameter.Name;
|
---|
[15001] | 263 | endTemperatureAssigner.Successor = temperatureAssigner;
|
---|
[14406] | 264 |
|
---|
[14452] | 265 | temperatureAssigner.Name = "Initialize Temperature";
|
---|
| 266 | temperatureAssigner.LeftSideParameter.ActualName = TemperatureName;
|
---|
| 267 | temperatureAssigner.RightSideParameter.ActualName = StartTemperatureName;
|
---|
[15001] | 268 | temperatureAssigner.Successor = resultsCollector;
|
---|
[14406] | 269 |
|
---|
[14452] | 270 | resultsCollector.CopyValue = new BoolValue(false);
|
---|
| 271 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(EvaluatedMovesName, null));
|
---|
| 272 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(IterationsName, null));
|
---|
| 273 | resultsCollector.ResultsParameter.ActualName = ResultsName;
|
---|
| 274 | resultsCollector.Successor = mainLoop;
|
---|
[14406] | 275 |
|
---|
[14452] | 276 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 277 | mainLoop.AnnealingOperatorParameter.ActualName = AnnealingOperatorParameter.Name;
|
---|
| 278 | mainLoop.CoolingParameter.ActualName = CoolingName;
|
---|
| 279 | mainLoop.EndTemperatureParameter.ActualName = EndTemperatureName;
|
---|
| 280 | mainLoop.EvaluatedMovesParameter.ActualName = EvaluatedMovesName;
|
---|
| 281 |
|
---|
| 282 | mainLoop.IterationsParameter.ActualName = IterationsName;
|
---|
| 283 | mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 284 | mainLoop.MoveEvaluatorParameter.ActualName = MoveEvaluatorParameter.Name;
|
---|
| 285 | mainLoop.MoveGeneratorParameter.ActualName = MoveGeneratorParameter.Name;
|
---|
| 286 | mainLoop.MoveMakerParameter.ActualName = MoveMakerParameter.Name;
|
---|
| 287 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 288 | mainLoop.ResultsParameter.ActualName = ResultsName;
|
---|
| 289 | mainLoop.StartTemperatureParameter.ActualName = StartTemperatureName;
|
---|
| 290 | mainLoop.TemperatureStartIndexParameter.ActualName = TemperatureStartIndexName;
|
---|
| 291 | mainLoop.TemperatureParameter.ActualName = TemperatureName;
|
---|
| 292 |
|
---|
[14555] | 293 | foreach (var op in ApplicationManager.Manager.GetInstances<IReheatingOperator>().OrderBy(x => x.Name))
|
---|
[14452] | 294 | {
|
---|
[14555] | 295 |
|
---|
| 296 | ReheatingOperatorParameter.ValidValues.Add(op);
|
---|
[14452] | 297 | }
|
---|
| 298 |
|
---|
[14555] | 299 | foreach (var op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
|
---|
[14452] | 300 | {
|
---|
[14555] | 301 | AnnealingOperatorParameter.ValidValues.Add(op);
|
---|
[14452] | 302 | }
|
---|
| 303 |
|
---|
[15315] | 304 | foreach(var op in ApplicationManager.Manager.GetInstances<ITemperatureInitializer>().OrderBy(x => x.Name))
|
---|
| 305 | {
|
---|
| 306 | TemperatureInitializerParameter.ValidValues.Add(op);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
[14452] | 309 | UpdateAnalyzers();
|
---|
| 310 |
|
---|
| 311 | Parameterize();
|
---|
| 312 |
|
---|
| 313 | RegisterEventHandlers();
|
---|
[14406] | 314 | }
|
---|
| 315 |
|
---|
[14452] | 316 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
| 317 | {
|
---|
| 318 | return new SimulatedAnnealing(this, cloner);
|
---|
[14406] | 319 | }
|
---|
| 320 |
|
---|
[14452] | 321 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 322 | private void AfterDeserialization()
|
---|
| 323 | {
|
---|
| 324 | RegisterEventHandlers();
|
---|
| 325 | }
|
---|
[14406] | 326 |
|
---|
[14452] | 327 | private void RegisterEventHandlers()
|
---|
| 328 | {
|
---|
| 329 | if (Problem != null)
|
---|
| 330 | {
|
---|
| 331 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
| 332 | foreach (var op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
| 333 | {
|
---|
| 334 | op.MoveQualityParameter.ActualNameChanged += MoveEvaluator_MoveQualityParameter_ActualNameChanged;
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 | MoveGeneratorParameter.ValueChanged += MoveGeneratorParameter_ValueChanged;
|
---|
| 338 | MoveEvaluatorParameter.ValueChanged += MoveEvaluatorParameter_ValueChanged;
|
---|
[14406] | 339 | }
|
---|
| 340 |
|
---|
[14452] | 341 | public override void Prepare()
|
---|
| 342 | {
|
---|
| 343 | if (Problem != null && MoveGenerator != null && MoveMaker != null && MoveEvaluator != null)
|
---|
| 344 | base.Prepare();
|
---|
[14406] | 345 | }
|
---|
[14452] | 346 |
|
---|
| 347 | #region Events
|
---|
| 348 | protected override void OnProblemChanged()
|
---|
| 349 | {
|
---|
| 350 | foreach (var op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
| 351 | {
|
---|
| 352 | op.MoveQualityParameter.ActualNameChanged += MoveEvaluator_MoveQualityParameter_ActualNameChanged;
|
---|
| 353 | }
|
---|
| 354 | UpdateMoveGenerator();
|
---|
| 355 | UpdateMoveOperators();
|
---|
| 356 | UpdateAnalyzers();
|
---|
| 357 | Parameterize();
|
---|
| 358 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
| 359 | base.OnProblemChanged();
|
---|
[14406] | 360 | }
|
---|
[14452] | 361 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e)
|
---|
| 362 | {
|
---|
| 363 | Parameterize();
|
---|
| 364 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
[14406] | 365 | }
|
---|
[14452] | 366 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e)
|
---|
| 367 | {
|
---|
| 368 | Parameterize();
|
---|
| 369 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 370 | base.Problem_EvaluatorChanged(sender, e);
|
---|
[14406] | 371 | }
|
---|
[14452] | 372 | protected override void Problem_OperatorsChanged(object sender, EventArgs e)
|
---|
| 373 | {
|
---|
| 374 | foreach (var op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
| 375 | {
|
---|
| 376 | op.MoveQualityParameter.ActualNameChanged -= MoveEvaluator_MoveQualityParameter_ActualNameChanged;
|
---|
| 377 | op.MoveQualityParameter.ActualNameChanged += MoveEvaluator_MoveQualityParameter_ActualNameChanged;
|
---|
| 378 | }
|
---|
| 379 | UpdateMoveGenerator();
|
---|
| 380 | UpdateMoveOperators();
|
---|
| 381 | UpdateAnalyzers();
|
---|
| 382 | Parameterize();
|
---|
| 383 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 384 | }
|
---|
| 385 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e)
|
---|
| 386 | {
|
---|
| 387 | Parameterize();
|
---|
| 388 | }
|
---|
| 389 | private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e)
|
---|
| 390 | {
|
---|
| 391 | UpdateMoveOperators();
|
---|
| 392 | }
|
---|
| 393 | private void MoveEvaluatorParameter_ValueChanged(object sender, EventArgs e)
|
---|
| 394 | {
|
---|
| 395 | Parameterize();
|
---|
| 396 | }
|
---|
| 397 | private void MoveEvaluator_MoveQualityParameter_ActualNameChanged(object sender, EventArgs e)
|
---|
| 398 | {
|
---|
| 399 | if (sender == MoveEvaluator) Parameterize();
|
---|
| 400 | }
|
---|
| 401 | #endregion
|
---|
| 402 |
|
---|
| 403 | #region Helpers
|
---|
| 404 |
|
---|
| 405 | private void UpdateMoveGenerator()
|
---|
| 406 | {
|
---|
| 407 | var oldMoveGenerator = MoveGenerator;
|
---|
| 408 | var defaultMoveGenerator = Problem.Operators.OfType<IMultiMoveGenerator>().FirstOrDefault();
|
---|
| 409 |
|
---|
| 410 | MoveGeneratorParameter.ValidValues.Clear();
|
---|
| 411 |
|
---|
| 412 | if (Problem != null)
|
---|
| 413 | {
|
---|
| 414 | foreach (var generator in Problem.Operators.OfType<IMultiMoveGenerator>().OrderBy(x => x.Name))
|
---|
| 415 | MoveGeneratorParameter.ValidValues.Add(generator);
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | if (oldMoveGenerator != null)
|
---|
| 419 | {
|
---|
| 420 | var newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
|
---|
| 421 | if (newMoveGenerator != null) MoveGenerator = newMoveGenerator;
|
---|
| 422 | else oldMoveGenerator = null;
|
---|
| 423 | }
|
---|
| 424 | if (oldMoveGenerator == null && defaultMoveGenerator != null)
|
---|
| 425 | MoveGenerator = defaultMoveGenerator;
|
---|
| 426 |
|
---|
| 427 | UpdateMoveOperators();
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | private void UpdateMoveOperators()
|
---|
| 431 | {
|
---|
| 432 | var oldMoveMaker = MoveMaker;
|
---|
| 433 | var oldMoveEvaluator = MoveEvaluator;
|
---|
| 434 |
|
---|
| 435 | MoveMakerParameter.ValidValues.Clear();
|
---|
| 436 | MoveEvaluatorParameter.ValidValues.Clear();
|
---|
| 437 |
|
---|
| 438 | if (MoveGenerator != null)
|
---|
| 439 | {
|
---|
| 440 | var moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
|
---|
| 441 | foreach (var type in moveTypes.ToList())
|
---|
| 442 | {
|
---|
| 443 | if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
|
---|
| 444 | moveTypes.Remove(type);
|
---|
| 445 | }
|
---|
| 446 | var operators = Problem.Operators.Where(op => moveTypes.Any(m => m.IsInstanceOfType(op))).ToList();
|
---|
| 447 | var defaultMoveMaker = operators.OfType<IMoveMaker>().FirstOrDefault();
|
---|
| 448 | var defaultMoveEvaluator = operators.OfType<ISingleObjectiveMoveEvaluator>().FirstOrDefault();
|
---|
| 449 |
|
---|
| 450 | foreach (var moveMaker in operators.OfType<IMoveMaker>().OrderBy(op => op.Name))
|
---|
| 451 | MoveMakerParameter.ValidValues.Add(moveMaker);
|
---|
| 452 |
|
---|
| 453 | foreach (var moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>().OrderBy(op => op.Name))
|
---|
| 454 | MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
|
---|
| 455 |
|
---|
| 456 | if (oldMoveMaker != null)
|
---|
| 457 | {
|
---|
| 458 | var mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
|
---|
| 459 | if (mm != null) MoveMaker = mm;
|
---|
| 460 | else oldMoveMaker = null;
|
---|
| 461 | }
|
---|
| 462 | if (oldMoveMaker == null && defaultMoveMaker != null)
|
---|
| 463 | MoveMaker = defaultMoveMaker;
|
---|
| 464 |
|
---|
| 465 | if (oldMoveEvaluator != null)
|
---|
| 466 | {
|
---|
| 467 | var me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
|
---|
| 468 | if (me != null) MoveEvaluator = me;
|
---|
| 469 | else oldMoveEvaluator = null;
|
---|
| 470 | }
|
---|
| 471 | if (oldMoveEvaluator == null & defaultMoveEvaluator != null)
|
---|
| 472 | MoveEvaluator = defaultMoveEvaluator;
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | private void UpdateAnalyzers()
|
---|
| 477 | {
|
---|
| 478 | Analyzer.Operators.Clear();
|
---|
| 479 | if (Problem != null)
|
---|
| 480 | {
|
---|
| 481 | foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>())
|
---|
| 482 | {
|
---|
| 483 | foreach (var param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 484 | param.Depth = 0;
|
---|
| 485 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 | var qualityAnalyzer = new QualityAnalyzer();
|
---|
| 489 | var temperatureAnalyzer = new SingleValueAnalyzer { Name = TemperatureAnalyzerName };
|
---|
| 490 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
| 491 | Analyzer.Operators.Add(temperatureAnalyzer, temperatureAnalyzer.EnabledByDefault);
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | private void Parameterize()
|
---|
| 495 | {
|
---|
| 496 |
|
---|
| 497 | #region IStochasticOperator
|
---|
| 498 | if (Problem != null)
|
---|
| 499 | {
|
---|
| 500 | foreach (var op in Problem.Operators.OfType<IStochasticOperator>())
|
---|
| 501 | {
|
---|
| 502 | op.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 503 | op.RandomParameter.Hidden = true;
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
| 506 | #endregion
|
---|
| 507 |
|
---|
| 508 | #region IIterationBasedOperator
|
---|
| 509 | if (Problem != null)
|
---|
| 510 | {
|
---|
| 511 | foreach (var op in Problem.Operators.OfType<IIterationBasedOperator>())
|
---|
| 512 | {
|
---|
| 513 | op.IterationsParameter.ActualName = IterationsName;
|
---|
| 514 | op.IterationsParameter.Hidden = true;
|
---|
| 515 | op.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 516 | op.MaximumIterationsParameter.Hidden = true;
|
---|
| 517 | }
|
---|
| 518 | }
|
---|
| 519 | #endregion
|
---|
| 520 |
|
---|
| 521 | #region Analyzers
|
---|
| 522 |
|
---|
| 523 | foreach (var qualityAnalyzer in Analyzer.Operators.OfType<QualityAnalyzer>())
|
---|
| 524 | {
|
---|
| 525 | qualityAnalyzer.ResultsParameter.ActualName = ResultsName;
|
---|
| 526 | if (Problem != null)
|
---|
| 527 | {
|
---|
| 528 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 529 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
| 530 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 531 | qualityAnalyzer.QualityParameter.Depth = 0;
|
---|
| 532 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
| 533 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 534 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
| 535 | }
|
---|
| 536 | else
|
---|
| 537 | {
|
---|
| 538 | qualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
| 539 | qualityAnalyzer.QualityParameter.Hidden = false;
|
---|
| 540 | qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
| 541 | }
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | var temperatureAnalyzer = Analyzer.Operators.OfType<SingleValueAnalyzer>().FirstOrDefault(x => x.Name == TemperatureAnalyzerName);
|
---|
| 545 | if (temperatureAnalyzer != null)
|
---|
| 546 | {
|
---|
| 547 | temperatureAnalyzer.ResultsParameter.ActualName = ResultsName;
|
---|
| 548 | temperatureAnalyzer.ResultsParameter.Hidden = true;
|
---|
| 549 | temperatureAnalyzer.ValueParameter.ActualName = TemperatureName;
|
---|
| 550 | temperatureAnalyzer.ValueParameter.Hidden = true;
|
---|
| 551 | temperatureAnalyzer.ValuesParameter.ActualName = TemperatureChartName;
|
---|
| 552 | temperatureAnalyzer.ValuesParameter.Hidden = true;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | #endregion
|
---|
| 556 |
|
---|
| 557 | #region SolutionCreator
|
---|
| 558 | if (Problem != null)
|
---|
| 559 | {
|
---|
| 560 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 561 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 562 | }
|
---|
| 563 | #endregion
|
---|
| 564 |
|
---|
| 565 | #region Annealing/Reheating Operators
|
---|
| 566 | foreach (var op in AnnealingOperatorParameter.ValidValues)
|
---|
| 567 | {
|
---|
| 568 | op.IndexParameter.ActualName = IterationsName;
|
---|
| 569 | op.IndexParameter.Hidden = true;
|
---|
| 570 | op.StartIndexParameter.Value = null;
|
---|
| 571 | op.StartIndexParameter.ActualName = TemperatureStartIndexName;
|
---|
| 572 | op.EndIndexParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
| 573 | op.ValueParameter.ActualName = TemperatureName;
|
---|
| 574 | op.ValueParameter.Hidden = true;
|
---|
| 575 | op.StartValueParameter.ActualName = StartTemperatureName;
|
---|
| 576 | op.StartValueParameter.Hidden = true;
|
---|
| 577 | op.EndValueParameter.ActualName = LowerTemperatureParameter.Name;
|
---|
| 578 | op.EndValueParameter.Hidden = true;
|
---|
| 579 | }
|
---|
[14555] | 580 | foreach (var op in ReheatingOperatorParameter.ValidValues)
|
---|
[14452] | 581 | {
|
---|
| 582 | op.Parameterize();
|
---|
| 583 | }
|
---|
| 584 | #endregion
|
---|
| 585 |
|
---|
| 586 | #region Move Operators
|
---|
| 587 | if (Problem != null)
|
---|
| 588 | {
|
---|
| 589 | foreach (var op in Problem.Operators.OfType<IMultiMoveGenerator>())
|
---|
| 590 | {
|
---|
| 591 | op.SampleSizeParameter.Value = new IntValue(1);
|
---|
| 592 | op.SampleSizeParameter.Hidden = true;
|
---|
| 593 | }
|
---|
| 594 | foreach (var op in Problem.Operators.OfType<IMoveMaker>())
|
---|
| 595 | {
|
---|
| 596 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 597 | op.QualityParameter.Hidden = true;
|
---|
| 598 | if (MoveEvaluator != null)
|
---|
| 599 | {
|
---|
| 600 | op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
| 601 | op.MoveQualityParameter.Hidden = true;
|
---|
| 602 | }
|
---|
| 603 | else
|
---|
| 604 | {
|
---|
| 605 | op.MoveQualityParameter.Hidden = false;
|
---|
| 606 | }
|
---|
| 607 | }
|
---|
| 608 | foreach (var op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>())
|
---|
| 609 | {
|
---|
| 610 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 611 | op.QualityParameter.Hidden = true;
|
---|
| 612 | }
|
---|
| 613 | }
|
---|
| 614 | #endregion
|
---|
| 615 |
|
---|
| 616 | #region MainLoop
|
---|
| 617 | if (Problem != null)
|
---|
| 618 | {
|
---|
| 619 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 620 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 621 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 622 | if (MoveEvaluator != null)
|
---|
| 623 | MainLoop.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
|
---|
| 624 | }
|
---|
| 625 | #endregion
|
---|
| 626 |
|
---|
| 627 | }
|
---|
| 628 | #endregion
|
---|
[14406] | 629 | }
|
---|
| 630 | } |
---|