Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2701_MemPRAlgorithm/HeuristicLab.Problems.QuadraticAssignment/3.3/QAPBasicProblem.cs @ 16189

Last change on this file since 16189 was 14453, checked in by abeham, 8 years ago

#2701:

  • Worked on MemPR algorithm for permutations
    • Made Evaluate() method mostly thread-safe and moved evaluated solutions counting to caller
  • Removed TSP specific similarity calculator (it is actually not specific to the TSP) and added it to the permutation encoding as HammingSimilarityCalculator
  • Fixed bug in qap basicproblem
File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.Instances;
33
34namespace HeuristicLab.Problems.QuadraticAssignment {
35  [Item("Basic Quadratic Assignment Problem (QAP)", "The Quadratic Assignment Problem (QAP) can be described as the problem of assigning N facilities to N fixed locations such that there is exactly one facility in each location and that the sum of the distances multiplied by the connection strength between the facilities becomes minimal.")]
36  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 141)]
37  [StorableClass]
38  public sealed class QAPBasicProblem : SingleObjectiveBasicProblem<PermutationEncoding>,
39    IProblemInstanceConsumer<QAPData>,
40    IProblemInstanceConsumer<TSPData> {
41
42    [Storable]
43    private IValueParameter<DoubleMatrix> weightsParameter;
44    public DoubleMatrix Weights {
45      get { return weightsParameter.Value; }
46      set { weightsParameter.Value = value; }
47    }
48
49    [Storable]
50    private IValueParameter<DoubleMatrix> distancesParameter;
51    public DoubleMatrix Distances {
52      get { return distancesParameter.Value; }
53      set { distancesParameter.Value = value; }
54    }
55
56
57    [StorableConstructor]
58    private QAPBasicProblem(bool deserializing) : base(deserializing) { }
59    private QAPBasicProblem(QAPBasicProblem original, Cloner cloner)
60      : base(original, cloner) {
61      weightsParameter = cloner.Clone(original.weightsParameter);
62      distancesParameter = cloner.Clone(original.distancesParameter);
63    }
64    public QAPBasicProblem() {
65      Parameters.Add(weightsParameter = new ValueParameter<DoubleMatrix>("Weights", "The weights matrix.", new DoubleMatrix(5, 5)));
66      Parameters.Add(distancesParameter = new ValueParameter<DoubleMatrix>("Distances", "The distances matrix.", new DoubleMatrix(5, 5)));
67
68      Operators.Add(new HammingSimilarityCalculator());
69      Operators.Add(new QualitySimilarityCalculator());
70      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
71
72      Parameterize();
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new QAPBasicProblem(this, cloner);
77    }
78
79    protected override void OnEncodingChanged() {
80      base.OnEncodingChanged();
81      Parameterize();
82    }
83
84    private void Parameterize() {
85      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
86        similarityCalculator.SolutionVariableName = Encoding.SolutionCreator.PermutationParameter.ActualName;
87        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
88      }
89    }
90
91    public override bool Maximization {
92      get { return false; }
93    }
94
95    public override double Evaluate(Individual individual, IRandom random) {
96      return QAPEvaluator.Apply(individual.Permutation(), Weights, Distances);
97    }
98
99    public void Load(QAPData data) {
100      var weights = new DoubleMatrix(data.Weights);
101      var distances = new DoubleMatrix(data.Distances);
102      Name = data.Name;
103      Description = data.Description;
104      Load(weights, distances);
105      if (data.BestKnownQuality.HasValue) BestKnownQuality = data.BestKnownQuality.Value;
106      EvaluateAndLoadAssignment(data.BestKnownAssignment);
107      OnReset();
108    }
109
110    public void Load(TSPData data) {
111      if (data.Dimension > 1000)
112        throw new System.IO.InvalidDataException("Instances with more than 1000 customers are not supported by the QAP.");
113      var weights = new DoubleMatrix(data.Dimension, data.Dimension);
114      for (int i = 0; i < data.Dimension; i++)
115        weights[i, (i + 1) % data.Dimension] = 1;
116      var distances = new DoubleMatrix(data.GetDistanceMatrix());
117      Name = data.Name;
118      Description = data.Description;
119      Load(weights, distances);
120      if (data.BestKnownQuality.HasValue) BestKnownQuality = data.BestKnownQuality.Value;
121      EvaluateAndLoadAssignment(data.BestKnownTour);
122      OnReset();
123    }
124
125    public void Load(DoubleMatrix weights, DoubleMatrix distances) {
126      if (weights == null || weights.Rows == 0)
127        throw new System.IO.InvalidDataException("The given instance does not contain weights!");
128      if (weights.Rows != weights.Columns)
129        throw new System.IO.InvalidDataException("The weights matrix is not a square matrix!");
130      if (distances == null || distances.Rows == 0)
131        throw new System.IO.InvalidDataException("The given instance does not contain distances!");
132      if (distances.Rows != distances.Columns)
133        throw new System.IO.InvalidDataException("The distances matrix is not a square matrix!");
134      if (weights.Rows != distances.Columns)
135        throw new System.IO.InvalidDataException("The weights matrix and the distance matrix are not of equal size!");
136
137      Weights = weights;
138      Distances = distances;
139      Encoding.Length = Weights.Rows;
140
141      BestKnownQuality = double.NaN;
142    }
143
144    public void EvaluateAndLoadAssignment(int[] assignment) {
145      if (assignment == null || assignment.Length == 0) return;
146      var vector = new Permutation(PermutationTypes.Absolute, assignment);
147      var result = QAPEvaluator.Apply(vector, Weights, Distances);
148      BestKnownQuality = result;
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.