Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/02/11 22:33:30 (14 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
Location:
branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3
Files:
1 added
4 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}
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/HeuristicLab.Problems.QuadraticAssignment-3.3.csproj

    r5583 r5598  
    144144    <Compile Include="Evaluators\QAPEvaluator.cs" />
    145145    <Compile Include="Interfaces\IQAPEvaluator.cs" />
     146    <Compile Include="Parsers\QAPLIBSolutionParser.cs" />
    146147    <Compile Include="Parsers\QAPLIBParser.cs" />
    147148    <Compile Include="QAPAssignment.cs" />
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/Interfaces/IQAPEvaluator.cs

    r5562 r5598  
    2828  public interface IQAPEvaluator : ISingleObjectiveEvaluator {
    2929    ILookupParameter<Permutation> PermutationParameter { get; }
    30     ILookupParameter<BoolValue> UseDistanceMatrixParameter { get; }
    3130    ILookupParameter<DoubleMatrix> DistanceMatrixParameter { get; }
    3231    ILookupParameter<DoubleMatrix> CoordinatesParameter { get; }
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs

    r5583 r5598  
    5959      get { return (ValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
    6060    }
    61     public ValueParameter<BoolValue> UseDistanceMatrixParameter {
    62       get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
    63     }
    64 
    6561    #endregion
    6662
     
    8177      get { return DistanceMatrixParameter.Value; }
    8278      set { DistanceMatrixParameter.Value = value; }
    83     }
    84     public BoolValue UseDistanceMatrix {
    85       get { return UseDistanceMatrixParameter.Value; }
    86       set { UseDistanceMatrixParameter.Value = value; }
    8779    }
    8880
     
    112104      : base() {
    113105      Parameters.Add(new ValueParameter<Permutation>("BestKnownSolution", "The best known solution which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
    114       Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations."));
     106      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations. If this is changed the distance matrix is calculated automatically using the euclidean distance."));
    115107      Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5)));
    116108      Parameters.Add(new ValueParameter<DoubleMatrix>("DistanceMatrix", "The distance matrix which can either be specified directly without the coordinates, or can be calculated automatically from the coordinates.", new DoubleMatrix(5, 5)));
    117       Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "Defaults to true, set to false only when the number of facilities is very high and a distance matrix would become too large (>1000 facilities).", new BoolValue(true)));
    118109
    119110      Maximization = new BoolValue(false);
     
    196187      ParameterizeOperators();
    197188    }
     189    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
     190      Coordinates.Reset += new EventHandler(Coordinates_Reset);
     191      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
     192      UpdateDistanceMatrix();
     193    }
     194    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
     195      UpdateDistanceMatrix();
     196    }
     197    private void Coordinates_Reset(object sender, EventArgs e) {
     198      UpdateDistanceMatrix();
     199    }
    198200    #endregion
    199201
     
    205207
    206208    private void AttachEventHandlers() {
     209      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
     210      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    207211      WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
    208212      Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
    209       SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
    210       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     213      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
     214      Coordinates.Reset += new EventHandler(Coordinates_Reset);
     215      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
    211216    }
    212217
     
    226231      if (Evaluator != null) {
    227232        Evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    228         Evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
    229233        Evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
    230234        Evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
     
    265269      }
    266270    }
     271
     272    private void UpdateDistanceMatrix() {
     273      if (Coordinates != null && Coordinates.Columns == 2 && Coordinates.Rows > 1) {
     274        DistanceMatrix = new DoubleMatrix(Coordinates.Rows, Coordinates.Rows);
     275        for (int i = 0; i < Coordinates.Rows - 1; i++) {
     276          for (int j = i + 1; j < Coordinates.Rows; j++) {
     277            double dx = Coordinates[i, 0] - Coordinates[j, 0];
     278            double dy = Coordinates[i, 1] - Coordinates[j, 1];
     279            DistanceMatrix[i, j] = Math.Sqrt(dx * dx + dy * dy);
     280            DistanceMatrix[j, i] = DistanceMatrix[i, j];
     281          }
     282        }
     283      }
     284    }
    267285    #endregion
    268286
     
    273291      DistanceMatrix = new DoubleMatrix(parser.Distances);
    274292      Weights = new DoubleMatrix(parser.Weights);
    275       UseDistanceMatrix.Value = true;
    276293      Name = "Quadratic Assignment Problem (imported from " + Path.GetFileNameWithoutExtension(filename) + ")";
    277294      Description = "Imported problem data using QAPLIBParser " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().FirstOrDefault().Version + ".";
     295      BestKnownQuality = null;
     296      BestKnownSolution = null;
    278297      OnReset();
    279298    }
     
    287306        DistanceMatrix = new DoubleMatrix(parser.Distances);
    288307        Weights = new DoubleMatrix(parser.Weights);
    289         UseDistanceMatrix.Value = true;
    290308        Name = "Quadratic Assignment Problem (loaded instance " + instance + ")";
    291309        Description = "Loaded embedded problem data of instance " + instance + ".";
    292310        OnReset();
    293311      }
     312      bool solutionExists = Assembly.GetExecutingAssembly()
     313          .GetManifestResourceNames()
     314          .Where(x => x.EndsWith(instance + ".sln"))
     315          .Any();
     316      if (solutionExists) {
     317        using (Stream solStream = Assembly.GetExecutingAssembly()
     318          .GetManifestResourceStream(InstancePrefix + instance + ".sln")) {
     319          QAPLIBSolutionParser solParser = new QAPLIBSolutionParser();
     320          solParser.Parse(solStream);
     321          BestKnownQuality = new DoubleValue(solParser.Qualiy);
     322          BestKnownSolution = new Permutation(PermutationTypes.Absolute, solParser.Assignment);
     323        }
     324      } else {
     325        BestKnownQuality = null;
     326        BestKnownSolution = null;
     327      }
    294328    }
    295329  }
Note: See TracChangeset for help on using the changeset viewer.