Changeset 15295
- Timestamp:
- 07/29/17 13:01:49 (7 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.3
- Files:
-
- 9 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/SimulatedAnnealing.cs
r14185 r15295 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 6Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using HeuristicLab.Analysis; … … 35 34 using HeuristicLab.Random; 36 35 37 namespace HeuristicLab.Algorithms.SimulatedAnnealing { 38 [Item("Simulated Annealing (SA)", "A simulated annealing algorithm.")] 39 [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 140)] 40 [StorableClass] 41 public sealed class SimulatedAnnealing : HeuristicOptimizationEngineAlgorithm, IStorableContent { 42 public string Filename { get; set; } 43 44 #region Problem Properties 45 public override Type ProblemType { 46 get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); } 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"; 50 private const string ReheatingOperatorName = "ReheatingOperator"; 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"; 66 private const string TemperatureInitializerName = "TemperatureInitializer"; 67 #endregion 68 69 public string Filename { get; set; } 70 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 82 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 } 108 109 public IConstrainedValueParameter<IReheatingOperator> ReheatingOperatorParameter 110 { 111 get { return (IConstrainedValueParameter<IReheatingOperator>)Parameters[ReheatingOperatorName]; } 112 } 113 public IConstrainedValueParameter<ITemperatureInitializer> TemperatureInitializerParameter 114 { 115 get { return (IConstrainedValueParameter<ITemperatureInitializer>)Parameters[TemperatureInitializerName]; } 116 } 117 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 131 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 } 163 164 public IReheatingOperator ReheatingOperator 165 { 166 get { return ReheatingOperatorParameter.Value; } 167 set { ReheatingOperatorParameter.Value = value; } 168 } 169 public ITemperatureInitializer TemperatureInitializer 170 { 171 get { return TemperatureInitializerParameter.Value; } 172 set { TemperatureInitializerParameter.Value = value; } 173 } 174 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 203 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.")); 220 Parameters.Add(new ConstrainedValueParameter<IReheatingOperator>(ReheatingOperatorName, "The operator used to reheat the temperature, if necessary.")); 221 Parameters.Add(new ConstrainedValueParameter<ITemperatureInitializer>(TemperatureInitializerName, "The operator used to initialize the temperature.")); 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())); 225 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; 235 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; 242 243 solutionsCreator.NumberOfSolutions = new IntValue(1); 244 solutionsCreator.Successor = variableCreator; 245 246 variableCreator.Name = "Initialize Variables"; 247 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(InitialTemperatureName, new DoubleValue(Double.PositiveInfinity))); 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))); 252 253 variableCreator.Successor = startTemperatureAssigner; 254 255 startTemperatureAssigner.Name = "Assign Start Temperature"; 256 startTemperatureAssigner.LeftSideParameter.ActualName = StartTemperatureName; 257 startTemperatureAssigner.RightSideParameter.ActualName = InitialTemperatureName; 258 startTemperatureAssigner.Successor = endTemperatureAssigner; 259 260 endTemperatureAssigner.Name = "Assign End Temperature"; 261 endTemperatureAssigner.LeftSideParameter.ActualName = EndTemperatureName; 262 endTemperatureAssigner.RightSideParameter.ActualName = LowerTemperatureParameter.Name; 263 endTemperatureAssigner.Successor = temperatureAssigner; 264 265 temperatureAssigner.Name = "Initialize Temperature"; 266 temperatureAssigner.LeftSideParameter.ActualName = TemperatureName; 267 temperatureAssigner.RightSideParameter.ActualName = StartTemperatureName; 268 temperatureAssigner.Successor = resultsCollector; 269 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; 275 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 293 foreach (var op in ApplicationManager.Manager.GetInstances<IReheatingOperator>().OrderBy(x => x.Name)) 294 { 295 296 ReheatingOperatorParameter.ValidValues.Add(op); 297 } 298 299 foreach (var op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name)) 300 { 301 AnnealingOperatorParameter.ValidValues.Add(op); 302 } 303 304 foreach(var op in ApplicationManager.Manager.GetInstances<ITemperatureInitializer>().OrderBy(x => x.Name)) 305 { 306 TemperatureInitializerParameter.ValidValues.Add(op); 307 } 308 309 UpdateAnalyzers(); 310 311 Parameterize(); 312 313 RegisterEventHandlers(); 314 } 315 316 public override IDeepCloneable Clone(Cloner cloner) 317 { 318 return new SimulatedAnnealing(this, cloner); 319 } 320 321 [StorableHook(HookType.AfterDeserialization)] 322 private void AfterDeserialization() 323 { 324 RegisterEventHandlers(); 325 } 326 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; 339 } 340 341 public override void Prepare() 342 { 343 if (Problem != null && MoveGenerator != null && MoveMaker != null && MoveEvaluator != null) 344 base.Prepare(); 345 } 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(); 360 } 361 protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) 362 { 363 Parameterize(); 364 base.Problem_SolutionCreatorChanged(sender, e); 365 } 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); 371 } 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 } 580 foreach (var op in ReheatingOperatorParameter.ValidValues) 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 47 629 } 48 public new ISingleObjectiveHeuristicOptimizationProblem Problem {49 get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }50 set { base.Problem = value; }51 }52 #endregion53 54 #region Parameter Properties55 private ValueParameter<IntValue> SeedParameter {56 get { return (ValueParameter<IntValue>)Parameters["Seed"]; }57 }58 private ValueParameter<BoolValue> SetSeedRandomlyParameter {59 get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }60 }61 public IConstrainedValueParameter<IMultiMoveGenerator> MoveGeneratorParameter {62 get { return (IConstrainedValueParameter<IMultiMoveGenerator>)Parameters["MoveGenerator"]; }63 }64 public IConstrainedValueParameter<IMoveMaker> MoveMakerParameter {65 get { return (IConstrainedValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }66 }67 public IConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {68 get { return (IConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }69 }70 public IConstrainedValueParameter<IDiscreteDoubleValueModifier> AnnealingOperatorParameter {71 get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["AnnealingOperator"]; }72 }73 private ValueParameter<IntValue> MaximumIterationsParameter {74 get { return (ValueParameter<IntValue>)Parameters["MaximumIterations"]; }75 }76 private ValueParameter<IntValue> InnerIterationsParameter {77 get { return (ValueParameter<IntValue>)Parameters["InnerIterations"]; }78 }79 private ValueParameter<DoubleValue> StartTemperatureParameter {80 get { return (ValueParameter<DoubleValue>)Parameters["StartTemperature"]; }81 }82 private ValueParameter<DoubleValue> EndTemperatureParameter {83 get { return (ValueParameter<DoubleValue>)Parameters["EndTemperature"]; }84 }85 private ValueParameter<MultiAnalyzer> AnalyzerParameter {86 get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }87 }88 #endregion89 90 #region Properties91 public IntValue Seed {92 get { return SeedParameter.Value; }93 set { SeedParameter.Value = value; }94 }95 public BoolValue SetSeedRandomly {96 get { return SetSeedRandomlyParameter.Value; }97 set { SetSeedRandomlyParameter.Value = value; }98 }99 public IMultiMoveGenerator MoveGenerator {100 get { return MoveGeneratorParameter.Value; }101 set { MoveGeneratorParameter.Value = value; }102 }103 public IMoveMaker MoveMaker {104 get { return MoveMakerParameter.Value; }105 set { MoveMakerParameter.Value = value; }106 }107 public ISingleObjectiveMoveEvaluator MoveEvaluator {108 get { return MoveEvaluatorParameter.Value; }109 set { MoveEvaluatorParameter.Value = value; }110 }111 public IDiscreteDoubleValueModifier AnnealingOperator {112 get { return AnnealingOperatorParameter.Value; }113 set { AnnealingOperatorParameter.Value = value; }114 }115 public IntValue MaximumIterations {116 get { return MaximumIterationsParameter.Value; }117 set { MaximumIterationsParameter.Value = value; }118 }119 public IntValue InnerIterations {120 get { return InnerIterationsParameter.Value; }121 set { InnerIterationsParameter.Value = value; }122 }123 public DoubleValue StartTemperature {124 get { return StartTemperatureParameter.Value; }125 set { StartTemperatureParameter.Value = value; }126 }127 public DoubleValue EndTemperature {128 get { return EndTemperatureParameter.Value; }129 set { EndTemperatureParameter.Value = value; }130 }131 public MultiAnalyzer Analyzer {132 get { return AnalyzerParameter.Value; }133 set { AnalyzerParameter.Value = value; }134 }135 private RandomCreator RandomCreator {136 get { return (RandomCreator)OperatorGraph.InitialOperator; }137 }138 private SolutionsCreator SolutionsCreator {139 get { return (SolutionsCreator)RandomCreator.Successor; }140 }141 private SimulatedAnnealingMainLoop MainLoop {142 get { return FindMainLoop(SolutionsCreator.Successor); }143 }144 [Storable]145 private QualityAnalyzer qualityAnalyzer;146 [Storable]147 private SingleValueAnalyzer temperatureAnalyzer;148 #endregion149 150 [StorableConstructor]151 private SimulatedAnnealing(bool deserializing) : base(deserializing) { }152 [StorableHook(HookType.AfterDeserialization)]153 private void AfterDeserialization() {154 if (temperatureAnalyzer == null) {155 temperatureAnalyzer = new SingleValueAnalyzer();156 temperatureAnalyzer.Name = "TemperatureAnalyzer";157 temperatureAnalyzer.ResultsParameter.ActualName = "Results";158 temperatureAnalyzer.ValueParameter.ActualName = "Temperature";159 temperatureAnalyzer.ValuesParameter.ActualName = "Temperature Chart";160 Analyzer.Operators.Add(temperatureAnalyzer);161 }162 Initialize();163 }164 private SimulatedAnnealing(SimulatedAnnealing original, Cloner cloner)165 : base(original, cloner) {166 qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);167 temperatureAnalyzer = cloner.Clone(original.temperatureAnalyzer);168 Initialize();169 }170 public override IDeepCloneable Clone(Cloner cloner) {171 return new SimulatedAnnealing(this, cloner);172 }173 public SimulatedAnnealing()174 : base() {175 Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));176 Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));177 Parameters.Add(new ConstrainedValueParameter<IMultiMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));178 Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));179 Parameters.Add(new ConstrainedValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));180 Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>("AnnealingOperator", "The operator used to modify the temperature."));181 Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(100)));182 Parameters.Add(new ValueParameter<IntValue>("InnerIterations", "The amount of inner iterations (number of moves before temperature is adjusted again).", new IntValue(10)));183 Parameters.Add(new ValueParameter<DoubleValue>("StartTemperature", "The initial temperature.", new DoubleValue(100)));184 Parameters.Add(new ValueParameter<DoubleValue>("EndTemperature", "The final temperature which should be reached when iterations reaches maximum iterations.", new DoubleValue(1e-6)));185 Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));186 187 RandomCreator randomCreator = new RandomCreator();188 SolutionsCreator solutionsCreator = new SolutionsCreator();189 VariableCreator variableCreator = new VariableCreator();190 ResultsCollector resultsCollector = new ResultsCollector();191 SimulatedAnnealingMainLoop mainLoop = new SimulatedAnnealingMainLoop();192 OperatorGraph.InitialOperator = randomCreator;193 194 randomCreator.RandomParameter.ActualName = "Random";195 randomCreator.SeedParameter.ActualName = SeedParameter.Name;196 randomCreator.SeedParameter.Value = null;197 randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;198 randomCreator.SetSeedRandomlyParameter.Value = null;199 randomCreator.Successor = solutionsCreator;200 201 solutionsCreator.NumberOfSolutions = new IntValue(1);202 solutionsCreator.Successor = variableCreator;203 204 variableCreator.Name = "Initialize EvaluatedMoves";205 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue()));206 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));207 variableCreator.Successor = resultsCollector;208 209 resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));210 resultsCollector.ResultsParameter.ActualName = "Results";211 resultsCollector.Successor = mainLoop;212 213 mainLoop.MoveGeneratorParameter.ActualName = MoveGeneratorParameter.Name;214 mainLoop.MoveEvaluatorParameter.ActualName = MoveEvaluatorParameter.Name;215 mainLoop.MoveMakerParameter.ActualName = MoveMakerParameter.Name;216 mainLoop.AnnealingOperatorParameter.ActualName = AnnealingOperatorParameter.Name;217 mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;218 mainLoop.TemperatureParameter.ActualName = "Temperature";219 mainLoop.StartTemperatureParameter.ActualName = StartTemperatureParameter.Name;220 mainLoop.EndTemperatureParameter.ActualName = EndTemperatureParameter.Name;221 mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;222 mainLoop.ResultsParameter.ActualName = "Results";223 mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;224 mainLoop.EvaluatedMovesParameter.ActualName = "EvaluatedMoves";225 mainLoop.IterationsParameter.ActualName = "Iterations";226 227 foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))228 AnnealingOperatorParameter.ValidValues.Add(op);229 ParameterizeAnnealingOperators();230 231 qualityAnalyzer = new QualityAnalyzer();232 temperatureAnalyzer = new SingleValueAnalyzer();233 temperatureAnalyzer.Name = "TemperatureAnalyzer";234 ParameterizeAnalyzers();235 UpdateAnalyzers();236 237 Initialize();238 }239 240 public override void Prepare() {241 if (Problem != null && MoveGenerator != null && MoveMaker != null && MoveEvaluator != null)242 base.Prepare();243 }244 245 #region Events246 protected override void OnProblemChanged() {247 ParameterizeStochasticOperator(Problem.SolutionCreator);248 ParameterizeStochasticOperator(Problem.Evaluator);249 foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);250 foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {251 op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);252 }253 ParameterizeSolutionsCreator();254 ParameterizeMainLoop();255 UpdateMoveGenerator();256 UpdateMoveParameters();257 UpdateAnalyzers();258 ParameterizeMoveEvaluators();259 ParameterizeMoveMakers();260 ParameterizeMoveGenerators();261 ParameterizeAnalyzers();262 ParameterizeIterationBasedOperators();263 Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);264 base.OnProblemChanged();265 }266 protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {267 ParameterizeStochasticOperator(Problem.SolutionCreator);268 ParameterizeSolutionsCreator();269 base.Problem_SolutionCreatorChanged(sender, e);270 }271 protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {272 ParameterizeStochasticOperator(Problem.Evaluator);273 ParameterizeSolutionsCreator();274 ParameterizeMainLoop();275 ParameterizeMoveEvaluators();276 ParameterizeMoveMakers();277 ParameterizeAnalyzers();278 Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);279 base.Problem_EvaluatorChanged(sender, e);280 }281 protected override void Problem_OperatorsChanged(object sender, EventArgs e) {282 foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);283 // This may seem pointless, but some operators already have the eventhandler registered, others don't284 // FIXME: Is there another way to solve this problem?285 foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {286 op.MoveQualityParameter.ActualNameChanged -= new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);287 op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);288 }289 UpdateMoveGenerator();290 UpdateMoveParameters();291 UpdateAnalyzers();292 ParameterizeMainLoop();293 ParameterizeMoveEvaluators();294 ParameterizeMoveMakers();295 ParameterizeMoveGenerators();296 ParameterizeIterationBasedOperators();297 base.Problem_OperatorsChanged(sender, e);298 }299 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {300 ParameterizeMainLoop();301 ParameterizeMoveEvaluators();302 ParameterizeMoveMakers();303 ParameterizeAnalyzers();304 }305 private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {306 UpdateMoveParameters();307 }308 private void MoveEvaluatorParameter_ValueChanged(object sender, EventArgs e) {309 ParameterizeMainLoop();310 ParameterizeMoveEvaluators();311 ParameterizeMoveMakers();312 }313 private void MoveEvaluator_MoveQualityParameter_ActualNameChanged(object sender, EventArgs e) {314 ParameterizeMainLoop();315 ParameterizeMoveEvaluators();316 ParameterizeMoveMakers();317 }318 #endregion319 320 #region Helpers321 private void Initialize() {322 if (Problem != null) {323 Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);324 foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {325 op.MoveQualityParameter.ActualNameChanged += new EventHandler(MoveEvaluator_MoveQualityParameter_ActualNameChanged);326 }327 }328 MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged);329 MoveEvaluatorParameter.ValueChanged += new EventHandler(MoveEvaluatorParameter_ValueChanged);330 }331 private void UpdateMoveGenerator() {332 IMultiMoveGenerator oldMoveGenerator = MoveGenerator;333 IMultiMoveGenerator defaultMoveGenerator = Problem.Operators.OfType<IMultiMoveGenerator>().FirstOrDefault();334 MoveGeneratorParameter.ValidValues.Clear();335 if (Problem != null) {336 foreach (IMultiMoveGenerator generator in Problem.Operators.OfType<IMultiMoveGenerator>().OrderBy(x => x.Name))337 MoveGeneratorParameter.ValidValues.Add(generator);338 }339 if (oldMoveGenerator != null) {340 IMultiMoveGenerator newMoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());341 if (newMoveGenerator != null) MoveGenerator = newMoveGenerator;342 else oldMoveGenerator = null;343 }344 if (oldMoveGenerator == null && defaultMoveGenerator != null)345 MoveGenerator = defaultMoveGenerator;346 if (MoveGenerator == null) {347 ClearMoveParameters();348 }349 }350 private void ParameterizeAnalyzers() {351 qualityAnalyzer.ResultsParameter.ActualName = "Results";352 if (Problem != null) {353 qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;354 qualityAnalyzer.MaximizationParameter.Hidden = true;355 qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;356 qualityAnalyzer.QualityParameter.Depth = 0;357 qualityAnalyzer.QualityParameter.Hidden = true;358 qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;359 qualityAnalyzer.BestKnownQualityParameter.Hidden = true;360 } else {361 qualityAnalyzer.MaximizationParameter.Hidden = false;362 qualityAnalyzer.QualityParameter.Hidden = false;363 qualityAnalyzer.BestKnownQualityParameter.Hidden = false;364 temperatureAnalyzer.ResultsParameter.ActualName = "Results";365 temperatureAnalyzer.ValueParameter.ActualName = "Temperature";366 temperatureAnalyzer.ValuesParameter.ActualName = "Temperature Chart";367 }368 }369 private void UpdateMoveParameters() {370 IMoveMaker oldMoveMaker = MoveMaker;371 ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;372 ClearMoveParameters();373 374 if (MoveGenerator != null) {375 List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();376 foreach (Type type in moveTypes.ToList()) {377 if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))378 moveTypes.Remove(type);379 }380 var operators = Problem.Operators.Where(op => moveTypes.Any(m => m.IsInstanceOfType(op))).ToList();381 IMoveMaker defaultMoveMaker = operators.OfType<IMoveMaker>().FirstOrDefault();382 ISingleObjectiveMoveEvaluator defaultMoveEvaluator = operators.OfType<ISingleObjectiveMoveEvaluator>().FirstOrDefault();383 384 foreach (IMoveMaker moveMaker in operators.OfType<IMoveMaker>().OrderBy(op => op.Name))385 MoveMakerParameter.ValidValues.Add(moveMaker);386 foreach (ISingleObjectiveMoveEvaluator moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>().OrderBy(op => op.Name))387 MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);388 389 if (oldMoveMaker != null) {390 IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());391 if (mm != null) MoveMaker = mm;392 else oldMoveMaker = null;393 }394 if (oldMoveMaker == null && defaultMoveMaker != null)395 MoveMaker = defaultMoveMaker;396 397 if (oldMoveEvaluator != null) {398 ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());399 if (me != null) MoveEvaluator = me;400 else oldMoveEvaluator = null;401 }402 if (oldMoveEvaluator == null & defaultMoveEvaluator != null)403 MoveEvaluator = defaultMoveEvaluator;404 }405 }406 private void ClearMoveParameters() {407 MoveMakerParameter.ValidValues.Clear();408 MoveEvaluatorParameter.ValidValues.Clear();409 }410 private void ParameterizeSolutionsCreator() {411 SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;412 SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;413 }414 private void ParameterizeMainLoop() {415 if (Problem != null) {416 MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;417 MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;418 MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;419 }420 if (MoveEvaluator != null)421 MainLoop.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;422 }423 private void ParameterizeStochasticOperator(IOperator op) {424 if (op is IStochasticOperator) {425 IStochasticOperator stOp = (IStochasticOperator)op;426 stOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;427 stOp.RandomParameter.Hidden = true;428 }429 }430 private void ParameterizeMoveEvaluators() {431 foreach (ISingleObjectiveMoveEvaluator op in Problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {432 op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;433 op.QualityParameter.Hidden = true;434 }435 }436 private void ParameterizeMoveMakers() {437 foreach (IMoveMaker op in Problem.Operators.OfType<IMoveMaker>()) {438 op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;439 op.QualityParameter.Hidden = true;440 if (MoveEvaluator != null) {441 op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;442 op.MoveQualityParameter.Hidden = true;443 } else {444 op.MoveQualityParameter.Hidden = false;445 }446 }447 }448 private void ParameterizeAnnealingOperators() {449 foreach (IDiscreteDoubleValueModifier op in AnnealingOperatorParameter.ValidValues) {450 op.IndexParameter.ActualName = "Iterations";451 op.IndexParameter.Hidden = true;452 op.StartIndexParameter.Value = new IntValue(0);453 op.EndIndexParameter.ActualName = MaximumIterationsParameter.Name;454 op.ValueParameter.ActualName = "Temperature";455 op.ValueParameter.Hidden = true;456 op.StartValueParameter.ActualName = StartTemperatureParameter.Name;457 op.StartValueParameter.Hidden = true;458 op.EndValueParameter.ActualName = EndTemperatureParameter.Name;459 op.EndValueParameter.Hidden = true;460 ParameterizeStochasticOperator(op);461 }462 }463 private void ParameterizeMoveGenerators() {464 foreach (IMultiMoveGenerator op in Problem.Operators.OfType<IMultiMoveGenerator>()) {465 op.SampleSizeParameter.ActualName = InnerIterationsParameter.Name;466 op.SampleSizeParameter.Hidden = true;467 }468 }469 private void ParameterizeIterationBasedOperators() {470 if (Problem != null) {471 foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {472 op.IterationsParameter.ActualName = "Iterations";473 op.IterationsParameter.Hidden = true;474 op.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;475 op.MaximumIterationsParameter.Hidden = true;476 }477 }478 }479 private void UpdateAnalyzers() {480 Analyzer.Operators.Clear();481 if (Problem != null) {482 foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {483 foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())484 param.Depth = 0;485 Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);486 }487 }488 Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);489 Analyzer.Operators.Add(temperatureAnalyzer, temperatureAnalyzer.EnabledByDefault);490 }491 private SimulatedAnnealingMainLoop FindMainLoop(IOperator start) {492 IOperator mainLoop = start;493 while (mainLoop != null && !(mainLoop is SimulatedAnnealingMainLoop))494 mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;495 if (mainLoop == null) return null;496 else return (SimulatedAnnealingMainLoop)mainLoop;497 }498 #endregion499 }500 630 } -
trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/SimulatedAnnealingImprovementOperator.cs
r14185 r15295 377 377 VariableCreator variableCreator = new VariableCreator(); 378 378 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(loop.IterationsParameter.ActualName, new IntValue(0))); 379 379 380 380 variableCreator.Successor = loop; 381 381 -
trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/SimulatedAnnealingMainLoop.cs
r14185 r15295 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 6Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 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"]; } 30 namespace HeuristicLab.Algorithms.SimulatedAnnealing 31 { 32 [Item("SimulatedAnnealingMainLoop", "An operator which represents the main loop of a simulated annealing algorithm.")] 33 [StorableClass] 34 public sealed class SimulatedAnnealingMainLoop : AlgorithmOperator 35 { 36 #region Strings 37 private const string MoveGeneratorName = "MoveGenerator"; 38 private const string MoveEvaluatorName = "MoveEvaluator"; 39 private const string MoveMakerName = "MoveMaker"; 40 private const string AnnealingOperatorName = "AnnealingOperator"; 41 private const string ReheatingOperatorName = "ReheatingOperator"; 42 private const string MaximumIterationsName = "MaximumIterations"; 43 private const string LowerTemperatureName = "LowerTemperature"; 44 private const string AnalyzerName = "Analyzer"; 45 private const string RandomName = "Random"; 46 private const string EvaluatedMovesName = "EvaluatedMoves"; 47 private const string IterationsName = "Iterations"; 48 private const string TemperatureStartIndexName = "TemperatureStartIndex"; 49 private const string CoolingName = "Cooling"; 50 private const string InitialTemperatureName = "InitialTemperature"; 51 private const string StartTemperatureName = "StartTemperature"; 52 private const string EndTemperatureName = "EndTemperature"; 53 private const string TemperatureName = "Temperature"; 54 private const string ResultsName = "Results"; 55 private const string MaximizationName = "Maximization"; 56 private const string QualityName = "Quality"; 57 private const string BestKnownQualityName = "BestKnownQuality"; 58 private const string MoveQualityName = "MoveQuality"; 59 private const string IsAcceptedName = "IsAccepted"; 60 private const string TerminateName = "Terminate"; 61 private const string AcceptanceMemoryName = "AcceptanceMemory"; 62 private const string ConsecutiveRejectedSolutionsCountName = "ConsecutiveRejectedSolutions"; 63 private const string AverageAcceptanceRatioName = "AverageAcceptanceRatio"; 64 private const string LastQualityName = "LastQuality"; 65 private const string UphillMovesMemoryName = "UphillMovesMemory"; 66 private const string TemperatureBeforeReheatName = "TemperatureBeforeReheat"; 67 private const string CurrentRandomWalkStepName = "CurrentRandomWalkStep"; 68 private const string QualitiesBeforeReheatingName = "QualitiesBeforeReheating"; 69 private const string LastAcceptedQualityName = "LastAcceptedQuality"; 70 private const string TemperatureInitializerName = "TemperatureInitializer"; 71 private const string TemperatureInitializedName = "TemperatureInitialized"; 72 73 #endregion 74 75 #region Parameter properties 76 public IValueLookupParameter<IRandom> RandomParameter 77 { 78 get { return (IValueLookupParameter<IRandom>)Parameters[RandomName]; } 79 } 80 public IValueLookupParameter<BoolValue> MaximizationParameter 81 { 82 get { return (IValueLookupParameter<BoolValue>)Parameters[MaximizationName]; } 83 } 84 public ILookupParameter<DoubleValue> QualityParameter 85 { 86 get { return (ILookupParameter<DoubleValue>)Parameters[QualityName]; } 87 } 88 public IValueLookupParameter<DoubleValue> BestKnownQualityParameter 89 { 90 get { return (IValueLookupParameter<DoubleValue>)Parameters[BestKnownQualityName]; } 91 } 92 public IValueLookupParameter<DoubleValue> LastQualityParameter 93 { 94 get { return (IValueLookupParameter<DoubleValue>)Parameters[LastQualityName]; } 95 } 96 public ILookupParameter<DoubleValue> InitialTemperatureParameter 97 { 98 get { return (ILookupParameter<DoubleValue>)Parameters[InitialTemperatureName]; } 99 } 100 public ILookupParameter<DoubleValue> MoveQualityParameter 101 { 102 get { return (ILookupParameter<DoubleValue>)Parameters[MoveQualityName]; } 103 } 104 public ILookupParameter<DoubleValue> TemperatureParameter 105 { 106 get { return (ILookupParameter<DoubleValue>)Parameters[TemperatureName]; } 107 } 108 public IValueLookupParameter<DoubleValue> LowerTemperatureParameter 109 { 110 get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerTemperatureName]; } 111 } 112 public ILookupParameter<IntValue> IterationsParameter 113 { 114 get { return (ILookupParameter<IntValue>)Parameters[IterationsName]; } 115 } 116 public IValueLookupParameter<IntValue> MaximumIterationsParameter 117 { 118 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsName]; } 119 } 120 public IValueLookupParameter<IOperator> MoveGeneratorParameter 121 { 122 get { return (IValueLookupParameter<IOperator>)Parameters[MoveGeneratorName]; } 123 } 124 public IValueLookupParameter<IOperator> MoveEvaluatorParameter 125 { 126 get { return (IValueLookupParameter<IOperator>)Parameters[MoveEvaluatorName]; } 127 } 128 public IValueLookupParameter<IOperator> MoveMakerParameter 129 { 130 get { return (IValueLookupParameter<IOperator>)Parameters[MoveMakerName]; } 131 } 132 public IValueLookupParameter<IOperator> AnnealingOperatorParameter 133 { 134 get { return (IValueLookupParameter<IOperator>)Parameters[AnnealingOperatorName]; } 135 } 136 public IValueLookupParameter<IOperator> ReheatingOperatorParameter 137 { 138 get { return (IValueLookupParameter<IOperator>)Parameters[ReheatingOperatorName]; } 139 } 140 public IValueLookupParameter<IOperator> AnalyzerParameter 141 { 142 get { return (IValueLookupParameter<IOperator>)Parameters[AnalyzerName]; } 143 } 144 public IValueLookupParameter<VariableCollection> ResultsParameter 145 { 146 get { return (IValueLookupParameter<VariableCollection>)Parameters[ResultsName]; } 147 } 148 public ILookupParameter<IntValue> EvaluatedMovesParameter 149 { 150 get { return (ILookupParameter<IntValue>)Parameters[EvaluatedMovesName]; } 151 } 152 public ILookupParameter<DoubleValue> StartTemperatureParameter 153 { 154 get { return (ILookupParameter<DoubleValue>)Parameters[StartTemperatureName]; } 155 } 156 public ILookupParameter<DoubleValue> EndTemperatureParameter 157 { 158 get { return (ILookupParameter<DoubleValue>)Parameters[EndTemperatureName]; } 159 } 160 public ILookupParameter<IntValue> TemperatureStartIndexParameter 161 { 162 get { return (ILookupParameter<IntValue>)Parameters[TemperatureStartIndexName]; } 163 } 164 public ILookupParameter<BoolValue> CoolingParameter 165 { 166 get { return (ILookupParameter<BoolValue>)Parameters[CoolingName]; } 167 } 168 #endregion 169 170 [StorableConstructor] 171 private SimulatedAnnealingMainLoop(bool deserializing) : base(deserializing) { } 172 private SimulatedAnnealingMainLoop(SimulatedAnnealingMainLoop original, Cloner cloner) 173 : base(original, cloner) 174 { 175 } 176 public override IDeepCloneable Clone(Cloner cloner) 177 { 178 return new SimulatedAnnealingMainLoop(this, cloner); 179 } 180 public SimulatedAnnealingMainLoop() 181 : base() 182 { 183 Initialize(); 184 } 185 186 private void Initialize() 187 { 188 #region Create parameters 189 Parameters.Add(new ValueLookupParameter<IRandom>(RandomName, "A pseudo random number generator.")); 190 Parameters.Add(new ValueLookupParameter<BoolValue>(MaximizationName, "True if the problem is a maximization problem, otherwise false.")); 191 Parameters.Add(new LookupParameter<DoubleValue>(QualityName, "The value which represents the quality of a solution.")); 192 Parameters.Add(new ValueLookupParameter<DoubleValue>(BestKnownQualityName, "The best known quality value found so far.")); 193 Parameters.Add(new LookupParameter<DoubleValue>(MoveQualityName, "The value which represents the quality of a move.")); 194 Parameters.Add(new LookupParameter<DoubleValue>(TemperatureName, "The current temperature.")); 195 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerTemperatureName, "The lower bound of the temperature.")); 196 Parameters.Add(new LookupParameter<IntValue>(IterationsName, "The number of iterations.")); 197 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsName, "The maximum number of iterations which should be processed.")); 198 199 Parameters.Add(new ValueLookupParameter<IOperator>(MoveGeneratorName, "The operator that generates the moves.")); 200 Parameters.Add(new ValueLookupParameter<IOperator>(MoveEvaluatorName, "The operator that evaluates a move.")); 201 Parameters.Add(new ValueLookupParameter<IOperator>(MoveMakerName, "The operator that performs a move and updates the quality.")); 202 Parameters.Add(new ValueLookupParameter<IOperator>(AnnealingOperatorName, "The operator that cools the temperature.")); 203 Parameters.Add(new ValueLookupParameter<IOperator>(ReheatingOperatorName, "The operator that reheats the temperature if necessary.")); 204 Parameters.Add(new ValueLookupParameter<IOperator>(TemperatureInitializerName, "The operator that initialized the temperature.")); 205 206 Parameters.Add(new ValueLookupParameter<IOperator>(AnalyzerName, "The operator used to analyze each generation.")); 207 Parameters.Add(new ValueLookupParameter<VariableCollection>(ResultsName, "The variable collection where results should be stored.")); 208 Parameters.Add(new LookupParameter<IntValue>(EvaluatedMovesName, "The number of evaluated moves.")); 209 210 Parameters.Add(new LookupParameter<DoubleValue>(InitialTemperatureName, "The initial temperature.")); 211 Parameters.Add(new LookupParameter<IntValue>(TemperatureStartIndexName, "The index where the annealing or heating was last changed.")); 212 Parameters.Add(new LookupParameter<BoolValue>(CoolingName, "True when the temperature should be cooled, false otherwise.")); 213 Parameters.Add(new LookupParameter<DoubleValue>(StartTemperatureName, "The temperature from which cooling or reheating should occur.")); 214 Parameters.Add(new LookupParameter<DoubleValue>(EndTemperatureName, "The temperature to which should be cooled or heated.")); 215 216 217 #endregion 218 219 #region Create operators 220 var variableCreator = new VariableCreator(); 221 var ssp1 = new SubScopesProcessor(); 222 var analyzer1 = new Placeholder(); 223 var ssp2 = new SubScopesProcessor(); 224 var resultsCollector = new ResultsCollector(); 225 var mainProcessor = new SubScopesProcessor(); 226 var moveGenerator = new Placeholder(); 227 var moveEvaluationProcessor = new SubScopesProcessor(); 228 var moveEvaluator = new Placeholder(); 229 var evaluatedMovesCounter = new IntCounter(); 230 var qualityComparator = new ProbabilisticQualityComparator(); 231 var acceptsQualityBranch = new ConditionalBranch(); 232 var moveMaker = new Placeholder(); 233 var temperatureController = new TemperatureController(); 234 var subScopesRemover = new SubScopesRemover(); 235 var iterationsCounter = new IntCounter(); 236 var iterationsComparator = new Comparator(); 237 var ssp3 = new SubScopesProcessor(); 238 var analyzer2 = new Placeholder(); 239 var iterationsTermination = new ConditionalBranch(); 240 241 variableCreator.Name = "Initialize Memory"; 242 variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>(TemperatureInitializedName, new BoolValue(false))); 243 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(AverageAcceptanceRatioName, new DoubleValue(0d))); 244 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(ConsecutiveRejectedSolutionsCountName, new IntValue(0))); 245 variableCreator.CollectedValues.Add(new ValueParameter<ItemList<BoolValue>>(AcceptanceMemoryName, new ItemList<BoolValue>())); 246 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(LastQualityName, new DoubleValue(-1))); 247 variableCreator.CollectedValues.Add(new ValueParameter<ItemList<DoubleValue>>(UphillMovesMemoryName, new ItemList<DoubleValue>())); 248 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(CurrentRandomWalkStepName, new IntValue(0))); 249 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(TemperatureBeforeReheatName, new DoubleValue(0))); 250 variableCreator.CollectedValues.Add(new ValueParameter<ItemList<DoubleValue>>(QualitiesBeforeReheatingName, new ItemList<DoubleValue>())); 251 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>(LastAcceptedQualityName, new DoubleValue(-1))); 252 253 analyzer1.Name = "Analyzer"; 254 analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name; 255 256 moveGenerator.Name = "Move generator"; 257 moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name; 258 259 moveEvaluator.Name = "Move evaluator"; 260 moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name; 261 262 evaluatedMovesCounter.Name = "EvaluatedMoves++"; 263 evaluatedMovesCounter.ValueParameter.ActualName = EvaluatedMovesParameter.Name; 264 evaluatedMovesCounter.Increment = new IntValue(1); 265 266 qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name; 267 qualityComparator.RightSideParameter.ActualName = QualityParameter.Name; 268 qualityComparator.ResultParameter.ActualName = IsAcceptedName; 269 qualityComparator.DampeningParameter.ActualName = TemperatureParameter.Name; 270 271 acceptsQualityBranch.ConditionParameter.ActualName = IsAcceptedName; 272 273 moveMaker.Name = "Move maker"; 274 moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name; 275 276 277 subScopesRemover.RemoveAllSubScopes = true; 278 279 iterationsCounter.Name = "Iterations++"; 280 iterationsCounter.Increment = new IntValue(1); 281 iterationsCounter.ValueParameter.ActualName = IterationsParameter.Name; 282 283 iterationsComparator.Name = "Iterations >= MaximumIterations"; 284 iterationsComparator.LeftSideParameter.ActualName = IterationsParameter.Name; 285 iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name; 286 iterationsComparator.ResultParameter.ActualName = TerminateName; 287 iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual; 288 289 analyzer2.Name = "Analyzer (placeholder)"; 290 analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name; 291 292 iterationsTermination.Name = "Iterations termination condition"; 293 iterationsTermination.ConditionParameter.ActualName = TerminateName; 294 #endregion 295 296 #region Create operator graph 297 OperatorGraph.InitialOperator = variableCreator; 298 variableCreator.Successor = ssp1; 299 ssp1.Operators.Add(analyzer1); 300 ssp1.Successor = ssp2; 301 analyzer1.Successor = null; 302 ssp2.Operators.Add(resultsCollector); 303 ssp2.Successor = mainProcessor; 304 resultsCollector.Successor = null; 305 mainProcessor.Operators.Add(moveGenerator); 306 mainProcessor.Successor = iterationsCounter; 307 moveGenerator.Successor = moveEvaluationProcessor; 308 moveEvaluationProcessor.Operators.Add(moveEvaluator); 309 moveEvaluationProcessor.Successor = evaluatedMovesCounter; 310 moveEvaluator.Successor = qualityComparator; 311 qualityComparator.Successor = acceptsQualityBranch; 312 acceptsQualityBranch.TrueBranch = moveMaker; 313 acceptsQualityBranch.FalseBranch = null; 314 acceptsQualityBranch.Successor = temperatureController; 315 moveMaker.Successor = null; 316 temperatureController.Successor = null; 317 evaluatedMovesCounter.Successor = subScopesRemover; 318 subScopesRemover.Successor = null; 319 iterationsCounter.Successor = iterationsComparator; 320 iterationsComparator.Successor = ssp3; 321 ssp3.Operators.Add(analyzer2); 322 ssp3.Successor = iterationsTermination; 323 iterationsTermination.TrueBranch = null; 324 iterationsTermination.FalseBranch = mainProcessor; 325 #endregion 326 } 327 328 public override IOperation Apply() 329 { 330 if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null) 331 return null; 332 return base.Apply(); 333 } 40 334 } 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 }47 public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {48 get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }49 }50 public LookupParameter<DoubleValue> MoveQualityParameter {51 get { return (LookupParameter<DoubleValue>)Parameters["MoveQuality"]; }52 }53 public ILookupParameter<DoubleValue> TemperatureParameter {54 get { return (ILookupParameter<DoubleValue>)Parameters["Temperature"]; }55 }56 public ValueLookupParameter<DoubleValue> StartTemperatureParameter {57 get { return (ValueLookupParameter<DoubleValue>)Parameters["StartTemperature"]; }58 }59 public ValueLookupParameter<DoubleValue> EndTemperatureParameter {60 get { return (ValueLookupParameter<DoubleValue>)Parameters["EndTemperature"]; }61 }62 public ValueLookupParameter<IntValue> InnerIterationsParameter {63 get { return (ValueLookupParameter<IntValue>)Parameters["InnerIterations"]; }64 }65 public LookupParameter<IntValue> IterationsParameter {66 get { return (LookupParameter<IntValue>)Parameters["Iterations"]; }67 }68 public ValueLookupParameter<IntValue> MaximumIterationsParameter {69 get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }70 }71 public ValueLookupParameter<IOperator> MoveGeneratorParameter {72 get { return (ValueLookupParameter<IOperator>)Parameters["MoveGenerator"]; }73 }74 public ValueLookupParameter<IOperator> MoveEvaluatorParameter {75 get { return (ValueLookupParameter<IOperator>)Parameters["MoveEvaluator"]; }76 }77 public ValueLookupParameter<IOperator> MoveMakerParameter {78 get { return (ValueLookupParameter<IOperator>)Parameters["MoveMaker"]; }79 }80 public ValueLookupParameter<IOperator> AnnealingOperatorParameter {81 get { return (ValueLookupParameter<IOperator>)Parameters["AnnealingOperator"]; }82 }83 public ValueLookupParameter<IOperator> AnalyzerParameter {84 get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }85 }86 public ValueLookupParameter<VariableCollection> ResultsParameter {87 get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }88 }89 public LookupParameter<IntValue> EvaluatedMovesParameter {90 get { return (LookupParameter<IntValue>)Parameters["EvaluatedMoves"]; }91 }92 #endregion93 94 [StorableConstructor]95 private SimulatedAnnealingMainLoop(bool deserializing) : base(deserializing) { }96 private SimulatedAnnealingMainLoop(SimulatedAnnealingMainLoop original, Cloner cloner)97 : base(original, cloner) {98 }99 public override IDeepCloneable Clone(Cloner cloner) {100 return new SimulatedAnnealingMainLoop(this, cloner);101 }102 public SimulatedAnnealingMainLoop()103 : base() {104 Initialize();105 }106 107 private void Initialize() {108 #region Create parameters109 Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));110 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));111 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));112 Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));113 Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The value which represents the quality of a move."));114 Parameters.Add(new LookupParameter<DoubleValue>("Temperature", "The current temperature."));115 Parameters.Add(new ValueLookupParameter<DoubleValue>("StartTemperature", "The initial temperature."));116 Parameters.Add(new ValueLookupParameter<DoubleValue>("EndTemperature", "The end temperature."));117 Parameters.Add(new ValueLookupParameter<IntValue>("InnerIterations", "The amount of inner iterations (number of moves before temperature is adjusted again)."));118 Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations."));119 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations which should be processed."));120 121 Parameters.Add(new ValueLookupParameter<IOperator>("MoveGenerator", "The operator that generates the moves."));122 Parameters.Add(new ValueLookupParameter<IOperator>("MoveEvaluator", "The operator that evaluates a move."));123 Parameters.Add(new ValueLookupParameter<IOperator>("MoveMaker", "The operator that performs a move and updates the quality."));124 Parameters.Add(new ValueLookupParameter<IOperator>("AnnealingOperator", "The operator that modifies the temperature."));125 126 Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));127 Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));128 Parameters.Add(new LookupParameter<IntValue>("EvaluatedMoves", "The number of evaluated moves."));129 #endregion130 131 #region Create operators132 Assigner temperatureInitializer = new Assigner();133 ResultsCollector resultsCollector1 = new ResultsCollector();134 SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();135 Placeholder analyzer1 = new Placeholder();136 SubScopesProcessor sssp = new SubScopesProcessor();137 ResultsCollector resultsCollector = new ResultsCollector();138 Placeholder annealingOperator = new Placeholder();139 UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();140 Placeholder moveGenerator = new Placeholder();141 UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();142 Placeholder moveEvaluator = new Placeholder();143 SubScopesCounter subScopesCounter = new SubScopesCounter();144 ProbabilisticQualityComparator qualityComparator = new ProbabilisticQualityComparator();145 ConditionalBranch improvesQualityBranch = new ConditionalBranch();146 Placeholder moveMaker = new Placeholder();147 SubScopesRemover subScopesRemover = new SubScopesRemover();148 IntCounter iterationsCounter = new IntCounter();149 Comparator iterationsComparator = new Comparator();150 SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();151 Placeholder analyzer2 = new Placeholder();152 ConditionalBranch iterationsTermination = new ConditionalBranch();153 154 temperatureInitializer.LeftSideParameter.ActualName = TemperatureParameter.ActualName;155 temperatureInitializer.RightSideParameter.ActualName = StartTemperatureParameter.Name;156 157 resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name));158 resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;159 160 analyzer1.Name = "Analyzer (placeholder)";161 analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;162 163 annealingOperator.Name = "Annealing operator (placeholder)";164 annealingOperator.OperatorParameter.ActualName = AnnealingOperatorParameter.Name;165 166 moveGenerator.Name = "Move generator (placeholder)";167 moveGenerator.OperatorParameter.ActualName = MoveGeneratorParameter.Name;168 169 moveEvaluator.Name = "Move evaluator (placeholder)";170 moveEvaluator.OperatorParameter.ActualName = MoveEvaluatorParameter.Name;171 172 subScopesCounter.Name = "Increment EvaluatedMoves";173 subScopesCounter.ValueParameter.ActualName = EvaluatedMovesParameter.Name;174 175 qualityComparator.LeftSideParameter.ActualName = MoveQualityParameter.Name;176 qualityComparator.RightSideParameter.ActualName = QualityParameter.Name;177 qualityComparator.ResultParameter.ActualName = "IsBetter";178 qualityComparator.DampeningParameter.ActualName = "Temperature";179 180 improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";181 182 moveMaker.Name = "Move maker (placeholder)";183 moveMaker.OperatorParameter.ActualName = MoveMakerParameter.Name;184 185 subScopesRemover.RemoveAllSubScopes = true;186 187 iterationsCounter.Name = "Increment Iterations";188 iterationsCounter.Increment = new IntValue(1);189 iterationsCounter.ValueParameter.ActualName = IterationsParameter.Name;190 191 iterationsComparator.Name = "Iterations >= MaximumIterations";192 iterationsComparator.LeftSideParameter.ActualName = IterationsParameter.Name;193 iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;194 iterationsComparator.ResultParameter.ActualName = "Terminate";195 iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;196 197 analyzer2.Name = "Analyzer (placeholder)";198 analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;199 200 iterationsTermination.Name = "Iterations termination condition";201 iterationsTermination.ConditionParameter.ActualName = "Terminate";202 #endregion203 204 #region Create operator graph205 OperatorGraph.InitialOperator = temperatureInitializer;206 temperatureInitializer.Successor = resultsCollector1;207 resultsCollector1.Successor = subScopesProcessor0;208 subScopesProcessor0.Operators.Add(analyzer1);209 subScopesProcessor0.Successor = sssp;210 analyzer1.Successor = null;211 sssp.Operators.Add(resultsCollector);212 sssp.Successor = annealingOperator;213 resultsCollector.Successor = null;214 annealingOperator.Successor = mainProcessor;215 mainProcessor.Operator = moveGenerator;216 mainProcessor.Successor = iterationsCounter;217 moveGenerator.Successor = moveEvaluationProcessor;218 moveEvaluationProcessor.Operator = moveEvaluator;219 moveEvaluationProcessor.Successor = subScopesCounter;220 moveEvaluator.Successor = qualityComparator;221 qualityComparator.Successor = improvesQualityBranch;222 improvesQualityBranch.TrueBranch = moveMaker;223 improvesQualityBranch.FalseBranch = null;224 improvesQualityBranch.Successor = null;225 moveMaker.Successor = null;226 subScopesCounter.Successor = subScopesRemover;227 subScopesRemover.Successor = null;228 iterationsCounter.Successor = iterationsComparator;229 iterationsComparator.Successor = subScopesProcessor1;230 subScopesProcessor1.Operators.Add(analyzer2);231 subScopesProcessor1.Successor = iterationsTermination;232 iterationsTermination.TrueBranch = null;233 iterationsTermination.FalseBranch = annealingOperator;234 #endregion235 }236 237 [StorableHook(HookType.AfterDeserialization)]238 private void AfterDeserialization() {239 // BackwardsCompatibility3.3240 #region Backwards compatible code (remove with 3.4)241 if (!Parameters.ContainsKey("Iterations"))242 Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations."));243 if (!Parameters.ContainsKey("Temperature"))244 Parameters.Add(new LookupParameter<DoubleValue>("Temperature", "The current temperature."));245 #endregion246 }247 248 public override IOperation Apply() {249 if (MoveGeneratorParameter.ActualValue == null || MoveEvaluatorParameter.ActualValue == null || MoveMakerParameter.ActualValue == null)250 return null;251 return base.Apply();252 }253 }254 335 }
Note: See TracChangeset
for help on using the changeset viewer.