Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/08/17 22:16:19 (7 years ago)
Author:
abeham
Message:

#2701: refactored breeding, removed hillclimbing after breeding

File:
1 edited

Legend:

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

    r14550 r14551  
    2727using HeuristicLab.Common;
    2828using HeuristicLab.Core;
     29using HeuristicLab.Data;
    2930using HeuristicLab.Encodings.BinaryVectorEncoding;
    3031using HeuristicLab.Optimization;
     
    156157      var evaluations = 0;
    157158      var N = p1.Solution.Length;
    158       if (double.IsNaN(p1.Fitness)) {
    159         Evaluate(p1, token);
    160         evaluations++;
    161       }
    162       if (double.IsNaN(p2.Fitness)) {
    163         Evaluate(p2, token);
    164         evaluations++;
    165       }
    166       var better = Problem.Maximization ? Math.Max(p1.Fitness, p2.Fitness)
    167                                         : Math.Min(p1.Fitness, p2.Fitness);
    168 
     159
     160      var probe = ToScope((BinaryVector)p1.Solution.Clone());
    169161
    170162      var cache = new HashSet<BinaryVector>(new BinaryVectorEqualityComparer());
     
    172164      cache.Add(p2.Solution);
    173165
     166      var cacheHits = 0;
    174167      ISingleObjectiveSolutionScope<BinaryVector> offspring = null;
    175       var probe = ToScope(new BinaryVector(N));
    176       // first try all possible combinations of 1-point crossover
    177       /*var order = Enumerable.Range(1, N - 2).Where(x => p1.Solution[x] != p2.Solution[x]).Shuffle(Context.Random).ToList();
    178       foreach (var b in order) {
    179         for (var i = 0; i <= b; i++)
    180           probe.Solution[i] = p1.Solution[i];
    181         for (var i = b + 1; i < probe.Solution.Length; i++)
    182           probe.Solution[i] = p2.Solution[i];
    183 
    184         // only add to cache, because all solutions must be unique
    185         if (cache.Contains(probe.Solution)) continue;
    186         cache.Add(probe.Solution);
     168      while (evaluations < N) {
     169        BinaryVector c = null;
     170        var xochoice = Context.Random.Next(3);
     171        switch (xochoice) {
     172          case 0: c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(1)); break;
     173          case 1: c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(2)); break;
     174          case 2: c = UniformCrossover.Apply(Context.Random, p1.Solution, p2.Solution); break;
     175        }
     176        if (cache.Contains(c)) {
     177          cacheHits++;
     178          if (cacheHits > 10) break;
     179          continue;
     180        }
    187181        Evaluate(probe, token);
    188182        evaluations++;
     183        cache.Add(c);
    189184        if (offspring == null || Context.IsBetter(probe, offspring)) {
    190           // immediately return upon finding a better offspring than better parent
    191           if (Context.IsBetter(probe.Fitness, better)) {
    192             Context.IncrementEvaluatedSolutions(evaluations);
    193             return probe;
    194           }
    195           offspring = (ISingleObjectiveSolutionScope<BinaryVector>)probe.Clone();
    196         }
    197       }*/
    198 
    199       var cacheHits = 0;
    200       // if we got some evaluations left, try uniform crossover
    201       while (evaluations < Math.Min(Context.LocalSearchEvaluations, N)) {
    202         probe.Solution = UniformCrossover.Apply(Context.Random, p1.Solution, p2.Solution);
    203         if (cache.Contains(probe.Solution)) {
    204           cacheHits++;
    205           if (cacheHits > 10) break; // variability of uniform crossover is too low -> parents are too similar
    206           continue;
    207         } else cache.Add(probe.Solution);
    208         Evaluate(probe, token);
    209         evaluations++;
    210         if (offspring == null || Context.IsBetter(probe, offspring)) {
    211           // immediately return upon finding a better offspring than better parent
    212           if (Context.IsBetter(probe.Fitness, better)) {
    213             Context.IncrementEvaluatedSolutions(evaluations);
    214             return probe;
    215           }
    216           offspring = (ISingleObjectiveSolutionScope<BinaryVector>)probe.Clone();
     185          offspring = probe;
     186          if (Context.IsBetter(offspring, p1) && Context.IsBetter(offspring, p2))
     187            break;
    217188        }
    218189      }
    219190      Context.IncrementEvaluatedSolutions(evaluations);
    220       // return best offspring found
    221191      return offspring ?? probe;
    222192    }
     
    224194    protected override ISingleObjectiveSolutionScope<BinaryVector> Link(ISingleObjectiveSolutionScope<BinaryVector> a, ISingleObjectiveSolutionScope<BinaryVector> b, CancellationToken token, bool delink = false) {
    225195      var evaluations = 0;
    226       if (double.IsNaN(a.Fitness)) {
    227         Evaluate(a, token);
    228         evaluations++;
    229       }
    230       if (double.IsNaN(b.Fitness)) {
    231         Evaluate(b, token);
    232         evaluations++;
    233       }
    234 
    235196      var childScope = (ISingleObjectiveSolutionScope<BinaryVector>)a.Clone();
    236197      var child = childScope.Solution;
Note: See TracChangeset for help on using the changeset viewer.