Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/02/11 22:33:30 (13 years ago)
Author:
abeham
Message:

#1330

  • Updated solution view
  • Made distance matrix a mandatory parameter (solution instances are small anyway)
  • Added and set best known solution if available for a QAPLIB instance
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Evaluators/QAPEvaluator.cs

    r5562 r5598  
    2020#endregion
    2121
    22 using System;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
     
    3534    public ILookupParameter<Permutation> PermutationParameter {
    3635      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
    37     }
    38     public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
    39       get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
    4036    }
    4137    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
     
    5753    public QAPEvaluator() {
    5854      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation that represents the current solution."));
    59       Parameters.Add(new ValueLookupParameter<BoolValue>("UseDistanceMatrix", "True if the distance matrix should be used, false if the distances should be caluclated by taking the euclidean distance between the coordinates."));
    6055      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The distance matrix that contains the distances between the locations."));
    6156      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The coordinates in case the distance matrix should not be used."));
     
    6863    }
    6964
    70     public static double Apply(Permutation assignment, DoubleMatrix weights, Func<int, int, double> distance) {
     65    public static double Apply(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances) {
    7166      double quality = 0;
    7267      for (int i = 0; i < assignment.Length; i++) {
    7368        for (int j = 0; j < assignment.Length; j++) {
    74           quality += weights[i, j] * distance(assignment[i], assignment[j]);
     69          quality += weights[i, j] * distances[assignment[i], assignment[j]];
    7570        }
    7671      }
     
    8075    public override IOperation Apply() {
    8176      Permutation assignment = PermutationParameter.ActualValue;
    82       bool useDistanceMatrix = UseDistanceMatrixParameter.ActualValue.Value;
    8377      DoubleMatrix weights = WeightsParameter.ActualValue;
    84       double quality;
     78      DoubleMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
    8579
    86       if (useDistanceMatrix) {
    87         DoubleMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
    88         quality = Apply(assignment, weights, (x, y) => distanceMatrix[x, y]);
    89       } else {
    90         DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
    91         quality = Apply(assignment, weights, (x, y) => Distance(coordinates, x, y));
    92       }
    93 
     80      double quality = Apply(assignment, weights, distanceMatrix);
    9481      QualityParameter.ActualValue = new DoubleValue(quality);
    9582
    9683      return base.Apply();
    9784    }
    98 
    99     private double Distance(DoubleMatrix coordinates, int row1, int row2) {
    100       double dx = coordinates[row1, 0] - coordinates[row2, 0];
    101       double dy = coordinates[row1, 1] - coordinates[row2, 1];
    102       return Math.Sqrt(dx * dx + dy * dy);
    103     }
    10485  }
    10586}
Note: See TracChangeset for help on using the changeset viewer.