Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/16/10 16:04:54 (14 years ago)
Author:
swagner
Message:

Implemented first version of distance matrix-based evaluation for the TSP (#912)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/Evaluators/TSPCoordinatesPathEvaluator.cs

    r3053 r3066  
    3939      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
    4040    }
     41    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
     42      get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
     43    }
     44    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
     45      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
     46    }
    4147
    4248    protected TSPCoordinatesPathEvaluator()
     
    4450      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));
    4551      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
     52      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
     53      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
    4654    }
    4755
    4856    public sealed override IOperation Apply() {
    49       Permutation p = PermutationParameter.ActualValue;
    50       DoubleMatrix c = CoordinatesParameter.ActualValue;
     57      if (UseDistanceMatrixParameter.ActualValue.Value) {
     58        Permutation p = PermutationParameter.ActualValue;
     59        DoubleMatrix dm = DistanceMatrixParameter.ActualValue;
    5160
    52       double length = 0;
    53       for (int i = 0; i < p.Length - 1; i++)
    54         length += CalculateDistance(c[p[i], 0], c[p[i], 1], c[p[i + 1], 0], c[p[i + 1], 1]);
    55       length += CalculateDistance(c[p[p.Length - 1], 0], c[p[p.Length - 1], 1], c[p[0], 0], c[p[0], 1]);
    56       QualityParameter.ActualValue = new DoubleValue(length);
     61        if (dm == null) {  // calculate distance matrix
     62          DoubleMatrix c = CoordinatesParameter.ActualValue;
    5763
     64          dm = new DoubleMatrix(c.Rows, c.Rows);
     65          for (int i = 0; i < dm.Rows; i++) {
     66            for (int j = 0; j < dm.Columns; j++)
     67              dm[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
     68          }
     69          DistanceMatrixParameter.ActualValue = dm;
     70        }
     71
     72        double length = 0;
     73        for (int i = 0; i < p.Length - 1; i++)
     74          length += dm[p[i], p[i + 1]];
     75        length += dm[p[p.Length - 1], p[0]];
     76        QualityParameter.ActualValue = new DoubleValue(length);
     77      } else {
     78        Permutation p = PermutationParameter.ActualValue;
     79        DoubleMatrix c = CoordinatesParameter.ActualValue;
     80
     81        double length = 0;
     82        for (int i = 0; i < p.Length - 1; i++)
     83          length += CalculateDistance(c[p[i], 0], c[p[i], 1], c[p[i + 1], 0], c[p[i + 1], 1]);
     84        length += CalculateDistance(c[p[p.Length - 1], 0], c[p[p.Length - 1], 1], c[p[0], 0], c[p[0], 1]);
     85        QualityParameter.ActualValue = new DoubleValue(length);
     86      }
    5887      return base.Apply();
    5988    }
Note: See TracChangeset for help on using the changeset viewer.