- Timestamp:
- 01/13/17 18:18:37 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRAlgorithm.cs
r14562 r14563 138 138 [Storable] 139 139 private BestAverageWorstQualityAnalyzer qualityAnalyzer; 140 [Storable] 141 private QualityPerClockAnalyzer qualityPerClockAnalyzer; 142 [Storable] 143 private QualityPerEvaluationsAnalyzer qualityPerEvaluationsAnalyzer; 140 144 141 145 [StorableConstructor] … … 144 148 context = cloner.Clone(original.context); 145 149 qualityAnalyzer = cloner.Clone(original.qualityAnalyzer); 150 qualityPerClockAnalyzer = cloner.Clone(original.qualityPerClockAnalyzer); 151 qualityPerEvaluationsAnalyzer = cloner.Clone(original.qualityPerEvaluationsAnalyzer); 152 146 153 RegisterEventHandlers(); 147 154 } … … 150 157 Parameters.Add(new FixedValueParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population that is evolved.", new IntValue(20))); 151 158 Parameters.Add(new OptionalValueParameter<IntValue>("MaximumEvaluations", "The maximum number of solution evaluations.")); 152 Parameters.Add(new OptionalValueParameter<TimeSpanValue>("MaximumExecutionTime", "The maximum runtime.", new TimeSpanValue(TimeSpan.FromMinutes(1 ))));159 Parameters.Add(new OptionalValueParameter<TimeSpanValue>("MaximumExecutionTime", "The maximum runtime.", new TimeSpanValue(TimeSpan.FromMinutes(10)))); 153 160 Parameters.Add(new OptionalValueParameter<DoubleValue>("TargetQuality", "The target quality at which the algorithm terminates.")); 154 161 Parameters.Add(new FixedValueParameter<BoolValue>("SetSeedRandomly", "Whether each run of the algorithm should be conducted with a new random seed.", new BoolValue(true))); … … 158 165 159 166 qualityAnalyzer = new BestAverageWorstQualityAnalyzer(); 167 qualityPerClockAnalyzer = new QualityPerClockAnalyzer(); 168 qualityPerEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer(); 169 160 170 RegisterEventHandlers(); 161 171 } … … 193 203 foreach (var param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>()) 194 204 param.Depth = 1; 195 multiAnalyzer.Operators.Add(analyzer, analyzer.EnabledByDefault );205 multiAnalyzer.Operators.Add(analyzer, analyzer.EnabledByDefault || analyzer is ISimilarityBasedOperator); 196 206 } 197 207 } 198 208 multiAnalyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault); 209 multiAnalyzer.Operators.Add(qualityPerClockAnalyzer, true); 210 multiAnalyzer.Operators.Add(qualityPerEvaluationsAnalyzer, true); 199 211 } 200 212 } … … 300 312 301 313 if (!replaced) { 302 offspring = (ISingleObjectiveSolutionScope<TSolution>)Context.Population.SampleRandom(Context.Random).Clone();303 var before = offspring.Fitness;314 var before = Context.Population.SampleRandom(Context.Random); 315 offspring = (ISingleObjectiveSolutionScope<TSolution>)before.Clone(); 304 316 AdaptiveWalk(offspring, Context.LocalSearchEvaluations * 2, token); 305 Context.AdaptivewalkingStat.Add(Tuple.Create(before, offspring.Fitness));306 if (Context.AdaptivewalkingStat.Count % 10 == 0) Context.RelearnAdaptiveWalkPerformanceModel();317 if (!Eq(before, offspring)) 318 Context.AddAdaptivewalkingResult(before, offspring); 307 319 if (Replace(offspring, token)) { 308 320 Context.ByAdaptivewalking++; … … 345 357 346 358 var sp = new ScatterPlot("Breeding Correlation", ""); 347 sp.Rows.Add(new ScatterPlotDataRow("Parent1 vs Offspring", "", Context.BreedingStat.Select(x => new Point2D<double>(x.Item1, x.Item3))) { VisualProperties = { PointSize = 6 }}); 348 sp.Rows.Add(new ScatterPlotDataRow("Parent2 vs Offspring", "", Context.BreedingStat.Select(x => new Point2D<double>(x.Item2, x.Item3))) { VisualProperties = { PointSize = 6 } }); 359 sp.Rows.Add(new ScatterPlotDataRow("Parent1 vs Offspring", "", Context.BreedingStat.Select(x => new Point2D<double>(x.Item1, x.Item4))) { VisualProperties = { PointSize = 6 }}); 360 sp.Rows.Add(new ScatterPlotDataRow("Parent2 vs Offspring", "", Context.BreedingStat.Select(x => new Point2D<double>(x.Item2, x.Item4))) { VisualProperties = { PointSize = 6 } }); 361 sp.Rows.Add(new ScatterPlotDataRow("Parent Distance vs Offspring", "", Context.BreedingStat.Select(x => new Point2D<double>(x.Item3, x.Item4))) { VisualProperties = { PointSize = 6 } }); 349 362 if (!Results.TryGetValue("BreedingStat", out res)) { 350 363 Results.Add(new Result("BreedingStat", sp)); … … 352 365 353 366 sp = new ScatterPlot("Relinking Correlation", ""); 354 sp.Rows.Add(new ScatterPlotDataRow("A vs Relink", "", Context.RelinkingStat.Select(x => new Point2D<double>(x.Item1, x.Item3))) { VisualProperties = { PointSize = 6 } }); 355 sp.Rows.Add(new ScatterPlotDataRow("B vs Relink", "", Context.RelinkingStat.Select(x => new Point2D<double>(x.Item2, x.Item3))) { VisualProperties = { PointSize = 6 } }); 367 sp.Rows.Add(new ScatterPlotDataRow("A vs Relink", "", Context.RelinkingStat.Select(x => new Point2D<double>(x.Item1, x.Item4))) { VisualProperties = { PointSize = 6 } }); 368 sp.Rows.Add(new ScatterPlotDataRow("B vs Relink", "", Context.RelinkingStat.Select(x => new Point2D<double>(x.Item2, x.Item4))) { VisualProperties = { PointSize = 6 } }); 369 sp.Rows.Add(new ScatterPlotDataRow("d(A,B) vs Offspring", "", Context.RelinkingStat.Select(x => new Point2D<double>(x.Item3, x.Item4))) { VisualProperties = { PointSize = 6 } }); 356 370 if (!Results.TryGetValue("RelinkingStat", out res)) { 357 371 Results.Add(new Result("RelinkingStat", sp)); … … 359 373 360 374 sp = new ScatterPlot("Delinking Correlation", ""); 361 sp.Rows.Add(new ScatterPlotDataRow("A vs Delink", "", Context.DelinkingStat.Select(x => new Point2D<double>(x.Item1, x.Item3))) { VisualProperties = { PointSize = 6 } }); 362 sp.Rows.Add(new ScatterPlotDataRow("B vs Delink", "", Context.DelinkingStat.Select(x => new Point2D<double>(x.Item2, x.Item3))) { VisualProperties = { PointSize = 6 } }); 375 sp.Rows.Add(new ScatterPlotDataRow("A vs Delink", "", Context.DelinkingStat.Select(x => new Point2D<double>(x.Item1, x.Item4))) { VisualProperties = { PointSize = 6 } }); 376 sp.Rows.Add(new ScatterPlotDataRow("B vs Delink", "", Context.DelinkingStat.Select(x => new Point2D<double>(x.Item2, x.Item4))) { VisualProperties = { PointSize = 6 } }); 377 sp.Rows.Add(new ScatterPlotDataRow("d(A,B) vs Offspring", "", Context.DelinkingStat.Select(x => new Point2D<double>(x.Item3, x.Item4))) { VisualProperties = { PointSize = 6 } }); 363 378 if (!Results.TryGetValue("DelinkingStat", out res)) { 364 379 Results.Add(new Result("DelinkingStat", sp)); … … 372 387 373 388 sp = new ScatterPlot("Hillclimbing Correlation", ""); 374 sp.Rows.Add(new ScatterPlotDataRow("Start vs End", "", Context.HillclimbingStat.Select(x => new Point2D<double>(x.Item1, x.Item2))) { VisualProperties = { PointSize = 6 } });389 sp.Rows.Add(new ScatterPlotDataRow("Start vs Improvement", "", Context.HillclimbingStat.Select(x => new Point2D<double>(x.Item1, x.Item2))) { VisualProperties = { PointSize = 6 } }); 375 390 if (!Results.TryGetValue("HillclimbingStat", out res)) { 376 391 Results.Add(new Result("HillclimbingStat", sp)); … … 535 550 Context.IncrementEvaluatedSolutions(1); 536 551 } 537 var before = scope.Fitness;552 var before = (ISingleObjectiveSolutionScope<TSolution>)scope.Clone(); 538 553 var lscontext = Context.CreateSingleSolutionContext(scope); 539 554 LocalSearchParameter.Value.Optimize(lscontext); 540 var after = scope.Fitness; 541 Context.HillclimbingStat.Add(Tuple.Create(before, after)); 542 if (Context.HillclimbingStat.Count % 10 == 0) Context.RelearnHillclimbingPerformanceModel(); 555 Context.AddHillclimbingResult(before, scope); 543 556 Context.IncrementEvaluatedSolutions(lscontext.EvaluatedSolutions); 544 557 return lscontext.EvaluatedSolutions; … … 550 563 Context.IncrementEvaluatedSolutions(1); 551 564 } 552 var before = scope.Fitness;553 565 var newScope = (ISingleObjectiveSolutionScope<TSolution>)scope.Clone(); 554 566 AdaptiveWalk(newScope, maxEvals, token, subspace); 555 Context.AdaptivewalkingStat.Add(Tuple.Create(before, newScope.Fitness));556 if (Context. AdaptivewalkingStat.Count % 10 == 0) Context.RelearnAdaptiveWalkPerformanceModel();557 if (Context.IsBetter(newScope, scope))567 568 if (Context.IsBetter(newScope, scope)) { 569 Context.AddAdaptivewalkingResult(scope, newScope); 558 570 scope.Adopt(newScope); 571 } else if (!Eq(newScope, scope)) 572 Context.AddAdaptivewalkingResult(scope, newScope); 559 573 } 560 574 protected abstract void AdaptiveWalk(ISingleObjectiveSolutionScope<TSolution> scope, int maxEvals, CancellationToken token, ISolutionSubspace<TSolution> subspace = null); … … 580 594 } 581 595 582 if (Context.BreedingSuited(p1, p2)) { 583 var offspring = Breed(p1, p2, token); 584 585 if (double.IsNaN(offspring.Fitness)) { 586 Context.Evaluate(offspring, token); 587 Context.IncrementEvaluatedSolutions(1); 588 } 589 590 // new best solutions are improved using hill climbing in full solution space 591 if (Context.Population.All(p => Context.IsBetter(offspring, p))) 592 HillClimb(offspring, token); 593 else if (!Eq(offspring, p1) && !Eq(offspring, p2) && Context.HillclimbingSuited(offspring.Fitness)) 594 HillClimb(offspring, token, CalculateSubspace(new[] { p1.Solution, p2.Solution }, inverse: false)); 595 596 Context.AddBreedingResult(p1, p2, offspring); 597 if (Context.BreedingStat.Count % 10 == 0) Context.RelearnBreedingPerformanceModel(); 598 return offspring; 599 } 600 return null; 596 if (!Context.BreedingSuited(p1, p2, Dist(p1, p2))) return null; 597 598 var offspring = Breed(p1, p2, token); 599 600 if (double.IsNaN(offspring.Fitness)) { 601 Context.Evaluate(offspring, token); 602 Context.IncrementEvaluatedSolutions(1); 603 } 604 605 Context.AddBreedingResult(p1, p2, Dist(p1, p2), offspring); 606 607 // new best solutions are improved using hill climbing in full solution space 608 if (Context.Population.All(p => Context.IsBetter(offspring, p))) 609 HillClimb(offspring, token); 610 else if (!Eq(offspring, p1) && !Eq(offspring, p2) && Context.HillclimbingSuited(offspring.Fitness)) 611 HillClimb(offspring, token, CalculateSubspace(new[] { p1.Solution, p2.Solution }, inverse: false)); 612 613 return offspring; 601 614 } 602 615 … … 613 626 var p2 = Context.AtPopulation(i2); 614 627 615 if (!Context.RelinkSuited(p1, p2 )) return null;628 if (!Context.RelinkSuited(p1, p2, Dist(p1, p2))) return null; 616 629 617 630 var link = PerformRelinking(p1, p2, token, delink: false); 618 if (double.IsNaN(link.Fitness)) { 619 Context.Evaluate(link, token); 620 Context.IncrementEvaluatedSolutions(1); 621 } 631 622 632 // new best solutions are improved using hill climbing in full solution space 623 633 if (Context.Population.All(p => Context.IsBetter(link, p))) … … 637 647 var p2 = Context.AtPopulation(i2); 638 648 639 if (!Context.DelinkSuited(p1, p2 )) return null;649 if (!Context.DelinkSuited(p1, p2, Dist(p1, p2))) return null; 640 650 641 651 var link = PerformRelinking(p1, p2, token, delink: true); 642 if (double.IsNaN(link.Fitness)) { 643 Context.Evaluate(link, token); 644 Context.IncrementEvaluatedSolutions(1); 645 } 652 646 653 // new best solutions are improved using hill climbing in full solution space 647 654 if (Context.Population.All(p => Context.IsBetter(link, p))) 648 655 HillClimb(link, token); 649 // intentionally not making hill climbing after delinking in sub-space656 // intentionally not making hill climbing otherwise after delinking in sub-space 650 657 return link; 651 658 } … … 659 666 } 660 667 661 // new best solutions are improved using hill climbing662 if (Context.Population.All(p => Context.IsBetter(relink, p)))663 HillClimb(relink, token);664 665 668 if (delink) { 666 Context.AddDelinkingResult(a, b, relink); 667 if (Context.DelinkingStat.Count % 10 == 0) Context.RelearnDelinkingPerformanceModel(); 669 Context.AddDelinkingResult(a, b, Dist(a, b), relink); 668 670 } else { 669 Context.AddRelinkingResult(a, b, relink);670 if (context.RelinkingStat.Count % 10 == 0) Context.RelearnRelinkingPerformanceModel();671 } 671 Context.AddRelinkingResult(a, b, Dist(a, b), relink); 672 } 673 672 674 return relink; 673 675 } … … 682 684 ISingleObjectiveSolutionScope<TSolution> bestSample = null; 683 685 var tries = 1; 686 var avgDist = (from a in Context.Population.Shuffle(Context.Random) 687 from b in Context.Population.Shuffle(Context.Random) 688 select Dist(a, b)).Average(); 684 689 for (; tries < 100; tries++) { 685 690 var sample = Context.ToScope(Context.Model.Sample()); … … 689 694 if (Context.Population.Any(x => !Context.IsBetter(x, bestSample))) break; 690 695 } 691 if (!Context.SamplingSuited( )) break;696 if (!Context.SamplingSuited(avgDist)) break; 692 697 } 693 698 Context.IncrementEvaluatedSolutions(tries); 694 Context.AddSamplingResult(bestSample); 695 if (Context.SamplingStat.Count % 10 == 0) Context.RelearnSamplingPerformanceModel(); 699 Context.AddSamplingResult(bestSample, avgDist); 696 700 return bestSample; 697 701 }
Note: See TracChangeset
for help on using the changeset viewer.