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