Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/05/16 12:27:19 (8 years ago)
Author:
abeham
Message:

#2701:

  • Worked on MemPR algorithm for permutations
    • Made Evaluate() method mostly thread-safe and moved evaluated solutions counting to caller
  • Removed TSP specific similarity calculator (it is actually not specific to the TSP) and added it to the permutation encoding as HammingSimilarityCalculator
  • Fixed bug in qap basicproblem
Location:
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive1Shift.cs

    r14450 r14453  
    6767        if (lastSuccessMove == null) break;
    6868      }
    69       return Tuple.Create(steps, evaluations);
     69      return Tuple.Create(evaluations, steps);
    7070    }
    7171  }
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive2Opt.cs

    r14450 r14453  
    6464        if (lastSuccessMove == null) break;
    6565      }
    66       return Tuple.Create(steps, evaluations);
     66      return Tuple.Create(evaluations, steps);
    6767    }
    6868  }
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/ExhaustiveSwap2.cs

    r14450 r14453  
    6666        if (lastSuccessMove == null) break;
    6767      }
    68       return Tuple.Create(steps, evaluations);
     68      return Tuple.Create(evaluations, steps);
    6969    }
    7070  }
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/PermutationMemPR.cs

    r14450 r14453  
    154154    }
    155155
    156     public static void TabuWalk(IRandom random, Encodings.PermutationEncoding.Permutation perm, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, ref double quality, int maxIterations = int.MaxValue, bool[,] noTouch = null) {
     156    public void TabuWalk(IRandom random, Encodings.PermutationEncoding.Permutation perm, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, ref double quality, int maxIterations = int.MaxValue, bool[,] noTouch = null) {
    157157      switch (perm.PermutationType) {
    158158        case PermutationTypes.Absolute:
     
    170170    }
    171171
    172     public static void TabuWalkSwap(IRandom random, Encodings.PermutationEncoding.Permutation perm, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, ref double quality, int maxIterations = int.MaxValue, bool[,] noTouch = null) {
    173       if (double.IsNaN(quality)) quality = eval(perm, random);
     172    public void TabuWalkSwap(IRandom random, Encodings.PermutationEncoding.Permutation perm, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, ref double quality, int maxIterations = int.MaxValue, bool[,] noTouch = null) {
     173      var evaluations = 0;
     174      if (double.IsNaN(quality)) {
     175        quality = eval(perm, random);
     176        evaluations++;
     177      }
    174178      Encodings.PermutationEncoding.Permutation bestOfTheWalk = null;
    175179      double bestOfTheWalkF = double.NaN;
     
    177181      var currentF = quality;
    178182      var overallImprovement = false;
    179       //Console.WriteLine("Current    {0}", string.Join(", ", current));
    180183      var tabu = new double[current.Length, current.Length];
    181184      for (var i = 0; i < current.Length; i++) {
     
    191194        Swap2Move bestOfTheRest = null;
    192195        var improved = false;
    193         //LogAlways("TabuWalk ({0}/{2}): {1}   ({3})", iter, currentF, maxIterations, string.Join(", ", current));
    194196        foreach (var swap in ExhaustiveSwap2MoveGenerator.Generate(current).Shuffle(random)) {
    195197          if (noTouch != null && (noTouch[swap.Index1, 0] || noTouch[swap.Index2, 0]))
     
    200202          current[swap.Index2] = h;
    201203          var q = eval(current, random);
     204          evaluations++;
    202205          if (q < quality) {
    203206            overallImprovement = true;
     
    233236        if (!allTabu && !improved) {
    234237          tabu[bestOfTheRest.Index1, current[bestOfTheRest.Index1]] = Math.Min(currentF, tabu[bestOfTheRest.Index1, current[bestOfTheRest.Index1]]);
    235           //LogAlways("Making Tabu [{0},{1}] = {2}", bestOfTheRest.Index1, current[bestOfTheRest.Index1], tabu[bestOfTheRest.Index1, current[bestOfTheRest.Index1]]);
    236238          tabu[bestOfTheRest.Index2, current[bestOfTheRest.Index2]] = Math.Min(currentF, tabu[bestOfTheRest.Index2, current[bestOfTheRest.Index2]]);
    237           //LogAlways("Making Tabu [{0},{1}] = {2}", bestOfTheRest.Index2, current[bestOfTheRest.Index2], tabu[bestOfTheRest.Index2, current[bestOfTheRest.Index2]]);
    238239
    239240          var h = current[bestOfTheRest.Index1];
     
    244245        } else if (allTabu) break;
    245246      }
     247      Context.IncrementEvaluatedSolutions(evaluations);
    246248      if (!overallImprovement && bestOfTheWalk != null) {
    247249        quality = bestOfTheWalkF;
     
    250252    }
    251253
    252     public static void TabuWalkShift(IRandom random, Encodings.PermutationEncoding.Permutation perm, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, ref double quality, int maxIterations = int.MaxValue, bool[,] noTouch = null) {
     254    public void TabuWalkShift(IRandom random, Encodings.PermutationEncoding.Permutation perm, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, ref double quality, int maxIterations = int.MaxValue, bool[,] noTouch = null) {
    253255      return;
    254256    }
    255257
    256     public static void TabuWalkOpt(IRandom random, Encodings.PermutationEncoding.Permutation perm, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, ref double quality, int maxIterations = int.MaxValue, bool[,] noTouch = null) {
    257       if (double.IsNaN(quality)) quality = eval(perm, random);
     258    public void TabuWalkOpt(IRandom random, Encodings.PermutationEncoding.Permutation perm, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, ref double quality, int maxIterations = int.MaxValue, bool[,] noTouch = null) {
     259      var evaluations = 0;
     260      if (double.IsNaN(quality)) {
     261        quality = eval(perm, random);
     262        evaluations++;
     263      }
    258264      Encodings.PermutationEncoding.Permutation bestOfTheWalk = null;
    259265      double bestOfTheWalkF = double.NaN;
     
    287293
    288294          var q = eval(current, random);
     295          evaluations++;
    289296          if (q < quality) {
    290297            overallImprovement = true;
     
    333340        } else if (allTabu) break;
    334341      }
     342      Context.IncrementEvaluatedSolutions(evaluations);
    335343      if (!overallImprovement && bestOfTheWalk != null) {
    336344        quality = bestOfTheWalkF;
     
    456464    }
    457465
    458     public static Encodings.PermutationEncoding.Permutation Relink(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, out double best) {
     466    public Encodings.PermutationEncoding.Permutation Relink(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, out double best) {
    459467      if (p1.PermutationType != p2.PermutationType) throw new ArgumentException(string.Format("Unequal permutation types {0} and {1}", p1.PermutationType, p2.PermutationType));
    460468      switch (p1.PermutationType) {
     
    469477    }
    470478
    471     public static Encodings.PermutationEncoding.Permutation RelinkSwap(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, out double best) {
     479    public Encodings.PermutationEncoding.Permutation RelinkSwap(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, out double best) {
     480      var evaluations = 0;
    472481      var child = (Encodings.PermutationEncoding.Permutation)p1.Clone();
    473482
     
    492501          Swap(child, invChild[p2[idx]], idx);
    493502          var moveF = eval(child, random);
     503          evaluations++;
    494504          if (double.IsNaN(bestChange) || moveF < bestChange) {
    495505            bestChange = moveF;
     
    515525        }
    516526      }
    517       //Log(string.Join(", ", p2));
     527      Context.IncrementEvaluatedSolutions(evaluations);
    518528
    519529      if (VALIDATE && bestChild != null && !bestChild.Validate()) throw new ArgumentException("Relinking produced invalid child");
    520530      if (VALIDATE && Dist(child, p2) > 0) throw new InvalidOperationException("Child is not equal to p2 after relinking");
    521 
     531     
    522532      if (bestChild == null) best = eval(child, random);
    523533      return bestChild ?? child;
    524534    }
    525535
    526     public static Encodings.PermutationEncoding.Permutation RelinkShift(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, out double best) {
     536    public Encodings.PermutationEncoding.Permutation RelinkShift(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, out double best) {
     537      var evaluations = 0;
    527538      var child = (Encodings.PermutationEncoding.Permutation)p1.Clone();
    528539
     
    545556          else Shift(child, from: c, to: n);
    546557          var moveF = eval(child, random);
     558          evaluations++;
    547559          if (double.IsNaN(bestChange) || moveF < bestChange) {
    548560            bestChange = moveF;
     
    564576      } while (!double.IsNaN(bestChange));
    565577
     578      Context.IncrementEvaluatedSolutions(evaluations);
    566579      if (VALIDATE && !bestChild.Validate()) throw new ArgumentException("Relinking produced invalid child");
    567580      if (VALIDATE && Dist(child, p2) > 0) throw new InvalidOperationException("Child is not equal to p2 after relinking");
     
    569582    }
    570583
    571     public static Encodings.PermutationEncoding.Permutation RelinkOpt(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, out double best) {
     584    public Encodings.PermutationEncoding.Permutation RelinkOpt(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, IRandom, double> eval, out double best) {
     585      var evaluations = 0;
    572586      var child = (Encodings.PermutationEncoding.Permutation)p1.Clone();
    573587
     
    646660          }
    647661          var moveF = eval(child, random);
     662          evaluations++;
    648663          if (double.IsNaN(bestChange) || moveF < bestChange) {
    649664            bestChange = moveF;
     
    659674          while (bestQueue.Count > 0) {
    660675            var m = bestQueue.Dequeue();
    661             //Log("Applying opt {0} {1}", m.Item1, m.Item2);
    662             //Log("{0}", string.Join(" ", child));
    663676            Opt(child, m.Item1, m.Item2);
    664             //Log("{0}", string.Join(" ", child));
    665677          }
    666678          for (var i = 0; i < child.Length; i++) invChild[child[i]] = i;
     
    672684      } while (!double.IsNaN(bestChange));
    673685
    674       //Log("{0}", string.Join(" ", p2));
     686      Context.IncrementEvaluatedSolutions(evaluations);
     687     
    675688      if (VALIDATE && !bestChild.Validate()) throw new ArgumentException("Relinking produced invalid child");
    676689      if (VALIDATE && Dist(child, p2) > 0) throw new InvalidOperationException("Child is not equal to p2 after relinking");
     
    681694      var d = Math.Abs(invP[a] - invP[b]);
    682695      return d == 1 || d == invP.Length - 1;
    683     }
    684 
    685     private static void Move(Encodings.PermutationEncoding.Permutation child, int from, int to) {
    686       if (child.PermutationType == PermutationTypes.Absolute) {
    687         // swap
    688         Swap(child, from, to);
    689       } else if (child.PermutationType == PermutationTypes.RelativeDirected) {
    690         // shift
    691         Shift(child, from, to);
    692       } else if (child.PermutationType == PermutationTypes.RelativeUndirected) {
    693         // opt
    694         Opt(child, from, to);
    695       } else throw new ArgumentException(string.Format("Unknown permutation type {0}", child.PermutationType));
    696     }
    697 
    698     private static void Move(PermutationTypes type, bool[] arr, int from, int to) {
    699       switch (type) {
    700         case PermutationTypes.Absolute: {
    701             /*var h = arr[from];
    702             arr[from] = arr[to];
    703             arr[to] = h;*/
    704             arr[from] = false;
    705             arr[to] = false;
    706             break;
    707           }
    708         case PermutationTypes.RelativeDirected: {
    709             var original = (bool[])arr.Clone();
    710             var number = original[from];
    711             int i = 0;  // index in new permutation
    712             int j = 0;  // index in old permutation
    713             while (i < original.Length) {
    714               if (j == from) {
    715                 j++;
    716               }
    717               if (i == to) {
    718                 arr[i] = number;
    719                 i++;
    720               }
    721               if ((i < original.Length) && (j < original.Length)) {
    722                 arr[i] = original[j];
    723                 i++;
    724                 j++;
    725               }
    726             }
    727             break;
    728           }
    729         case PermutationTypes.RelativeUndirected: {
    730             if (from > to) {
    731               var hu = from;
    732               from = to;
    733               to = hu;
    734             }
    735             for (int i = 0; i <= (to - from) / 2; i++) {  // invert permutation between breakpoints
    736               var temp = arr[from + i];
    737               arr[from + i] = arr[to - i];
    738               arr[to - i] = temp;
    739             }
    740             break;
    741           }
    742         default:
    743           throw new ArgumentException(string.Format("Unknown permutation type {0}", type));
    744       }
    745696    }
    746697
Note: See TracChangeset for help on using the changeset viewer.