Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14453


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
Files:
2 added
11 edited

Legend:

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

    r14450 r14453  
    9898
    9999    protected override void TabuWalk(ISingleObjectiveSolutionScope<BinaryVector> scope, int steps, CancellationToken token, ISolutionSubspace<BinaryVector> subspace = null) {
     100      var evaluations = 0;
    100101      var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null;
    101       if (double.IsNaN(scope.Fitness)) Evaluate(scope, token);
     102      if (double.IsNaN(scope.Fitness)) {
     103        Evaluate(scope, token);
     104        evaluations++;
     105      }
    102106      SingleObjectiveSolutionScope<BinaryVector> bestOfTheWalk = null;
    103107      var currentScope = (SingleObjectiveSolutionScope<BinaryVector>)scope.Clone();
     
    121125          current[idx] = !current[idx];
    122126          Evaluate(currentScope, token);
     127          evaluations++;
    123128          var after = currentScope.Fitness;
    124129
     
    151156      }
    152157
     158      Context.IncrementEvaluatedSolutions(evaluations);
    153159      scope.Adopt(bestOfTheWalk ?? currentScope);
    154160    }
     
    187193
    188194    protected override ISingleObjectiveSolutionScope<BinaryVector> Relink(ISingleObjectiveSolutionScope<BinaryVector> a, ISingleObjectiveSolutionScope<BinaryVector> b, CancellationToken token) {
    189       if (double.IsNaN(a.Fitness)) Evaluate(a, token);
    190       if (double.IsNaN(b.Fitness)) Evaluate(b, token);
     195      if (double.IsNaN(a.Fitness)) {
     196        Evaluate(a, token);
     197        Context.IncrementEvaluatedSolutions(1);
     198      }
     199      if (double.IsNaN(b.Fitness)) {
     200        Evaluate(b, token);
     201        Context.IncrementEvaluatedSolutions(1);
     202      }
    191203      if (Context.Random.NextDouble() < 0.5)
    192204        return IsBetter(a, b) ? Relink(a, b, token, false) : Relink(b, a, token, true);
     
    195207
    196208    protected virtual ISingleObjectiveSolutionScope<BinaryVector> Relink(ISingleObjectiveSolutionScope<BinaryVector> betterScope, ISingleObjectiveSolutionScope<BinaryVector> worseScope, CancellationToken token, bool fromWorseToBetter) {
     209      var evaluations = 0;
    197210      var childScope = (ISingleObjectiveSolutionScope<BinaryVector>)(fromWorseToBetter ? worseScope : betterScope).Clone();
    198211      var child = childScope.Solution;
     
    213226          child[idx] = !child[idx]; // move
    214227          Evaluate(childScope, token);
     228          evaluations++;
    215229          var s = childScope.Fitness;
    216230          childScope.Fitness = cF;
     
    236250        }
    237251      }
     252      Context.IncrementEvaluatedSolutions(evaluations);
    238253      return best ?? childScope;
    239254    }
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRAlgorithm.cs

    r14450 r14453  
    252252      var p1 = Context.AtPopulation(i1);
    253253      var p2 = Context.AtPopulation(i2);
    254 
    255       if (double.IsNaN(p1.Fitness)) Evaluate(p1, token);
    256       if (double.IsNaN(p2.Fitness)) Evaluate(p2, token);
    257254
    258255      var parentDist = Dist(p1, p2);
     
    361358
    362359    protected int Replace(ISingleObjectiveSolutionScope<TSolution> child, CancellationToken token) {
    363       if (double.IsNaN(child.Fitness)) Evaluate(child, token);
     360      if (double.IsNaN(child.Fitness)) {
     361        Evaluate(child, token);
     362        Context.IncrementEvaluatedSolutions(1);
     363      }
     364      if (IsBetter(child.Fitness, Context.BestQuality)) {
     365        Context.BestQuality = child.Fitness;
     366        Context.BestSolution = (TSolution)child.Solution.Clone();
     367      }
    364368
    365369      var popSize = MaximumPopulationSize;
     
    460464    protected abstract ISolutionSubspace<TSolution> CalculateSubspace(IEnumerable<TSolution> solutions, bool inverse = false);
    461465    protected virtual void Evaluate(ISingleObjectiveSolutionScope<TSolution> scope, CancellationToken token) {
    462       Context.EvaluatedSolutions++;
    463466      var prob = Problem as ISingleObjectiveProblemDefinition;
    464467      if (prob != null) {
     
    466469        scope.Fitness = prob.Evaluate(ind, Context.Random);
    467470      } else RunOperator(Problem.Evaluator, scope, token);
    468       if (IsBetter(scope.Fitness, Context.BestQuality))
    469         Context.BestQuality = scope.Fitness;
    470471    }
    471472
     
    480481    #region Improve
    481482    protected virtual int HillClimb(ISingleObjectiveSolutionScope<TSolution> scope, CancellationToken token, ISolutionSubspace<TSolution> subspace = null) {
    482       if (double.IsNaN(scope.Fitness)) Evaluate(scope, token);
     483      if (double.IsNaN(scope.Fitness)) {
     484        Evaluate(scope, token);
     485        Context.IncrementEvaluatedSolutions(1);
     486      }
    483487      var before = scope.Fitness;
    484488      var lscontext = Context.CreateSingleSolutionContext(scope);
     
    486490      var after = scope.Fitness;
    487491      Context.HillclimbingStat.Add(Tuple.Create(before, after));
     492      Context.IncrementEvaluatedSolutions(lscontext.EvaluatedSolutions);
    488493      return lscontext.Iterations;
    489494    }
    490495
    491496    protected virtual void PerformTabuWalk(ISingleObjectiveSolutionScope<TSolution> scope, int steps, CancellationToken token, ISolutionSubspace<TSolution> subspace = null) {
    492       if (double.IsNaN(scope.Fitness)) Evaluate(scope, token);
     497      if (double.IsNaN(scope.Fitness)) {
     498        Evaluate(scope, token);
     499        Context.IncrementEvaluatedSolutions(1);
     500      }
    493501      var before = scope.Fitness;
    494502      var newScope = (ISingleObjectiveSolutionScope<TSolution>)scope.Clone();
     
    500508    protected abstract void TabuWalk(ISingleObjectiveSolutionScope<TSolution> scope, int steps, CancellationToken token, ISolutionSubspace<TSolution> subspace = null);
    501509    protected virtual void TabuClimb(ISingleObjectiveSolutionScope<TSolution> scope, int steps, CancellationToken token, ISolutionSubspace<TSolution> subspace = null) {
    502       if (double.IsNaN(scope.Fitness)) Evaluate(scope, token);
     510      if (double.IsNaN(scope.Fitness)) {
     511        Evaluate(scope, token);
     512        Context.IncrementEvaluatedSolutions(1);
     513      }
    503514      var before = scope.Fitness;
    504515      var newScope = (ISingleObjectiveSolutionScope<TSolution>)scope.Clone();
     
    520531      var p2 = Context.AtPopulation(i2);
    521532
    522       if (double.IsNaN(p1.Fitness)) Evaluate(p1, token);
    523       if (double.IsNaN(p2.Fitness)) Evaluate(p2, token);
     533      if (double.IsNaN(p1.Fitness)) {
     534        Evaluate(p1, token);
     535        Context.IncrementEvaluatedSolutions(1);
     536      }
     537      if (double.IsNaN(p2.Fitness)) {
     538        Evaluate(p2, token);
     539        Context.IncrementEvaluatedSolutions(1);
     540      }
    524541
    525542      return BreedAndImprove(p1, p2, token);
     
    532549        Mutate(offspring, token, subspace); // mutate the solutions, especially to widen the sub-space
    533550      }
    534       if (double.IsNaN(offspring.Fitness)) Evaluate(offspring, token);
     551      if (double.IsNaN(offspring.Fitness)) {
     552        Evaluate(offspring, token);
     553        Context.IncrementEvaluatedSolutions(1);
     554      }
    535555      Context.BreedingStat.Add(Tuple.Create(p1.Fitness, p2.Fitness, offspring.Fitness));
    536556      if ((IsBetter(offspring, p1) && IsBetter(offspring, p2))
     
    556576      var p2 = Context.AtPopulation(i2);
    557577
    558       if (double.IsNaN(p1.Fitness)) Evaluate(p1, token);
    559       if (double.IsNaN(p2.Fitness)) Evaluate(p2, token);
    560 
    561578      return RelinkAndImprove(p1, p2, token);
    562579    }
     
    584601      SolutionModelTrainerParameter.Value.TrainModel(Context);
    585602      var sample = ToScope(Context.Model.Sample());
     603      Evaluate(sample, token);
     604      Context.IncrementEvaluatedSolutions(1);
    586605      if (Context.Population.Any(p => IsBetter(sample, p) || sample.Fitness == p.Fitness)) return sample;
    587606
     
    608627
    609628    protected double ProbabilityAccept(ISingleObjectiveSolutionScope<TSolution> scope, IList<Tuple<double, double>> data) {
    610       if (double.IsNaN(scope.Fitness)) Evaluate(scope, CancellationToken.None);
     629      if (double.IsNaN(scope.Fitness)) {
     630        Evaluate(scope, CancellationToken.None);
     631        Context.IncrementEvaluatedSolutions(1);
     632      }
    611633      return ProbabilityAccept(scope.Fitness, data);
    612634    }
  • 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
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/SingleObjectiveSolutionScope.cs

    r14450 r14453  
    2929
    3030namespace HeuristicLab.Algorithms.MemPR {
    31   [Item("MemPRScope", "Scope for a MemPR individual.")]
     31  [Item("Solution Scope", "Scope for an individual/solution of a single-objective single-encoded problem.")]
    3232  [StorableClass]
    3333  public sealed class SingleObjectiveSolutionScope<T> : NamedItem, ISingleObjectiveSolutionScope<T> where T : class, IItem {
  • branches/MemPRAlgorithm/HeuristicLab.Encodings.PermutationEncoding/3.3/HeuristicLab.Encodings.PermutationEncoding-3.3.csproj

    r11961 r14453  
    203203    <Compile Include="Properties\AssemblyInfo.cs" />
    204204    <Compile Include="ShakingOperators\PermutationShakingOperator.cs" />
     205    <Compile Include="SimilarityCalculators\HammingSimilarityCalculator.cs" />
    205206  </ItemGroup>
    206207  <ItemGroup>
  • branches/MemPRAlgorithm/HeuristicLab.Problems.QuadraticAssignment/3.3/QAPBasicProblem.cs

    r14451 r14453  
    2020#endregion
    2121
     22using System.Linq;
     23using HeuristicLab.Analysis;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
     
    2527using HeuristicLab.Encodings.PermutationEncoding;
    2628using HeuristicLab.Optimization;
     29using HeuristicLab.Optimization.Operators;
    2730using HeuristicLab.Parameters;
    2831using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3033
    3134namespace HeuristicLab.Problems.QuadraticAssignment {
    32   [Item("Basic Quadratic Assignment Problem (BQAP)", "The Quadratic Assignment Problem (QAP) can be described as the problem of assigning N facilities to N fixed locations such that there is exactly one facility in each location and that the sum of the distances multiplied by the connection strength between the facilities becomes minimal.")]
     35  [Item("Basic Quadratic Assignment Problem (QAP)", "The Quadratic Assignment Problem (QAP) can be described as the problem of assigning N facilities to N fixed locations such that there is exactly one facility in each location and that the sum of the distances multiplied by the connection strength between the facilities becomes minimal.")]
    3336  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 141)]
    3437  [StorableClass]
     
    6265      Parameters.Add(weightsParameter = new ValueParameter<DoubleMatrix>("Weights", "The weights matrix.", new DoubleMatrix(5, 5)));
    6366      Parameters.Add(distancesParameter = new ValueParameter<DoubleMatrix>("Distances", "The distances matrix.", new DoubleMatrix(5, 5)));
     67
     68      Operators.Add(new HammingSimilarityCalculator());
     69      Operators.Add(new QualitySimilarityCalculator());
     70      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
     71
     72      Parameterize();
    6473    }
    6574
    6675    public override IDeepCloneable Clone(Cloner cloner) {
    6776      return new QAPBasicProblem(this, cloner);
     77    }
     78
     79    protected override void OnEncodingChanged() {
     80      base.OnEncodingChanged();
     81      Parameterize();
     82    }
     83
     84    private void Parameterize() {
     85      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
     86        similarityCalculator.SolutionVariableName = Encoding.SolutionCreator.PermutationParameter.ActualName;
     87        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
     88      }
    6889    }
    6990
     
    116137      Weights = weights;
    117138      Distances = distances;
     139      Encoding.Length = Weights.Rows;
    118140
    119141      BestKnownQuality = double.NaN;
  • branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators/TSPSimilarityCalculator.cs

    r14185 r14453  
    2525using HeuristicLab.Encodings.PermutationEncoding;
    2626using HeuristicLab.Optimization.Operators;
     27using HeuristicLab.PluginInfrastructure;
    2728
    2829namespace HeuristicLab.Problems.TravelingSalesman {
     
    3435  /// </remarks>
    3536  [Item("TSPSimilarityCalculator", "An operator that performs similarity calculation between two traveling salesman solutions. The operator calculates the similarity based on the number of edges the two solutions have in common.")]
    36   public sealed class TSPSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
     37  [Obsolete("Please use the HammingSimilarityCalculator in the Encodings.PermutationEncoding plugin.")]
     38  [NonDiscoverableType]
     39  internal sealed class TSPSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
    3740    protected override bool IsCommutative { get { return true; } }
    3841
  • branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r14185 r14453  
    233233      Operators.Add(new TSPPathRelinker());
    234234      Operators.Add(new TSPSimultaneousPathRelinker());
    235       Operators.Add(new TSPSimilarityCalculator());
     235      Operators.Add(new HammingSimilarityCalculator());
    236236      Operators.Add(new QualitySimilarityCalculator());
    237       Operators.Add(new NoSimilarityCalculator());
    238237
    239238      Operators.Add(new BestTSPSolutionAnalyzer());
Note: See TracChangeset for help on using the changeset viewer.