Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14556


Ignore:
Timestamp:
01/11/17 01:53:44 (7 years ago)
Author:
abeham
Message:

#2701:

  • fixed bug with breeding
  • fixed permutation sub-space hillclimber
  • improved speed of inversion by calling Array.Reverse(int, int) instead of using a for-loop
  • added delinking for relative-undirected permutations
Location:
branches/MemPRAlgorithm
Files:
8 edited

Legend:

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

    r14552 r14556  
    160160      while (evaluations < N) {
    161161        BinaryVector c = null;
    162         var xochoice = Context.Random.Next(3);
    163         switch (xochoice) {
    164           case 0: c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(1)); break;
    165           case 1: c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(2)); break;
    166           case 2: c = UniformCrossover.Apply(Context.Random, p1.Solution, p2.Solution); break;
    167         }
     162        var xochoice = Context.Random.NextDouble();
     163        if (xochoice < 0.2)
     164          c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(1));
     165        else if (xochoice < 0.5)
     166          c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(2));
     167        else c = UniformCrossover.Apply(Context.Random, p1.Solution, p2.Solution);
    168168        if (cache.Contains(c)) {
    169169          cacheHits++;
    170           if (cacheHits > 10) break;
     170          if (cacheHits > 20) break;
    171171          continue;
    172172        }
     173        probe.Solution = c;
    173174        Context.Evaluate(probe, token);
    174175        evaluations++;
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LinearLinkageMemPR.cs

    r14552 r14556  
    260260          continue;
    261261        }
     262        probe.Solution = c;
    262263        Context.Evaluate(probe, token);
    263264        evaluations++;
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRAlgorithm.cs

    r14552 r14556  
    4343      where TPopulationContext : MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext>, new()
    4444      where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TPopulationContext, TSolutionContext> {
    45     private const double MutationProbabilityMagicConst = 0.1;
    4645
    4746    public override Type ProblemType {
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/ExhaustiveHillClimbSubspace.cs

    r14552 r14556  
    3333  [StorableClass]
    3434  public class ExhaustiveHillClimbSubspace<TContext> : NamedItem, ILocalSearch<TContext>
    35       where TContext : ISingleSolutionHeuristicAlgorithmContext<SingleObjectiveBasicProblem<PermutationEncoding>, Encodings.PermutationEncoding.Permutation>,
     35      where TContext : ISingleSolutionHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, Encodings.PermutationEncoding.Permutation>,
    3636                       IPermutationSubspaceContext, IEvaluationServiceContext<Encodings.PermutationEncoding.Permutation> {
    3737
     
    5252      try {
    5353        var result = Exhaustive.HillClimb(context.Random, context.Solution.Solution, ref quality,
    54           context.Problem.Maximization, context.Evaluate, CancellationToken.None, context.Subspace.Subspace);
     54          context.Maximization, context.Evaluate, CancellationToken.None, context.Subspace.Subspace);
    5555        context.IncrementEvaluatedSolutions(result.Item1);
    5656        context.Iterations = result.Item2;
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive2Opt.cs

    r14552 r14556  
    5555            continue;
    5656
    57           InversionManipulator.Apply(current, opt.Index1, opt.Index2);
     57          current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1);
    5858          var q = eval(current, token);
    5959          evaluations++;
     
    6262            quality = q;
    6363            lastSuccessMove = opt;
    64           } else InversionManipulator.Apply(current, opt.Index1, opt.Index2);
     64          } else current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1);
    6565
    6666          if (token.IsCancellationRequested) {
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/PermutationMemPR.cs

    r14552 r14556  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Runtime.CompilerServices;
    2526using System.Threading;
    2627using HeuristicLab.Algorithms.MemPR.Interfaces;
     
    297298            continue;
    298299
    299           InversionManipulator.Apply(current, opt.Index1, opt.Index2);
     300          current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1);
    300301
    301302          var q = eval(current, token);
     
    338339              bestOfTheRestF = q;
    339340            }
    340 
    341             InversionManipulator.Apply(current, opt.Index1, opt.Index2);
     341           
     342            current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1);
    342343          }
    343344          if (evaluations >= maxEvals) break;
     
    359360            tabu[current[next], current[bestOfTheRest.Index2]] = Math.Min(currentF, tabu[current[next], current[bestOfTheRest.Index2]]);
    360361          }
    361           InversionManipulator.Apply(current, bestOfTheRest.Index1, bestOfTheRest.Index2);
     362          current.Reverse(bestOfTheRest.Index1, bestOfTheRest.Index2 - bestOfTheRest.Index1 + 1);
    362363
    363364          currentF = bestOfTheRestF;
     
    397398      var evaluations = 0;
    398399      ISingleObjectiveSolutionScope<Encodings.PermutationEncoding.Permutation> offspring = null;
     400      var probe = Context.ToScope((Encodings.PermutationEncoding.Permutation)p1.Solution.Clone());
    399401      while (evaluations < p1.Solution.Length) {
    400402        Encodings.PermutationEncoding.Permutation c = null;
     
    410412          continue;
    411413        }
    412         var probe = Context.ToScope(c);
     414        probe.Solution = c;
    413415        Context.Evaluate(probe, token);
    414416        evaluations++;
     
    432434      var evaluations = 0;
    433435      ISingleObjectiveSolutionScope<Encodings.PermutationEncoding.Permutation> offspring = null;
    434       while(evaluations < p1.Solution.Length) {
     436      var probe = Context.ToScope((Encodings.PermutationEncoding.Permutation)p1.Solution.Clone());
     437      while (evaluations < p1.Solution.Length) {
    435438        Encodings.PermutationEncoding.Permutation c = null;
    436439        var xochoice = Context.Random.Next(3);
     
    445448          continue;
    446449        }
    447         var probe = Context.ToScope(c);
     450        probe.Solution = c;
    448451        Context.Evaluate(probe, token);
    449452        evaluations++;
     
    467470      var evaluations = 0;
    468471      ISingleObjectiveSolutionScope<Encodings.PermutationEncoding.Permutation> offspring = null;
    469       while(evaluations <= p1.Solution.Length) {
     472      var probe = Context.ToScope((Encodings.PermutationEncoding.Permutation)p1.Solution.Clone());
     473      while (evaluations <= p1.Solution.Length) {
    470474        Encodings.PermutationEncoding.Permutation c = null;
    471475        var xochoice = Context.Random.Next(3);
     
    480484          continue;
    481485        }
    482         var probe = Context.ToScope(c);
     486        probe.Solution = c;
    483487        Context.Evaluate(probe, token);
    484488        evaluations++;
     
    507511          return RelinkShift(random, p1, p2, eval, token, delink, out best);
    508512        case PermutationTypes.RelativeUndirected:
    509           return RelinkOpt(random, p1, p2, eval, token, delink, out best);
     513          return delink ? DelinkOpt(random, p1, p2, eval, token, out best) : RelinkOpt(random, p1, p2, eval, token, out best);
    510514        default: throw new ArgumentException(string.Format("Unknown permutation type {0}", p1.PermutationType));
    511515      }
     
    684688    }
    685689
    686     public Encodings.PermutationEncoding.Permutation RelinkOpt(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, CancellationToken token, bool delink, out double best) {
     690    public Encodings.PermutationEncoding.Permutation RelinkOpt(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, CancellationToken token, out double best) {
    687691      var maximization = Context.Maximization;
    688692      var evaluations = 0;
     
    777781            var m = bestQueue.Dequeue();
    778782            Opt(child, m.Item1, m.Item2);
    779           }
    780           for (var i = 0; i < child.Length; i++) invChild[child[i]] = i;
     783            for (var i = m.Item1; i <= m.Item2; i++) invChild[child[i]] = i;
     784          }
    781785          if (FitnessComparer.IsBetter(maximization, bestChange, best)) {
    782786            best = bestChange;
     
    797801    }
    798802
     803    public Encodings.PermutationEncoding.Permutation DelinkOpt(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, CancellationToken token, out double best) {
     804      var evaluations = 0;
     805      var child = (Encodings.PermutationEncoding.Permutation)p1.Clone();
     806
     807      best = double.NaN;
     808      Encodings.PermutationEncoding.Permutation bestChild = null;
     809
     810      var invChild = new int[child.Length];
     811      var invP2 = new int[child.Length];
     812      for (var i = 0; i < child.Length; i++) {
     813        invChild[child[i]] = i;
     814        invP2[p2[i]] = i;
     815      }
     816
     817      var order = Enumerable.Range(0, p2.Length).Where(x => IsUndirectedEdge(invP2, child[x], child.GetCircular(x + 1))).Shuffle(Context.Random).ToList();
     818      while (order.Count > 0) {
     819        var idx = order.First();
     820        var bestChange = double.NaN;
     821        var bestIdx = -1;
     822        for (var m = 0; m < p2.Length; m++) {
     823          if (Math.Abs(m - idx) <= 1 || Math.Abs(m - idx) >= p2.Length - 2) continue;
     824          if (m < idx) {
     825            if (IsUndirectedEdge(invP2, child.GetCircular(m - 1), child[idx])
     826              || IsUndirectedEdge(invP2, child[m], child.GetCircular(idx + 1))) continue;
     827            Opt(child, m, idx);
     828            var moveF = eval(child, token);
     829            evaluations++;
     830            if (Context.IsBetter(moveF, bestChange)) {
     831              bestChange = moveF;
     832              bestIdx = m;
     833            }
     834            // undo
     835            Opt(child, m, idx);
     836          } else {
     837            if (IsUndirectedEdge(invP2, child[idx], child[m])
     838              || IsUndirectedEdge(invP2, child.GetCircular(idx + 1), child.GetCircular(m + 1))) continue;
     839            Opt(child, idx + 1, m);
     840            var moveF = eval(child, token);
     841            evaluations++;
     842            if (Context.IsBetter(moveF, bestChange)) {
     843              bestChange = moveF;
     844              bestIdx = m;
     845            }
     846            // undo
     847            Opt(child, idx + 1, m);
     848          }
     849        }
     850        if (bestIdx >= 0) {
     851          if (bestIdx > idx)
     852            Opt(child, idx + 1, bestIdx);
     853          else Opt(child, bestIdx, idx);
     854          for (var i = Math.Min(idx, bestIdx); i <= Math.Max(idx, bestIdx); i++)
     855            invChild[child[i]] = i;
     856
     857          order = Enumerable.Range(0, p2.Length).Where(x => IsUndirectedEdge(invP2, child[x], child.GetCircular(x + 1))).Shuffle(Context.Random).ToList();
     858          if (Context.IsBetter(bestChange, best)) {
     859            best = bestChange;
     860            bestChild = (Encodings.PermutationEncoding.Permutation)child.Clone();
     861          }
     862        }
     863      }
     864
     865      if (bestChild == null) {
     866        best = eval(child, token);
     867        evaluations++;
     868      }
     869      Context.IncrementEvaluatedSolutions(evaluations);
     870
     871      if (VALIDATE && bestChild != null && !bestChild.Validate()) throw new ArgumentException("Delinking produced invalid child");
     872      if (VALIDATE && Dist(child, p2) < 1) throw new InvalidOperationException("Child is not different from p2 after delinking");
     873      return bestChild ?? child;
     874    }
     875
    799876    private static bool IsUndirectedEdge(int[] invP, int a, int b) {
    800877      var d = Math.Abs(invP[a] - invP[b]);
     
    810887    }
    811888
     889    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    812890    private static void Opt(Encodings.PermutationEncoding.Permutation child, int from, int to) {
    813       if (from > to) {
    814         var h = from;
    815         from = to;
    816         to = h;
    817       }
    818       InversionManipulator.Apply(child, from, to);
     891      if (from > to) child.Reverse(to, from - to + 1);
     892      else child.Reverse(from, to - from + 1);
    819893    }
    820894  }
  • branches/MemPRAlgorithm/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/InversionManipulator.cs

    r14185 r14556  
    6060
    6161    public static void Apply(Permutation permutation, int breakPoint1, int breakPoint2) {
    62       for (int i = 0; i <= (breakPoint2 - breakPoint1) / 2; i++) {  // invert permutation between breakpoints
    63         int temp = permutation[breakPoint1 + i];
    64         permutation[breakPoint1 + i] = permutation[breakPoint2 - i];
    65         permutation[breakPoint2 - i] = temp;
    66       }
     62      permutation.Reverse(breakPoint1, breakPoint2 - breakPoint1 + 1);
    6763    }
    6864
  • branches/MemPRAlgorithm/HeuristicLab.Encodings.PermutationEncoding/3.3/Permutation.cs

    r14185 r14556  
    121121    }
    122122
     123    public virtual void Reverse(int startIndex, int length) {
     124      Array.Reverse(array, startIndex, length);
     125    }
     126
    123127    public event EventHandler PermutationTypeChanged;
    124128
Note: See TracChangeset for help on using the changeset viewer.