Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/05/17 17:50:03 (8 years ago)
Author:
abeham
Message:

#2701:

  • Added BinaryVectorEqualityComparer (identical to the one in the P3 plugin) to binaryvector plugin
  • Added delinking for absolute coded permutations
  • Some tweaks
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRAlgorithm.cs

    r14544 r14550  
    230230          var child = Create(token);
    231231          Context.LocalSearchEvaluations += HillClimb(child, token);
     232          Context.LocalOptimaLevel += child.Fitness;
    232233          Context.AddToPopulation(child);
    233234          Context.BestQuality = child.Fitness;
     
    237238        }
    238239        Context.LocalSearchEvaluations /= 2;
     240        Context.LocalOptimaLevel /= 2;
    239241        Context.Initialized = true;
    240242      }
     
    254256      if (offspring != null) {
    255257        if (Context.PopulationCount < MaximumPopulationSize)
    256           HillClimb(offspring, token);
     258          AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token);
    257259        var replNew = Replace(offspring, token);
    258260        if (replNew) {
     
    265267      if (offspring != null) {
    266268        if (Context.PopulationCount < MaximumPopulationSize)
    267           HillClimb(offspring, token);
     269          AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token);
    268270        if (Replace(offspring, token)) {
    269271          replaced = true;
     
    275277      if (offspring != null) {
    276278        if (Context.PopulationCount < MaximumPopulationSize)
    277           HillClimb(offspring, token);
     279          AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token);
    278280        if (Replace(offspring, token)) {
    279281          replaced = true;
     
    285287      if (offspring != null) {
    286288        if (Context.PopulationCount < MaximumPopulationSize)
    287           HillClimb(offspring, token);
     289          AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token);
    288290        if (Replace(offspring, token)) {
    289291          replaced = true;
     
    294296      if (!replaced && offspring != null) {
    295297        if (Context.HillclimbingSuited(offspring)) {
    296           HillClimb(offspring, token);
     298          AdaptiveClimb(offspring, Context.LocalSearchEvaluations, token);
    297299          if (Replace(offspring, token)) {
    298300            Context.ByHillclimbing++;
     
    516518      return false;// -1;
    517519    }
    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);
    520525    protected abstract double Dist(ISingleObjectiveSolutionScope<TSolution> a, ISingleObjectiveSolutionScope<TSolution> b);
    521526    protected abstract ISingleObjectiveSolutionScope<TSolution> ToScope(TSolution code, double fitness = double.NaN);
     
    599604        if (Context.Population.All(p => Context.IsBetter(offspring, p)))
    600605          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));
    602608
    603609        Context.AddBreedingResult(p1, p2, offspring);
     
    620626      var p2 = Context.AtPopulation(i2);
    621627
    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;
    623642    }
    624643
     
    630649      var p1 = Context.AtPopulation(i1);
    631650      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;
    634666    }
    635667
     
    661693    #region Sample
    662694    protected virtual ISingleObjectiveSolutionScope<TSolution> Sample(CancellationToken token) {
    663       if (Context.PopulationCount == MaximumPopulationSize && Context.SamplingSuited()) {
     695      if (Context.PopulationCount == MaximumPopulationSize) {
    664696        SolutionModelTrainerParameter.Value.TrainModel(Context);
    665697        ISingleObjectiveSolutionScope<TSolution> bestSample = null;
    666698        var tries = 1;
    667         for (; tries < Context.LocalSearchEvaluations; tries++) {
     699        for (; tries < 100; tries++) {
    668700          var sample = ToScope(Context.Model.Sample());
    669701          Evaluate(sample, token);
    670702          if (bestSample == null || Context.IsBetter(sample, bestSample)) {
    671703            bestSample = sample;
     704            if (Context.Population.Any(x => !Context.IsBetter(x, bestSample))) break;
    672705          }
    673           if (Context.Population.Any(x => !Context.IsBetter(x, bestSample))) break;
     706          if (!Context.SamplingSuited()) break;
    674707        }
    675708        Context.IncrementEvaluatedSolutions(tries);
Note: See TracChangeset for help on using the changeset viewer.