Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/24/12 15:04:37 (12 years ago)
Author:
jkarder
Message:

#1331:

  • applied some of the changes suggested by ascheibe in comment:32:ticket:1331
  • restructured path relinking and improvement operators and similarity calculators
  • fixed bug in TSPMultipleGuidesPathRelinker
Location:
branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers/TSPImprovementOperator.cs

    r8086 r8319  
    3434  /// An operator that improves traveling salesman solutions.
    3535  /// </summary>
     36  /// <remarks>
     37  /// The operator tries to improve the traveling salesman solution by swapping two randomly chosen edges for a certain number of times.
     38  /// </remarks>
    3639  [Item("TSPImprovementOperator", "An operator that improves traveling salesman solutions.")]
    3740  [StorableClass]
    38   public sealed class TSPImprovementOperator : SingleSuccessorOperator, IImprovementOperator {
     41  public sealed class TSPImprovementOperator : SingleSuccessorOperator, ISingleObjectiveImprovementOperator {
    3942    #region Parameter properties
    4043    public ScopeParameter CurrentScopeParameter {
     
    5053      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    5154    }
    52     public IValueLookupParameter<IItem> TargetParameter {
    53       get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
     55    public IValueLookupParameter<IItem> SolutionParameter {
     56      get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }
    5457    }
    5558    #endregion
     
    8386      Parameters.Add(new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
    8487      Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
    85       Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only."));
     88      Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
    8689      #endregion
    8790    }
     
    9295
    9396    public override IOperation Apply() {
    94       Permutation currSol = CurrentScope.Variables[TargetParameter.ActualName].Value as Permutation;
     97      Permutation currSol = CurrentScope.Variables[SolutionParameter.ActualName].Value as Permutation;
    9598      if (currSol == null)
    9699        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPMultipleGuidesPathRelinker.cs

    r8086 r8319  
    3535  /// An operator that relinks paths between traveling salesman solutions using a multiple guiding strategy.
    3636  /// </summary>
     37  /// <remarks>
     38  /// The operator incrementally changes the initiating solution towards the guiding solution by correcting edges as needed. For each city it choses the best edge from all guiding solutions.
     39  /// </remarks>
    3740  [Item("TSPMultipleGuidesPathRelinker", "An operator that relinks paths between traveling salesman solutions using a multiple guiding strategy.")]
    3841  [StorableClass]
    39   public sealed class TSPMultipleGuidesPathRelinker : PathRelinker {
     42  public sealed class TSPMultipleGuidesPathRelinker : SingleObjectivePathRelinker {
    4043    #region Parameter properties
    4144    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
     
    9396          }
    9497        });
    95         Invert(v1, i, bestCityIndex);
     98        Invert(v1, currCityIndex + 1, bestCityIndex);
    9699        solutions.Add(v1.Clone() as Permutation);
    97100      }
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPPathRelinker.cs

    r7789 r8319  
    3434  /// An operator that relinks paths between traveling salesman solutions.
    3535  /// </summary>
     36  /// <remarks>
     37  /// The operator incrementally assimilates the initiating solution into the guiding solution by correcting edges as needed.
     38  /// </remarks>
    3639  [Item("TSPPathRelinker", "An operator that relinks paths between traveling salesman solutions.")]
    3740  [StorableClass]
    38   public sealed class TSPPathRelinker : PathRelinker {
     41  public sealed class TSPPathRelinker : SingleObjectivePathRelinker {
    3942    [StorableConstructor]
    4043    private TSPPathRelinker(bool deserializing) : base(deserializing) { }
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPSimultaneousPathRelinker.cs

    r7789 r8319  
    3434  /// An operator that relinks paths between traveling salesman solutions starting from both ends.
    3535  /// </summary>
     36  /// <remarks>
     37  /// The operator incrementally assimilates the initiating solution into the guiding solution and vice versa by correcting edges as needed.
     38  /// </remarks>
    3639  [Item("TSPSimultaneousPathRelinker", "An operator that relinks paths between traveling salesman solutions starting from both ends.")]
    3740  [StorableClass]
    38   public sealed class TSPSimultaneousPathRelinker : PathRelinker {
     41  public sealed class TSPSimultaneousPathRelinker : SingleObjectivePathRelinker {
    3942    [StorableConstructor]
    4043    private TSPSimultaneousPathRelinker(bool deserializing) : base(deserializing) { }
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators/TSPSimilarityCalculator.cs

    r8304 r8319  
    3030  /// An operator that performs similarity calculation between two traveling salesman solutions.
    3131  /// </summary>
     32  /// <remarks>
     33  /// The operator calculates the similarity based on the number of edges the two solutions have in common.
     34  /// </remarks>
    3235  [Item("TSPSimilarityCalculator", "An operator that performs similarity calculation between two traveling salesman solutions.")]
    33   public sealed class TSPSimilarityCalculator : SimilarityCalculator {
     36  public sealed class TSPSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
    3437    private TSPSimilarityCalculator(bool deserializing) : base(deserializing) { }
    3538    private TSPSimilarityCalculator(TSPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
     
    4346      if (left == null || right == null)
    4447        throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
    45       if (left == right) return 1.0;
     48      if (left.PermutationType != right.PermutationType)
     49        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different types.");
     50      if (left.Length != right.Length)
     51        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
     52      var comparer = new PermutationEqualityComparer();
     53      if (object.ReferenceEquals(left, right) || comparer.Equals(left, right)) return 1.0;
    4654
     55      switch (left.PermutationType) {
     56        case PermutationTypes.Absolute:
     57          return CalculateAbsolute(left, right);
     58        case PermutationTypes.RelativeDirected:
     59          return CalculateRelativeDirected(left, right);
     60        case PermutationTypes.RelativeUndirected:
     61          return CalculateRelativeUndirected(left, right);
     62        default:
     63          throw new InvalidOperationException("unknown permutation type");
     64      }
     65    }
     66
     67    private static double CalculateAbsolute(Permutation left, Permutation right) {
     68      double similarity = 0.0;
     69      for (int i = 0; i < left.Length && left[i] == right[i]; similarity = ++i) ;
     70      return similarity / left.Length;
     71    }
     72
     73    private static double CalculateRelativeDirected(Permutation left, Permutation right) {
     74      throw new NotImplementedException();
     75    }
     76
     77    private static double CalculateRelativeUndirected(Permutation left, Permutation right) {
    4778      int[,] edges = new int[right.Length, 2];
    4879      for (int i = 0; i < right.Length; i++) {
     
    6192    }
    6293
    63     public override double CalculateIndividualSimilarity(IScope left, IScope right) {
    64       var sol1 = left.Variables[Target].Value as Permutation;
    65       var sol2 = right.Variables[Target].Value as Permutation;
     94    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
     95      var sol1 = leftSolution.Variables[SolutionVariableName].Value as Permutation;
     96      var sol2 = rightSolution.Variables[SolutionVariableName].Value as Permutation;
    6697
    6798      return CalculateSimilarity(sol1, sol2);
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r8086 r8319  
    342342        op.PermutationParameter.Hidden = true;
    343343      }
    344       foreach (IImprovementOperator op in Operators.OfType<IImprovementOperator>()) {
    345         op.TargetParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    346         op.TargetParameter.Hidden = true;
    347       }
    348       foreach (IPathRelinker op in Operators.OfType<IPathRelinker>()) {
     344      foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
     345        op.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     346        op.SolutionParameter.Hidden = true;
     347      }
     348      foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) {
    349349        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    350350        op.ParentsParameter.Hidden = true;
    351351      }
    352       foreach (ISimilarityCalculator op in Operators.OfType<ISimilarityCalculator>()) {
    353         op.Target = SolutionCreator.PermutationParameter.ActualName;
     352      foreach (TSPSimilarityCalculator op in Operators.OfType<TSPSimilarityCalculator>()) {
     353        op.SolutionVariableName = SolutionCreator.PermutationParameter.ActualName;
    354354      }
    355355    }
Note: See TracChangeset for help on using the changeset viewer.