Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/22/12 11:11:38 (12 years ago)
Author:
jkarder
Message:

#1331:

  • synced branch with trunk
  • added custom interface (ISimilarityBasedOperator) to mark operators that conduct similarity calculation
  • similarity calculators are now parameterized by the algorithm
  • deleted SolutionPool2TierUpdateMethod
  • deleted KnapsackMultipleGuidesPathRelinker
  • moved IImprovementOperator, IPathRelinker and ISimilarityCalculator to HeuristicLab.Optimization
  • added parameter descriptions
  • fixed plugin references
  • fixed count of EvaluatedSolutions
  • fixed check for duplicate solutions
  • minor code improvements
Location:
branches/ScatterSearch (trunk integration)
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch (trunk integration)

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

    r7789 r8086  
    2727using HeuristicLab.Operators;
    2828using HeuristicLab.Optimization;
    29 using HeuristicLab.Optimization.Operators;
    3029using HeuristicLab.Parameters;
    3130using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4443    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
    4544      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
    46     }
    47     public ILookupParameter<IEvaluator> EvaluatorParameter {
    48       get { return (ILookupParameter<IEvaluator>)Parameters["Evaluator"]; }
    4945    }
    5046    public IValueParameter<IntValue> ImprovementAttemptsParameter {
     
    6763      set { DistanceMatrixParameter.ActualValue = value; }
    6864    }
    69     public IEvaluator Evaluator {
    70       get { return EvaluatorParameter.ActualValue; }
    71       set { EvaluatorParameter.ActualValue = value; }
    72     }
    7365    public IntValue ImprovementAttempts {
    7466      get { return ImprovementAttemptsParameter.Value; }
     
    7870      get { return RandomParameter.ActualValue; }
    7971      set { RandomParameter.ActualValue = value; }
    80     }
    81     private IItem Target {
    82       get { return TargetParameter.ActualValue; }
    8372    }
    8473    #endregion
     
    9079      : base() {
    9180      #region Create parameters
    92       Parameters.Add(new ScopeParameter("CurrentScope"));
    93       Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix"));
    94       Parameters.Add(new LookupParameter<IEvaluator>("Evaluator"));
    95       Parameters.Add(new ValueParameter<IntValue>("ImprovementAttempts", new IntValue(100)));
    96       Parameters.Add(new LookupParameter<IRandom>("Random"));
    97       Parameters.Add(new ValueLookupParameter<IItem>("Target"));
     81      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
     82      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
     83      Parameters.Add(new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
     84      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."));
    9886      #endregion
    9987    }
     
    10593    public override IOperation Apply() {
    10694      Permutation currSol = CurrentScope.Variables[TargetParameter.ActualName].Value as Permutation;
     95      if (currSol == null)
     96        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
    10797      if (currSol.PermutationType != PermutationTypes.RelativeUndirected)
    10898        throw new ArgumentException("Cannot improve solution because the permutation type is not supported.");
     
    118108          Invert(currSol, a, b);
    119109      }
     110
     111      CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", ImprovementAttempts));
    120112
    121113      return base.Apply();
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPMultipleGuidesPathRelinker.cs

    r7789 r8086  
    4747    public DistanceMatrix DistanceMatrix {
    4848      get { return DistanceMatrixParameter.ActualValue; }
    49       set { DistanceMatrixParameter.ActualValue = value; }
    5049    }
    5150    #endregion
     
    5756      : base() {
    5857      #region Create parameters
    59       Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix"));
     58      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
    6059      #endregion
    6160    }
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/Plugin.cs.frame

    r7558 r8086  
    3737  [PluginDependency("HeuristicLab.Operators", "3.3")]
    3838  [PluginDependency("HeuristicLab.Optimization", "3.3")]
     39  [PluginDependency("HeuristicLab.Optimization.Operators", "3.3")]
    3940  [PluginDependency("HeuristicLab.Parameters", "3.3")]
    4041  [PluginDependency("HeuristicLab.Persistence", "3.3")]
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators/TSPSimilarityCalculator.cs

    r7793 r8086  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Encodings.PermutationEncoding;
    2526using HeuristicLab.Optimization.Operators;
    26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Problems.TravelingSalesman {
     
    3131  /// </summary>
    3232  [Item("TSPSimilarityCalculator", "An operator that performs similarity calculation between two traveling salesman solutions.")]
    33   [StorableClass]
    3433  public sealed class TSPSimilarityCalculator : SimilarityCalculator {
    35     [StorableConstructor]
    3634    private TSPSimilarityCalculator(bool deserializing) : base(deserializing) { }
    3735    private TSPSimilarityCalculator(TSPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
    3836    public TSPSimilarityCalculator() : base() { }
    3937
    40     public override IDeepCloneable Clone(Cloner cloner) {
    41       return new TSPSimilarityCalculator(this, cloner);
     38    public static double CalculateSimilarity(Permutation left, Permutation right) {
     39      if (left == null || right == null)
     40        throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
     41
     42      int[,] edges = new int[right.Length, 2];
     43      for (int i = 0; i < right.Length; i++) {
     44        edges[right[i], 0] = right[(i + 1) % right.Length];
     45        edges[right[i], 1] = right[(i - 1 + right.Length) % right.Length];
     46      }
     47
     48      double similarity = 0.0;
     49      for (int i = 0; i < left.Length; i++) {
     50        int targetCity = left[(i + 1) % left.Length];
     51        if (targetCity == edges[left[i], 0] || targetCity == edges[left[i], 1])
     52          similarity++;
     53      }
     54
     55      return similarity / left.Length;
    4256    }
    4357
    4458    protected override double CalculateSimilarity(IScope left, IScope right) {
    45       Permutation v1 = left.Variables[TargetParameter.ActualName].Value as Permutation;
    46       Permutation v2 = right.Variables[TargetParameter.ActualName].Value as Permutation;
     59      Permutation sol1 = left.Variables[Target].Value as Permutation;
     60      Permutation sol2 = right.Variables[Target].Value as Permutation;
    4761
    48       int[,] edges = new int[v2.Length, 2];
    49       for (int i = 0; i < v2.Length; i++) {
    50         edges[v2[i], 0] = v2[(i + 1) % v2.Length];
    51         edges[v2[i], 1] = v2[(i - 1 + v2.Length) % v2.Length];
    52       }
    53 
    54       double similarity = 0.0;
    55       for (int i = 0; i < v1.Length; i++) {
    56         int targetCity = v1[(i + 1) % v1.Length];
    57         if (targetCity == edges[v1[i], 0] || targetCity == edges[v1[i], 1])
    58           similarity++;
    59       }
    60 
    61       return similarity / v1.Length;
     62      return CalculateSimilarity(sol1, sol2);
    6263    }
    6364  }
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r7789 r8086  
    2929using HeuristicLab.Encodings.PermutationEncoding;
    3030using HeuristicLab.Optimization;
    31 using HeuristicLab.Optimization.Operators;
    3231using HeuristicLab.Parameters;
    3332using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    352351      }
    353352      foreach (ISimilarityCalculator op in Operators.OfType<ISimilarityCalculator>()) {
    354         op.TargetParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    355         op.TargetParameter.Hidden = true;
     353        op.Target = SolutionCreator.PermutationParameter.ActualName;
    356354      }
    357355    }
     
    366364      if (data.Coordinates == null && data.Distances == null)
    367365        throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
    368       if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == TSPDistanceMeasure.Att
    369         || data.DistanceMeasure == TSPDistanceMeasure.Manhattan
    370         || data.DistanceMeasure == TSPDistanceMeasure.Maximum
    371         || data.DistanceMeasure == TSPDistanceMeasure.UpperEuclidean))
     366      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
     367        || data.DistanceMeasure == DistanceMeasure.Manhattan
     368        || data.DistanceMeasure == DistanceMeasure.Maximum
     369        || data.DistanceMeasure == DistanceMeasure.UpperEuclidean))
    372370        throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
    373371      if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
     
    382380
    383381      TSPEvaluator evaluator;
    384       if (data.DistanceMeasure == TSPDistanceMeasure.Att
    385         || data.DistanceMeasure == TSPDistanceMeasure.Manhattan
    386         || data.DistanceMeasure == TSPDistanceMeasure.Maximum
    387         || data.DistanceMeasure == TSPDistanceMeasure.UpperEuclidean) {
     382      if (data.DistanceMeasure == DistanceMeasure.Att
     383        || data.DistanceMeasure == DistanceMeasure.Manhattan
     384        || data.DistanceMeasure == DistanceMeasure.Maximum
     385        || data.DistanceMeasure == DistanceMeasure.UpperEuclidean) {
    388386        evaluator = new TSPDistanceMatrixEvaluator();
    389387        UseDistanceMatrix = new BoolValue(true);
    390388        DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
    391       } else if (data.DistanceMeasure == TSPDistanceMeasure.Direct && data.Distances != null) {
     389      } else if (data.DistanceMeasure == DistanceMeasure.Direct && data.Distances != null) {
    392390        evaluator = new TSPDistanceMatrixEvaluator();
    393391        UseDistanceMatrix = new BoolValue(true);
     
    397395        UseDistanceMatrix = new BoolValue(data.Dimension <= DistanceMatrixSizeLimit);
    398396        switch (data.DistanceMeasure) {
    399           case TSPDistanceMeasure.Euclidean:
     397          case DistanceMeasure.Euclidean:
    400398            evaluator = new TSPEuclideanPathEvaluator();
    401399            break;
    402           case TSPDistanceMeasure.RoundedEuclidean:
     400          case DistanceMeasure.RoundedEuclidean:
    403401            evaluator = new TSPRoundedEuclideanPathEvaluator();
    404402            break;
    405           case TSPDistanceMeasure.Geo:
     403          case DistanceMeasure.Geo:
    406404            evaluator = new TSPGeoPathEvaluator();
    407405            break;
Note: See TracChangeset for help on using the changeset viewer.