Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/05/16 16:06:18 (8 years ago)
Author:
abeham
Message:

#2701:

  • Worked on MemPR algorithm for permutations
  • Refactored TSP
Location:
branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators
Files:
8 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPMoveEvaluator.cs

    r14185 r14454  
    3636  [StorableClass]
    3737  public abstract class TSPMoveEvaluator : SingleSuccessorOperator, ITSPMoveEvaluator, IMoveOperator {
    38 
    39     public abstract Type EvaluatorType { get; }
     38   
    4039    public override bool CanChangeName {
    4140      get { return false; }
  • branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPPathMoveEvaluator.cs

    r14185 r14454  
    4747      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
    4848    }
     49    public ILookupParameter<EnumValue<TSPDistanceFunction>> DistanceFunctionParameter {
     50      get { return (ILookupParameter<EnumValue<TSPDistanceFunction>>)Parameters["DistanceFunction"]; }
     51    }
    4952
    5053    [StorableConstructor]
     
    5760      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
    5861      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated (if it does not exist already) and used for evaluation, otherwise false."));
    59     }
    60 
    61     [StorableHook(HookType.AfterDeserialization)]
    62     private void AfterDeserialization() {
    63       // BackwardsCompatibility3.3
    64       #region Backwards compatible code (remove with 3.4)
    65       LookupParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as LookupParameter<DoubleMatrix>;
    66       if (oldDistanceMatrixParameter != null) {
    67         Parameters.Remove(oldDistanceMatrixParameter);
    68         Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
    69         DistanceMatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName;
    70       }
    71       #endregion
     62      Parameters.Add(new LookupParameter<EnumValue<TSPDistanceFunction>>("DistanceFunction", "The distance function to use when the distance matrix is not being used."));
    7263    }
    7364
    7465    public override IOperation Apply() {
    75       Permutation permutation = PermutationParameter.ActualValue;
    76       DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
     66      var permutation = PermutationParameter.ActualValue;
     67      var coordinates = CoordinatesParameter.ActualValue;
     68      var distanceFunction = DistanceFunctionParameter.ActualValue.Value;
    7769      double relativeQualityDifference = 0;
    7870      if (UseDistanceMatrixParameter.ActualValue.Value) {
    7971        DistanceMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
    8072        if (distanceMatrix == null) {
    81           if (coordinates == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
    82           distanceMatrix = CalculateDistanceMatrix(coordinates);
    83           DistanceMatrixParameter.ActualValue = distanceMatrix;
     73          throw new InvalidOperationException("Distance matrix is not given.");
    8474        }
    8575        relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix);
    8676      } else {
    8777        if (coordinates == null) throw new InvalidOperationException("No coordinates were given.");
    88         relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates);
     78        relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates, distanceFunction);
    8979      }
    9080      DoubleValue moveQuality = MoveQualityParameter.ActualValue;
     
    9383      return base.Apply();
    9484    }
    95 
    96     protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);
    9785    protected abstract double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix);
    98     protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates);
    99 
    100     private DistanceMatrix CalculateDistanceMatrix(DoubleMatrix c) {
    101       DistanceMatrix distanceMatrix = new DistanceMatrix(c.Rows, c.Rows);
    102       for (int i = 0; i < distanceMatrix.Rows; i++) {
    103         for (int j = 0; j < distanceMatrix.Columns; j++)
    104           distanceMatrix[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
    105       }
    106       return (DistanceMatrix)distanceMatrix.AsReadOnly();
    107     }
     86    protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction);
    10887  }
    10988}
  • branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPTranslocationMovePathEvaluator.cs

    r14185 r14454  
    4646    }
    4747
    48     public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, TSPTranslocationMovePathEvaluator evaluator) {
     48    public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) {
    4949      if (move.Index1 == move.Index3
    5050        || move.Index2 == permutation.Length - 1 && move.Index3 == 0
     
    6565      double moveQuality = 0;
    6666      // remove three edges
    67       moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
     67      moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1],
    6868        coordinates[edge1target, 0], coordinates[edge1target, 1]);
    69       moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
     69      moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge2source, 0], coordinates[edge2source, 1],
    7070        coordinates[edge2target, 0], coordinates[edge2target, 1]);
    71       moveQuality -= evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
     71      moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge3source, 0], coordinates[edge3source, 1],
    7272        coordinates[edge3target, 0], coordinates[edge3target, 1]);
    7373      // add three edges
    74       moveQuality += evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
     74      moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge3source, 0], coordinates[edge3source, 1],
    7575        coordinates[edge1target, 0], coordinates[edge1target, 1]);
    76       moveQuality += evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
     76      moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge2source, 0], coordinates[edge2source, 1],
    7777        coordinates[edge3target, 0], coordinates[edge3target, 1]);
    78       moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
     78      moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1],
    7979        coordinates[edge2target, 0], coordinates[edge2target, 1]);
    8080      return moveQuality;
     
    110110    }
    111111
    112     protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
    113       return EvaluateByCoordinates(permutation, TranslocationMoveParameter.ActualValue, coordinates, this);
     112    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) {
     113      return EvaluateByCoordinates(permutation, TranslocationMoveParameter.ActualValue, coordinates, distanceFunction);
    114114    }
    115115
  • branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPInversionMovePathEvaluator.cs

    r14185 r14454  
    3333  [Item("TSPInversionMovePathEvaluator", "Evaluates an inversion move (2-opt) by summing up the length of all added edges and subtracting the length of all deleted edges.")]
    3434  [StorableClass]
    35   public abstract class TSPInversionMovePathEvaluator : TSPPathMoveEvaluator, IPermutationInversionMoveOperator {
     35  public class TSPInversionMovePathEvaluator : TSPPathMoveEvaluator, IPermutationInversionMoveOperator {
    3636    public ILookupParameter<InversionMove> InversionMoveParameter {
    3737      get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
     
    4646    }
    4747
    48     public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, TSPInversionMovePathEvaluator evaluator) {
     48    public override IDeepCloneable Clone(Cloner cloner) {
     49      return new TSPInversionMovePathEvaluator(this, cloner);
     50    }
     51
     52    public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) {
    4953      int edge1source = permutation.GetCircular(move.Index1 - 1);
    5054      int edge1target = permutation[move.Index1];
     
    5458      double moveQuality = 0;
    5559      // remove two edges
    56       moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
     60      moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1],
    5761            coordinates[edge1target, 0], coordinates[edge1target, 1]);
    58       moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
     62      moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge2source, 0], coordinates[edge2source, 1],
    5963        coordinates[edge2target, 0], coordinates[edge2target, 1]);
    6064      // add two edges
    61       moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
     65      moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1],
    6266        coordinates[edge2source, 0], coordinates[edge2source, 1]);
    63       moveQuality += evaluator.CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
     67      moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1target, 0], coordinates[edge1target, 1],
    6468        coordinates[edge2target, 0], coordinates[edge2target, 1]);
    6569      return moveQuality;
     
    8286    }
    8387
    84     protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
    85       return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, this);
     88    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) {
     89      return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, distanceFunction);
    8690    }
    8791
Note: See TracChangeset for help on using the changeset viewer.