- Timestamp:
- 01/05/17 17:50:03 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRAlgorithm.cs
r14544 r14550 230 230 var child = Create(token); 231 231 Context.LocalSearchEvaluations += HillClimb(child, token); 232 Context.LocalOptimaLevel += child.Fitness; 232 233 Context.AddToPopulation(child); 233 234 Context.BestQuality = child.Fitness; … … 237 238 } 238 239 Context.LocalSearchEvaluations /= 2; 240 Context.LocalOptimaLevel /= 2; 239 241 Context.Initialized = true; 240 242 } … … 254 256 if (offspring != null) { 255 257 if (Context.PopulationCount < MaximumPopulationSize) 256 HillClimb(offspring, token);258 AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token); 257 259 var replNew = Replace(offspring, token); 258 260 if (replNew) { … … 265 267 if (offspring != null) { 266 268 if (Context.PopulationCount < MaximumPopulationSize) 267 HillClimb(offspring, token);269 AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token); 268 270 if (Replace(offspring, token)) { 269 271 replaced = true; … … 275 277 if (offspring != null) { 276 278 if (Context.PopulationCount < MaximumPopulationSize) 277 HillClimb(offspring, token);279 AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token); 278 280 if (Replace(offspring, token)) { 279 281 replaced = true; … … 285 287 if (offspring != null) { 286 288 if (Context.PopulationCount < MaximumPopulationSize) 287 HillClimb(offspring, token);289 AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token); 288 290 if (Replace(offspring, token)) { 289 291 replaced = true; … … 294 296 if (!replaced && offspring != null) { 295 297 if (Context.HillclimbingSuited(offspring)) { 296 HillClimb(offspring, token);298 AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token); 297 299 if (Replace(offspring, token)) { 298 300 Context.ByHillclimbing++; … … 516 518 return false;// -1; 517 519 } 518 519 protected abstract bool Eq(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b); 520 521 protected bool Eq(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b) { 522 return Eq(a.Solution, b.Solution); 523 } 524 protected abstract bool Eq(TSolution a, TSolution b); 520 525 protected abstract double Dist(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b); 521 526 protected abstract ISingleObjectiveSolutionScope<TSolution> ToScope(TSolution code, double fitness = double.NaN); … … 599 604 if (Context.Population.All(p => Context.IsBetter(offspring, p))) 600 605 HillClimb(offspring, token); 601 else HillClimb(offspring, token, CalculateSubspace(new[] { p1.Solution, p2.Solution })); 606 else if (!Eq(offspring, p1) && !Eq(offspring, p2) && Context.HillclimbingSuited(offspring.Fitness)) 607 HillClimb(offspring, token, CalculateSubspace(new[] { p1.Solution, p2.Solution }, inverse: false)); 602 608 603 609 Context.AddBreedingResult(p1, p2, offspring); … … 620 626 var p2 = Context.AtPopulation(i2); 621 627 622 return Context.RelinkSuited(p1, p2) ? PerformRelinking(p1, p2, token, delink: false) : null; 628 if (!Context.RelinkSuited(p1, p2)) return null; 629 630 var link = PerformRelinking(p1, p2, token, delink: false); 631 if (double.IsNaN(link.Fitness)) { 632 Evaluate(link, token); 633 Context.IncrementEvaluatedSolutions(1); 634 } 635 // new best solutions are improved using hill climbing in full solution space 636 if (Context.Population.All(p => Context.IsBetter(link, p))) 637 HillClimb(link, token); 638 else if (!Eq(link, p1) && !Eq(link, p2) && Context.HillclimbingSuited(link.Fitness)) 639 HillClimb(link, token, CalculateSubspace(new[] { p1.Solution, p2.Solution }, inverse: true)); 640 641 return link; 623 642 } 624 643 … … 630 649 var p1 = Context.AtPopulation(i1); 631 650 var p2 = Context.AtPopulation(i2); 632 633 return Context.DelinkSuited(p1, p2) ? PerformRelinking(p1, p2, token, delink: true) : null; 651 652 if (!Context.DelinkSuited(p1, p2)) return null; 653 654 var link = PerformRelinking(p1, p2, token, delink: true); 655 if (double.IsNaN(link.Fitness)) { 656 Evaluate(link, token); 657 Context.IncrementEvaluatedSolutions(1); 658 } 659 // new best solutions are improved using hill climbing in full solution space 660 if (Context.Population.All(p => Context.IsBetter(link, p))) 661 HillClimb(link, token); 662 /*else if (!Eq(link, p1) && !Eq(link, p2) && Context.HillclimbingSuited(link.Fitness)) 663 HillClimb(link, token, CalculateSubspace(new[] { p1.Solution, p2.Solution }, inverse: false));*/ 664 665 return link; 634 666 } 635 667 … … 661 693 #region Sample 662 694 protected virtual ISingleObjectiveSolutionScope<TSolution> Sample(CancellationToken token) { 663 if (Context.PopulationCount == MaximumPopulationSize && Context.SamplingSuited()) {695 if (Context.PopulationCount == MaximumPopulationSize) { 664 696 SolutionModelTrainerParameter.Value.TrainModel(Context); 665 697 ISingleObjectiveSolutionScope<TSolution> bestSample = null; 666 698 var tries = 1; 667 for (; tries < Context.LocalSearchEvaluations; tries++) {699 for (; tries < 100; tries++) { 668 700 var sample = ToScope(Context.Model.Sample()); 669 701 Evaluate(sample, token); 670 702 if (bestSample == null || Context.IsBetter(sample, bestSample)) { 671 703 bestSample = sample; 704 if (Context.Population.Any(x => !Context.IsBetter(x, bestSample))) break; 672 705 } 673 if ( Context.Population.Any(x => !Context.IsBetter(x, bestSample))) break;706 if (!Context.SamplingSuited()) break; 674 707 } 675 708 Context.IncrementEvaluatedSolutions(tries);
Note: See TracChangeset
for help on using the changeset viewer.