- Timestamp:
- 07/26/15 21:32:52 (9 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Analysis/3.3/Optimizers/IRRestarter.cs
r12803 r12804 37 37 /// A run in which an algorithm is executed for a certain maximum time only. 38 38 /// </summary> 39 [Item("Independent Random Restart Run", "A run in which an optimizer is repeateduntil either a certain target value is reached or a maximum budget is exceeded.")]39 [Item("Independent Random Restarter", "An optimizer that repeats an algorithm until either a certain target value is reached or a maximum budget is exceeded.")] 40 40 [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 117)] 41 41 [StorableClass] 42 public sealed class IndepdentRandomRestartRun : NamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged { 42 public sealed class IndepdentRandomRestarter : NamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged { 43 private const string ExecutionTimeResultName = "Execution Time"; 44 private const string BestQualityResultName = "BestQuality"; 45 43 46 public string Filename { get; set; } 44 47 … … 100 103 public bool Maximization { 101 104 get { return maximization; } 102 privateset {105 set { 103 106 if (maximization == value) return; 104 107 maximization = value; … … 114 117 if (moveCostPerSolution == value) return; 115 118 moveCostPerSolution = value; 119 perEvaluationsAnalyzer.MoveCostPerSolutionParameter.Value = new DoubleValue(moveCostPerSolution); 116 120 OnPropertyChanged("MoveCostPerSolution"); 117 121 } 118 122 } 119 123 120 124 [Storable] 125 private QualityPerClockAnalyzer perClockAnalyzer; 126 [Storable] 127 private QualityPerEvaluationsAnalyzer perEvaluationsAnalyzer; 128 129 [Storable] 130 private ExecutionState executionState; 121 131 public ExecutionState ExecutionState { 122 get { return (Algorithm != null) ? Algorithm.ExecutionState : ExecutionState.Stopped; } 132 get { return executionState; } 133 private set { 134 if (executionState != value) { 135 executionState = value; 136 OnExecutionStateChanged(); 137 OnItemImageChanged(); 138 } 139 } 123 140 } 124 141 … … 176 193 get { return algorithm; } 177 194 set { 195 if (value != null && !typeof(ISingleObjectiveHeuristicOptimizationProblem).IsAssignableFrom(value.ProblemType)) 196 throw new ArgumentException("Algorithm is not single-objective!"); 178 197 if (algorithm == value) return; 179 if (algorithm != null) DeregisterAlgorithmEvents(); 198 if (algorithm != null) { 199 DeregisterAlgorithmEvents(); 200 RemoveAlgorithmAnalyzers(); 201 } 180 202 algorithm = value; 181 203 if (algorithm != null) { 182 204 RegisterAlgorithmEvents(); 205 AddAlgorithmAnalyzers(); 183 206 } 184 207 OnPropertyChanged("Algorithm"); … … 228 251 } 229 252 253 private ISingleObjectiveHeuristicOptimizationProblem problem; 254 230 255 [StorableConstructor] 231 private IndepdentRandomRestart Run(bool deserializing) : base(deserializing) { }232 private IndepdentRandomRestart Run(IndepdentRandomRestartRunoriginal, Cloner cloner)256 private IndepdentRandomRestarter(bool deserializing) : base(deserializing) { } 257 private IndepdentRandomRestarter(IndepdentRandomRestarter original, Cloner cloner) 233 258 : base(original, cloner) { 234 259 terminationCriterium = original.terminationCriterium; 235 260 maximumExecutionTime = original.maximumExecutionTime; 236 261 maximumEvaluations = original.maximumEvaluations; 262 moveCostPerSolution = original.moveCostPerSolution; 237 263 targetValue = original.targetValue; 264 maximization = original.maximization; 238 265 executionTime = original.executionTime; 239 266 evaluations = original.evaluations; … … 243 270 lastAlgorithmEvaluatedMoves = original.lastAlgorithmEvaluatedMoves; 244 271 272 perClockAnalyzer = cloner.Clone(original.perClockAnalyzer); 273 perEvaluationsAnalyzer = cloner.Clone(original.perEvaluationsAnalyzer); 274 245 275 algorithm = cloner.Clone(original.algorithm); 246 276 runs = cloner.Clone(original.runs); 247 277 278 ExecutionState = original.ExecutionState; 279 248 280 Initialize(); 249 281 } 250 public IndepdentRandomRestart Run()282 public IndepdentRandomRestarter() 251 283 : base() { 252 284 name = ItemName; 253 285 description = ItemDescription; 254 terminationCriterium = TerminationCriterium. OnlyByEvaluations;286 terminationCriterium = TerminationCriterium.ByTargetAndEvaluations; 255 287 maximumExecutionTime = TimeSpan.FromMinutes(1); 256 288 maximumEvaluations = 10000000; // 10 mio 289 moveCostPerSolution = 1; 257 290 targetValue = 0; 291 maximization = false; 258 292 executionTime = TimeSpan.Zero; 259 293 evaluations = 0; … … 263 297 lastAlgorithmEvaluatedMoves = 0; 264 298 299 perClockAnalyzer = new QualityPerClockAnalyzer(); 300 perEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer(); 301 265 302 Runs = new RunCollection { OptimizerName = Name }; 266 303 Initialize(); 267 304 } 268 public IndepdentRandomRestart Run(string name)305 public IndepdentRandomRestarter(string name) 269 306 : base(name) { 270 307 description = ItemDescription; 271 terminationCriterium = TerminationCriterium. OnlyByEvaluations;308 terminationCriterium = TerminationCriterium.ByTargetAndEvaluations; 272 309 maximumExecutionTime = TimeSpan.FromMinutes(1); 273 310 maximumEvaluations = 10000000; // 10 mio 311 moveCostPerSolution = 1; 274 312 targetValue = 0; 313 maximization = false; 275 314 executionTime = TimeSpan.Zero; 276 315 evaluations = 0; … … 280 319 lastAlgorithmEvaluatedMoves = 0; 281 320 321 perClockAnalyzer = new QualityPerClockAnalyzer(); 322 perEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer(); 323 282 324 Runs = new RunCollection { OptimizerName = Name }; 283 325 Initialize(); 284 326 } 285 public IndepdentRandomRestart Run(string name, string description)327 public IndepdentRandomRestarter(string name, string description) 286 328 : base(name, description) { 287 terminationCriterium = TerminationCriterium. OnlyByEvaluations;329 terminationCriterium = TerminationCriterium.ByTargetAndEvaluations; 288 330 maximumExecutionTime = TimeSpan.FromMinutes(1); 289 331 maximumEvaluations = 10000000; // 10 mio 332 moveCostPerSolution = 1; 290 333 targetValue = 0; 334 maximization = false; 291 335 executionTime = TimeSpan.Zero; 292 336 evaluations = 0; … … 296 340 lastAlgorithmEvaluatedMoves = 0; 297 341 342 perClockAnalyzer = new QualityPerClockAnalyzer(); 343 perEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer(); 344 298 345 Runs = new RunCollection { OptimizerName = Name }; 299 346 Initialize(); … … 302 349 public override IDeepCloneable Clone(Cloner cloner) { 303 350 if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState)); 304 return new IndepdentRandomRestart Run(this, cloner);351 return new IndepdentRandomRestarter(this, cloner); 305 352 } 306 353 … … 314 361 } 315 362 363 private void Reset() { 364 ExecutionTime = TimeSpan.Zero; 365 Evaluations = 0; 366 BestSoFar = double.NaN; 367 lastAlgorithmExecutionTime = TimeSpan.Zero; 368 lastAlgorithmEvaluatedSolutions = 0; 369 lastAlgorithmEvaluatedMoves = 0; 370 371 CurrentRun = null; 372 } 373 316 374 public void Prepare() { 317 375 Prepare(false); 318 376 } 319 377 public void Prepare(bool clearRuns) { 320 executionTime = TimeSpan.Zero; 321 evaluations = 0; 322 lastAlgorithmExecutionTime = TimeSpan.Zero; 323 lastAlgorithmEvaluatedSolutions = 0; 324 325 Algorithm.Prepare(clearRuns); 378 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) 379 throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState)); 380 Reset(); 381 382 if (Algorithm != null) { 383 Algorithm.Prepare(clearRuns); 384 ExecutionState = ExecutionState.Prepared; 385 OnPrepared(); 386 } 326 387 } 327 388 public void Start() { 389 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) 390 throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); 391 328 392 if (ExecutionState == ExecutionState.Prepared) { 329 currentRun = new Run(Algorithm) {393 CurrentRun = new Run(Algorithm) { 330 394 Name = Algorithm.Name + " IRRRun" + Runs.Count 331 395 }; 396 if (!CurrentRun.Results.ContainsKey(ExecutionTimeResultName)) 397 CurrentRun.Results.Add(ExecutionTimeResultName, new TimeSpanValue(TimeSpan.Zero)); 398 CurrentRun.Results.Add(perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName, new IntValue(0)); 399 CurrentRun.Results.Add(perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName, new IntValue(0)); 400 CurrentRun.Results.Add(BestQualityResultName, new DoubleValue(Maximization ? double.MinValue : double.MaxValue)); 332 401 } 333 402 Algorithm.Start(); 403 ExecutionState = ExecutionState.Started; 404 OnStarted(); 334 405 } 335 406 public void Pause() { 407 if (ExecutionState != ExecutionState.Started) 408 throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState)); 336 409 Algorithm.Pause(); 337 } 410 ExecutionState = ExecutionState.Paused; 411 OnPaused(); 412 } 413 414 private bool forceStop = false; 338 415 public void Stop() { 416 if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused)) 417 throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState)); 418 forceStop = true; 339 419 Algorithm.Stop(); 420 } 421 422 private void AddAlgorithmAnalyzers() { 423 if (!Algorithm.Parameters.ContainsKey("Analyzer")) return; 424 var analyzerParam = Algorithm.Parameters["Analyzer"] as IValueParameter<MultiAnalyzer>; 425 if (analyzerParam == null) return; 426 if (!analyzerParam.Value.Operators.Contains(perClockAnalyzer)) 427 analyzerParam.Value.Operators.Add(perClockAnalyzer); 428 if (!analyzerParam.Value.Operators.Contains(perEvaluationsAnalyzer)) 429 analyzerParam.Value.Operators.Add(perEvaluationsAnalyzer); 430 } 431 432 private void RemoveAlgorithmAnalyzers() { 433 if (!Algorithm.Parameters.ContainsKey("Analyzer")) return; 434 var analyzerParam = Algorithm.Parameters["Analyzer"] as IValueParameter<MultiAnalyzer>; 435 if (analyzerParam == null) return; 436 analyzerParam.Value.Operators.Remove(perClockAnalyzer); 437 analyzerParam.Value.Operators.Remove(perEvaluationsAnalyzer); 340 438 } 341 439 … … 400 498 algorithm.Stopped += Algorithm_Stopped; 401 499 algorithm.ProblemChanged += Algorithm_ProblemChanged; 500 Algorithm_ProblemChanged(algorithm, EventArgs.Empty); 402 501 } 403 502 private void DeregisterAlgorithmEvents() { … … 415 514 } 416 515 private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) { 417 ExecutionTime += Algorithm.ExecutionTime - lastAlgorithmExecutionTime;418 lastAlgorithmExecutionTime = Algorithm.ExecutionTime; 419 420 UpdateAlgorithmResults();421 422 if (IsFinished ) {516 if (Algorithm.ExecutionState != ExecutionState.Started) return; 517 518 if (ExecutionState == ExecutionState.Started) 519 UpdateAlgorithmResults(); 520 521 if (IsFinished && ExecutionState != ExecutionState.Stopped) { 423 522 Algorithm.Stop(); 424 523 } … … 427 526 428 527 private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) { 429 OnExecutionStateChanged();528 //OnExecutionStateChanged(); 430 529 } 431 530 private void Algorithm_Paused(object sender, EventArgs e) { 432 ExecutionTime += Algorithm.ExecutionTime - lastAlgorithmExecutionTime;433 lastAlgorithmExecutionTime = Algorithm.ExecutionTime;434 435 531 UpdateAlgorithmResults(); 436 532 OnPaused(); 437 533 } 438 534 private void Algorithm_Prepared(object sender, EventArgs e) { 439 OnPrepared(); 535 lastAlgorithmEvaluatedSolutions = 0; 536 lastAlgorithmEvaluatedMoves = 0; 537 lastAlgorithmExecutionTime = TimeSpan.Zero; 440 538 } 441 539 private void Algorithm_Started(object sender, EventArgs e) { 442 OnStarted();540 //OnStarted(); 443 541 } 444 542 private void Algorithm_Stopped(object sender, EventArgs e) { 445 ExecutionTime += Algorithm.ExecutionTime - lastAlgorithmExecutionTime;446 lastAlgorithmExecutionTime = Algorithm.ExecutionTime;447 448 543 var bestQuality = UpdateAlgorithmResults(); 449 544 545 var execTime = ((TimeSpanValue)currentRun.Results[ExecutionTimeResultName]).Value; 450 546 foreach (var result in Algorithm.Results) { 451 if (result.Name == "QualityPerClock") {547 if (result.Name == perClockAnalyzer.QualityPerClockParameter.ResultName) { 452 548 if (!currentRun.Results.ContainsKey(result.Name)) 453 549 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 454 550 else { 455 551 var dt = (IndexedDataTable<double>)currentRun.Results[result.Name]; 456 var execTime = ((TimeSpanValue)currentRun.Results["ExecutionTime"]).Value.TotalSeconds;457 552 var best = dt.Rows.First().Values.Last().Item2; 458 553 var resultDt = (IndexedDataTable<double>)result.Value; 459 554 foreach (var tupl in resultDt.Rows.First().Values) { 460 555 if (Maximization && tupl.Item2 > best || !Maximization && tupl.Item2 < best) { 461 dt.Rows.First().Values.Add(Tuple.Create(execTime + tupl.Item1, tupl.Item2));556 dt.Rows.First().Values.Add(Tuple.Create(execTime.TotalSeconds + tupl.Item1, tupl.Item2)); 462 557 best = tupl.Item2; 463 558 } 464 559 } 465 560 } 466 } else if (result.Name == "QualityPerEvaluations") {561 } else if (result.Name == perEvaluationsAnalyzer.QualityPerEvaluationsParameter.ResultName) { 467 562 if (!currentRun.Results.ContainsKey(result.Name)) 468 563 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 469 564 else { 470 565 var dt = (IndexedDataTable<double>)currentRun.Results[result.Name]; 471 var evalSols = (( DoubleValue)currentRun.Results["EvaluatedSolutions"]).Value;472 var evalMoves = (( DoubleValue)currentRun.Results["EvaluatedMoves"]).Value;566 var evalSols = ((IntValue)currentRun.Results[perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName]).Value; 567 var evalMoves = ((IntValue)currentRun.Results[perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName]).Value; 473 568 var best = dt.Rows.First().Values.Last().Item2; 474 569 var resultDt = (IndexedDataTable<double>)result.Value; … … 480 575 } 481 576 } 482 } else if (result.Name == "EvaluatedSolutions") { 483 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 484 } else if (result.Name == "EvaluatedMoves") { 485 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 486 } else if (result.Name == "ExecutionTime") { 487 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 488 } else if (result.Name == "BestQuality") { 489 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 577 } else if (result.Name == perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName) { 578 var evalSols = ((IntValue)currentRun.Results[perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName]); 579 evalSols.Value += ((IntValue)result.Value).Value; 580 } else if (result.Name == perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName) { 581 var evalMoves = ((IntValue)currentRun.Results[perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName]); 582 evalMoves.Value += ((IntValue)result.Value).Value; 583 } else if (result.Name == perEvaluationsAnalyzer.BestQualityParameter.ActualName) { 584 var best = ((DoubleValue)currentRun.Results[BestQualityResultName]).Value; 585 if (Maximization && best < bestQuality || !Maximization && best > bestQuality) 586 currentRun.Results[BestQualityResultName] = new DoubleValue(bestQuality); 490 587 } else if (result.Name.ToLower().EndsWith("solution") && BestSoFar == bestQuality) { 491 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 588 if (currentRun.Results.ContainsKey(result.Name)) 589 currentRun.Results[result.Name] = (IItem)result.Value.Clone(); 590 else currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 492 591 } 493 592 } 494 foreach (var result in Algorithm.Results) { 495 if (result.Name == "QualityPerClock") { 496 497 } else if (result.Name == "QualityPerEvaluations") { 498 } else if (result.Name == "EvaluatedSolutions") { 499 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 500 } else if (result.Name == "EvaluatedMoves") { 501 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 502 } else if (result.Name == "ExecutionTime") { 503 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 504 } else if (result.Name == "BestQuality") { 505 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 506 } else if (result.Name.ToLower().EndsWith("solution") && BestSoFar == bestQuality) { 507 currentRun.Results.Add(result.Name, (IItem)result.Value.Clone()); 508 } 509 } 510 511 512 if (IsFinished) { 513 514 } 515 516 // TODO 517 var cloner = new Cloner(); 518 var algRun = cloner.Clone(Algorithm.Runs.Last()); 519 Runs.Add(algRun); 520 Algorithm.Runs.Clear(); 521 OnStopped(); 593 currentRun.Results[ExecutionTimeResultName] = new TimeSpanValue(execTime + Algorithm.ExecutionTime); 594 595 if (!forceStop && !IsFinished) { 596 Algorithm.Prepare(); 597 Algorithm.Start(); 598 } else { 599 forceStop = false; 600 Runs.Add(currentRun); 601 currentRun = null; 602 Algorithm.Prepare(true); 603 ExecutionState = ExecutionState.Stopped; 604 OnStopped(); 605 } 522 606 } 523 607 524 608 private double UpdateAlgorithmResults() { 609 ExecutionTime += Algorithm.ExecutionTime - lastAlgorithmExecutionTime; 610 lastAlgorithmExecutionTime = Algorithm.ExecutionTime; 611 525 612 IResult evaluationsResult; 526 if (Algorithm.Results.TryGetValue( "EvaluatedSolutions", out evaluationsResult)) {613 if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName, out evaluationsResult)) { 527 614 var evals = ((IntValue)evaluationsResult.Value).Value; 528 615 Evaluations += evals - lastAlgorithmEvaluatedSolutions; 529 616 lastAlgorithmEvaluatedSolutions = evals; 530 617 } 531 if (Algorithm.Results.TryGetValue( "EvaluatedMoves", out evaluationsResult)) {618 if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName, out evaluationsResult)) { 532 619 var evals = ((IntValue)evaluationsResult.Value).Value; 533 620 Evaluations += moveCostPerSolution * (evals - lastAlgorithmEvaluatedMoves); 534 621 lastAlgorithmEvaluatedMoves = evals; 535 622 } 536 if (Algorithm.Results.TryGetValue( "BestQuality", out evaluationsResult)) {537 var bestQuality = ((DoubleValue)evaluationsResult ).Value;623 if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.BestQualityParameter.ActualName, out evaluationsResult)) { 624 var bestQuality = ((DoubleValue)evaluationsResult.Value).Value; 538 625 if (double.IsNaN(BestSoFar) 539 626 || Maximization && bestQuality > BestSoFar … … 546 633 547 634 private void Algorithm_ProblemChanged(object sender, EventArgs e) { 548 var soProblem = Algorithm.Problem as ISingleObjectiveHeuristicOptimizationProblem; 549 if (soProblem == null) return; 550 var maxParam = soProblem.MaximizationParameter as IValueParameter<BoolValue>; 635 if (problem != null) DeregisterProblemEvents(); 636 637 problem = Algorithm.Problem as ISingleObjectiveHeuristicOptimizationProblem; 638 if (problem == null) return; 639 RegisterProblemEvents(); 640 641 AddAlgorithmAnalyzers(); 642 643 var maxParam = problem.MaximizationParameter as IValueParameter<BoolValue>; 551 644 if (maxParam != null) 552 645 Maximization = maxParam.Value.Value; 553 var bkParam = soProblem.BestKnownQualityParameter as IValueParameter<DoubleValue>;646 var bkParam = problem.BestKnownQualityParameter as IValueParameter<DoubleValue>; 554 647 if (bkParam != null && bkParam.Value != null) 555 648 TargetValue = bkParam.Value.Value; 649 650 Reset(); 651 } 652 653 private void RegisterProblemEvents() { 654 problem.Reset += ProblemOnReset; 655 problem.OperatorsChanged += ProblemOnOperatorsChanged; 656 } 657 658 private void DeregisterProblemEvents() { 659 problem.Reset -= ProblemOnReset; 660 problem.OperatorsChanged -= ProblemOnOperatorsChanged; 661 } 662 663 private void ProblemOnReset(object sender, EventArgs eventArgs) { 664 AddAlgorithmAnalyzers(); 665 } 666 667 private void ProblemOnOperatorsChanged(object sender, EventArgs eventArgs) { 668 AddAlgorithmAnalyzers(); 556 669 } 557 670 #endregion
Note: See TracChangeset
for help on using the changeset viewer.