Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/24/10 21:15:04 (15 years ago)
Author:
abeham
Message:

Added 3-opt move and evaluators for the TSP
Added 2-opt move evaluators for TSP Geo and Euclidean
#889

Location:
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators
Files:
10 added
4 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPMoveEvaluator.cs

    r3158 r3209  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Core;
    2324using HeuristicLab.Data;
     
    3435  [StorableClass]
    3536  public abstract class TSPMoveEvaluator : SingleSuccessorOperator, ITSPMoveEvaluator, IMoveOperator {
     37    public abstract Type EvaluatorType { get; }
     38
    3639    public ILookupParameter<DoubleValue> QualityParameter {
    3740      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPPathMoveEvaluator.cs

    r3158 r3209  
    5454      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."));
    5555    }
     56
     57    public override IOperation Apply() {
     58      Permutation permutation = PermutationParameter.ActualValue;
     59      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
     60      double relativeQualityDifference = 0;
     61      if (UseDistanceMatrixParameter.ActualValue.Value) {
     62        DoubleMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
     63        if (distanceMatrix == null) {
     64          distanceMatrix = CalculateDistanceMatrix(coordinates);
     65          DistanceMatrixParameter.ActualValue = distanceMatrix;
     66        }
     67        relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix);
     68      } else relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates);
     69      DoubleValue moveQuality = MoveQualityParameter.ActualValue;
     70      if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(QualityParameter.ActualValue.Value + relativeQualityDifference);
     71      else moveQuality.Value = QualityParameter.ActualValue.Value + relativeQualityDifference;
     72      return base.Apply();
     73    }
     74
     75    protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);
     76    protected abstract double EvaluateByDistanceMatrix(Permutation permutation, DoubleMatrix distanceMatrix);
     77    protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates);
     78
     79    private DoubleMatrix CalculateDistanceMatrix(DoubleMatrix c) {
     80      DoubleMatrix distanceMatrix = new DoubleMatrix(c.Rows, c.Rows);
     81      for (int i = 0; i < distanceMatrix.Rows; i++) {
     82        for (int j = 0; j < distanceMatrix.Columns; j++)
     83          distanceMatrix[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
     84      }
     85      return distanceMatrix;
     86    }
    5687  }
    5788}
Note: See TracChangeset for help on using the changeset viewer.