[5558] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5558] | 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 |
|
---|
| 22 | using System;
|
---|
[7626] | 23 | using System.Collections.Generic;
|
---|
[5558] | 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[17320] | 26 | using System.Threading;
|
---|
[16948] | 27 | using HEAL.Attic;
|
---|
[5558] | 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Data;
|
---|
| 31 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 32 | using HeuristicLab.Optimization;
|
---|
| 33 | using HeuristicLab.Parameters;
|
---|
[5562] | 34 | using HeuristicLab.PluginInfrastructure;
|
---|
[7558] | 35 | using HeuristicLab.Problems.Instances;
|
---|
[5558] | 36 |
|
---|
| 37 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
[13173] | 38 | [Item("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.")]
|
---|
[12504] | 39 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 140)]
|
---|
[16723] | 40 | [StorableType("A86B1F49-D8E6-45E4-8EFB-8F5CCA2F9DC7")]
|
---|
[16948] | 41 | public sealed class QuadraticAssignmentProblem : PermutationProblem,
|
---|
[7558] | 42 | IProblemInstanceConsumer<QAPData>,
|
---|
[17252] | 43 | IProblemInstanceConsumer<TSPData>, IProblemInstanceExporter<QAPData> {
|
---|
| 44 | public static int ProblemSizeLimit = 1000;
|
---|
[5953] | 45 |
|
---|
[7201] | 46 | public static new Image StaticItemImage {
|
---|
[13396] | 47 | get { return Common.Resources.VSImageLibrary.Type; }
|
---|
[5558] | 48 | }
|
---|
| 49 |
|
---|
| 50 | #region Parameter Properties
|
---|
[17252] | 51 | [Storable] public IValueParameter<ItemSet<Permutation>> BestKnownSolutionsParameter { get; private set; }
|
---|
| 52 | [Storable] public IValueParameter<Permutation> BestKnownSolutionParameter { get; private set; }
|
---|
| 53 | [Storable] public IValueParameter<DoubleMatrix> WeightsParameter { get; private set; }
|
---|
| 54 | [Storable] public IValueParameter<DoubleMatrix> DistancesParameter { get; private set; }
|
---|
| 55 | [Storable] public IValueParameter<DoubleValue> LowerBoundParameter { get; private set; }
|
---|
| 56 | [Storable] public IValueParameter<DoubleValue> AverageQualityParameter { get; private set; }
|
---|
[5558] | 57 | #endregion
|
---|
| 58 |
|
---|
| 59 | #region Properties
|
---|
[6525] | 60 | public ItemSet<Permutation> BestKnownSolutions {
|
---|
[17252] | 61 | get { return BestKnownSolutionsParameter.Value; }
|
---|
| 62 | set { BestKnownSolutionsParameter.Value = value; }
|
---|
[6342] | 63 | }
|
---|
[5558] | 64 | public Permutation BestKnownSolution {
|
---|
[17252] | 65 | get { return BestKnownSolutionParameter.Value; }
|
---|
| 66 | set { BestKnownSolutionParameter.Value = value; }
|
---|
[5558] | 67 | }
|
---|
| 68 | public DoubleMatrix Weights {
|
---|
[17252] | 69 | get { return WeightsParameter.Value; }
|
---|
| 70 | set { WeightsParameter.Value = value; }
|
---|
[5558] | 71 | }
|
---|
[5838] | 72 | public DoubleMatrix Distances {
|
---|
[17252] | 73 | get { return DistancesParameter.Value; }
|
---|
| 74 | set { DistancesParameter.Value = value; }
|
---|
[5558] | 75 | }
|
---|
[7877] | 76 | public DoubleValue LowerBound {
|
---|
[17252] | 77 | get { return LowerBoundParameter.Value; }
|
---|
| 78 | set { LowerBoundParameter.Value = value; }
|
---|
[7877] | 79 | }
|
---|
| 80 | public DoubleValue AverageQuality {
|
---|
[17252] | 81 | get { return AverageQualityParameter.Value; }
|
---|
| 82 | set { AverageQualityParameter.Value = value; }
|
---|
[7877] | 83 | }
|
---|
[5563] | 84 |
|
---|
[5583] | 85 | private BestQAPSolutionAnalyzer BestQAPSolutionAnalyzer {
|
---|
| 86 | get { return Operators.OfType<BestQAPSolutionAnalyzer>().FirstOrDefault(); }
|
---|
| 87 | }
|
---|
[6342] | 88 |
|
---|
| 89 | private QAPAlleleFrequencyAnalyzer QAPAlleleFrequencyAnalyzer {
|
---|
| 90 | get { return Operators.OfType<QAPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
|
---|
| 91 | }
|
---|
[5558] | 92 | #endregion
|
---|
| 93 |
|
---|
| 94 | [StorableConstructor]
|
---|
[16723] | 95 | private QuadraticAssignmentProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[5562] | 96 | private QuadraticAssignmentProblem(QuadraticAssignmentProblem original, Cloner cloner)
|
---|
[5558] | 97 | : base(original, cloner) {
|
---|
[17252] | 98 | BestKnownSolutionsParameter = cloner.Clone(original.BestKnownSolutionsParameter);
|
---|
| 99 | BestKnownSolutionParameter = cloner.Clone(original.BestKnownSolutionParameter);
|
---|
| 100 | WeightsParameter = cloner.Clone(original.WeightsParameter);
|
---|
| 101 | DistancesParameter = cloner.Clone(original.DistancesParameter);
|
---|
| 102 | LowerBoundParameter = cloner.Clone(original.LowerBoundParameter);
|
---|
| 103 | AverageQualityParameter = cloner.Clone(original.AverageQualityParameter);
|
---|
[5558] | 104 | }
|
---|
| 105 | public QuadraticAssignmentProblem()
|
---|
[13396] | 106 | : base(new PermutationEncoding("Assignment") { Length = 5 }) {
|
---|
[17270] | 107 | Maximization = false;
|
---|
[17680] | 108 | Encoding.LengthParameter.ReadOnly = DimensionRefParameter.ReadOnly = true;
|
---|
| 109 | Encoding.PermutationTypeParameter.ReadOnly = PermutationTypeRefParameter.ReadOnly = true;
|
---|
| 110 | PermutationTypeRefParameter.Hidden = true;
|
---|
| 111 |
|
---|
[17252] | 112 | Parameters.Add(BestKnownSolutionsParameter = new OptionalValueParameter<ItemSet<Permutation>>("BestKnownSolutions", "The list of best known solutions which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
|
---|
| 113 | Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<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(WeightsParameter = new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities."));
|
---|
| 115 | Parameters.Add(DistancesParameter = new ValueParameter<DoubleMatrix>("Distances", "The distance matrix which can either be specified directly without the coordinates, or can be calculated automatically from the coordinates."));
|
---|
| 116 | Parameters.Add(LowerBoundParameter = new OptionalValueParameter<DoubleValue>("LowerBound", "The Gilmore-Lawler lower bound to the solution quality."));
|
---|
| 117 | Parameters.Add(AverageQualityParameter = new OptionalValueParameter<DoubleValue>("AverageQuality", "The expected quality of a random solution."));
|
---|
[5558] | 118 |
|
---|
[7877] | 119 | WeightsParameter.GetsCollected = false;
|
---|
[5558] | 120 | Weights = new DoubleMatrix(new double[,] {
|
---|
| 121 | { 0, 1, 0, 0, 1 },
|
---|
| 122 | { 1, 0, 1, 0, 0 },
|
---|
| 123 | { 0, 1, 0, 1, 0 },
|
---|
| 124 | { 0, 0, 1, 0, 1 },
|
---|
| 125 | { 1, 0, 0, 1, 0 }
|
---|
[17252] | 126 | }, @readonly: true);
|
---|
[5558] | 127 |
|
---|
[7877] | 128 | DistancesParameter.GetsCollected = false;
|
---|
[5838] | 129 | Distances = new DoubleMatrix(new double[,] {
|
---|
[5558] | 130 | { 0, 360, 582, 582, 360 },
|
---|
| 131 | { 360, 0, 360, 582, 582 },
|
---|
| 132 | { 582, 360, 0, 360, 582 },
|
---|
| 133 | { 582, 582, 360, 0, 360 },
|
---|
| 134 | { 360, 582, 582, 360, 0 }
|
---|
[17252] | 135 | }, @readonly: true);
|
---|
[5558] | 136 |
|
---|
| 137 | InitializeOperators();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[17382] | 140 | public override ISingleObjectiveEvaluationResult Evaluate(Permutation assignment, IRandom random, CancellationToken cancellationToken) {
|
---|
| 141 | var quality = Evaluate(assignment, cancellationToken);
|
---|
| 142 | return new SingleObjectiveEvaluationResult(quality);
|
---|
[13396] | 143 | }
|
---|
| 144 |
|
---|
[17320] | 145 | public double Evaluate(Permutation assignment, CancellationToken cancellationToken) {
|
---|
[13396] | 146 | double quality = 0;
|
---|
| 147 | for (int i = 0; i < assignment.Length; i++) {
|
---|
| 148 | for (int j = 0; j < assignment.Length; j++) {
|
---|
| 149 | quality += Weights[i, j] * Distances[assignment[i], assignment[j]];
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | return quality;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[5558] | 155 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 156 | return new QuadraticAssignmentProblem(this, cloner);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | #region Events
|
---|
[5583] | 160 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[13396] | 161 | Parameterize();
|
---|
[5583] | 162 | }
|
---|
[5558] | 163 | #endregion
|
---|
| 164 |
|
---|
[17544] | 165 | protected override void DimensionOnChanged() {
|
---|
| 166 | base.DimensionOnChanged();
|
---|
| 167 | if (Dimension != Weights.Rows) Dimension = Weights.Rows;
|
---|
[5558] | 168 | }
|
---|
| 169 |
|
---|
[7877] | 170 | #region Helpers
|
---|
[5558] | 171 | private void InitializeOperators() {
|
---|
[6088] | 172 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IQAPMoveEvaluator>());
|
---|
[13396] | 173 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IQAPLocalImprovementOperator>());
|
---|
[5583] | 174 | Operators.Add(new BestQAPSolutionAnalyzer());
|
---|
[6342] | 175 | Operators.Add(new QAPAlleleFrequencyAnalyzer());
|
---|
[11300] | 176 |
|
---|
[9029] | 177 | Operators.Add(new QAPSimilarityCalculator());
|
---|
[13396] | 178 | Parameterize();
|
---|
[5558] | 179 | }
|
---|
[13396] | 180 | private void Parameterize() {
|
---|
| 181 | var operators = new List<IItem>();
|
---|
[5583] | 182 | if (BestQAPSolutionAnalyzer != null) {
|
---|
[13396] | 183 | operators.Add(BestQAPSolutionAnalyzer);
|
---|
[5583] | 184 | BestQAPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
[5838] | 185 | BestQAPSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
[5583] | 186 | BestQAPSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 187 | BestQAPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
[6342] | 188 | BestQAPSolutionAnalyzer.BestKnownSolutionsParameter.ActualName = BestKnownSolutionsParameter.Name;
|
---|
[5583] | 189 | BestQAPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 190 | }
|
---|
[6342] | 191 | if (QAPAlleleFrequencyAnalyzer != null) {
|
---|
[13396] | 192 | operators.Add(QAPAlleleFrequencyAnalyzer);
|
---|
[6342] | 193 | QAPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 194 | QAPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 195 | QAPAlleleFrequencyAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
| 196 | QAPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 197 | QAPAlleleFrequencyAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 198 | }
|
---|
[13396] | 199 | foreach (var localOpt in Operators.OfType<IQAPLocalImprovementOperator>()) {
|
---|
| 200 | operators.Add(localOpt);
|
---|
[6342] | 201 | localOpt.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
| 202 | localOpt.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 203 | localOpt.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 204 | localOpt.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 205 | }
|
---|
[9029] | 206 |
|
---|
[13396] | 207 | foreach (var moveOp in Operators.OfType<IQAPMoveEvaluator>()) {
|
---|
| 208 | operators.Add(moveOp);
|
---|
| 209 | moveOp.DistancesParameter.ActualName = DistancesParameter.Name;
|
---|
| 210 | moveOp.WeightsParameter.ActualName = WeightsParameter.Name;
|
---|
| 211 | moveOp.QualityParameter.ActualName = Evaluator.QualityParameter.Name;
|
---|
| 212 |
|
---|
| 213 | var swaMoveOp = moveOp as QAPSwap2MoveEvaluator;
|
---|
| 214 | if (swaMoveOp != null) {
|
---|
| 215 | var moveQualityName = swaMoveOp.MoveQualityParameter.ActualName;
|
---|
| 216 | foreach (var o in Encoding.Operators.OfType<IPermutationSwap2MoveQualityOperator>())
|
---|
| 217 | o.MoveQualityParameter.ActualName = moveQualityName;
|
---|
| 218 | }
|
---|
| 219 | var invMoveOp = moveOp as QAPInversionMoveEvaluator;
|
---|
| 220 | if (invMoveOp != null) {
|
---|
| 221 | var moveQualityName = invMoveOp.MoveQualityParameter.ActualName;
|
---|
| 222 | foreach (var o in Encoding.Operators.OfType<IPermutationInversionMoveQualityOperator>())
|
---|
| 223 | o.MoveQualityParameter.ActualName = moveQualityName;
|
---|
| 224 | }
|
---|
| 225 | var traMoveOp = moveOp as QAPTranslocationMoveEvaluator;
|
---|
| 226 | if (traMoveOp != null) {
|
---|
| 227 | var moveQualityName = traMoveOp.MoveQualityParameter.ActualName;
|
---|
| 228 | foreach (var o in Encoding.Operators.OfType<IPermutationTranslocationMoveQualityOperator>())
|
---|
| 229 | o.MoveQualityParameter.ActualName = moveQualityName;
|
---|
| 230 | }
|
---|
| 231 | var scrMoveOp = moveOp as QAPScrambleMoveEvaluator;
|
---|
| 232 | if (scrMoveOp != null) {
|
---|
| 233 | var moveQualityName = scrMoveOp.MoveQualityParameter.ActualName;
|
---|
| 234 | foreach (var o in Encoding.Operators.OfType<IPermutationScrambleMoveQualityOperator>())
|
---|
| 235 | o.MoveQualityParameter.ActualName = moveQualityName;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
[16692] | 238 | foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
[17252] | 239 | operators.Add(similarityCalculator);
|
---|
[13396] | 240 | similarityCalculator.SolutionVariableName = Encoding.Name;
|
---|
[9029] | 241 | similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
[16692] | 242 | var qapsimcalc = similarityCalculator as QAPSimilarityCalculator;
|
---|
| 243 | if (qapsimcalc != null) {
|
---|
| 244 | qapsimcalc.Weights = Weights;
|
---|
| 245 | qapsimcalc.Distances = Distances;
|
---|
| 246 | }
|
---|
[9029] | 247 | }
|
---|
[13396] | 248 |
|
---|
| 249 | if (operators.Count > 0) Encoding.ConfigureOperators(operators);
|
---|
[5562] | 250 | }
|
---|
[5598] | 251 |
|
---|
[7877] | 252 | private void UpdateParameterValues() {
|
---|
[8910] | 253 | Permutation lbSolution;
|
---|
| 254 | // calculate the optimum of a LAP relaxation and use it as lower bound of our QAP
|
---|
| 255 | LowerBound = new DoubleValue(GilmoreLawlerBoundCalculator.CalculateLowerBound(Weights, Distances, out lbSolution));
|
---|
[17320] | 256 | // evaluate the LAP optimal solution as if it was a QAP solution
|
---|
| 257 | var lbSolutionQuality = Evaluate(lbSolution, CancellationToken.None);
|
---|
[8910] | 258 | // in case both qualities are the same it means that the LAP optimum is also a QAP optimum
|
---|
| 259 | if (LowerBound.Value.IsAlmost(lbSolutionQuality)) {
|
---|
| 260 | BestKnownSolution = lbSolution;
|
---|
[13396] | 261 | BestKnownQuality = LowerBound.Value;
|
---|
[8910] | 262 | }
|
---|
| 263 | AverageQuality = new DoubleValue(ComputeAverageQuality());
|
---|
[7877] | 264 | }
|
---|
[8910] | 265 |
|
---|
| 266 | private double ComputeAverageQuality() {
|
---|
| 267 | double rt = 0, rd = 0, wt = 0, wd = 0;
|
---|
| 268 | int n = Weights.Rows;
|
---|
| 269 | for (int i = 0; i < n; i++)
|
---|
| 270 | for (int j = 0; j < n; j++) {
|
---|
| 271 | if (i == j) {
|
---|
| 272 | rd += Distances[i, i];
|
---|
| 273 | wd += Weights[i, i];
|
---|
| 274 | } else {
|
---|
| 275 | rt += Distances[i, j];
|
---|
| 276 | wt += Weights[i, j];
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | return rt * wt / (n * (n - 1)) + rd * wd / n;
|
---|
| 281 | }
|
---|
[5558] | 282 | #endregion
|
---|
[5562] | 283 |
|
---|
[7558] | 284 | public void Load(QAPData data) {
|
---|
[17320] | 285 | if (data.Dimension > ProblemSizeLimit) throw new System.IO.InvalidDataException("The problem is limited to instance of size " + ProblemSizeLimit + ". You can change this limit by modifying " + nameof(QuadraticAssignmentProblem) + "." + nameof(ProblemSizeLimit) + "!");
|
---|
[17252] | 286 | var weights = new DoubleMatrix(data.Weights, @readonly: true);
|
---|
| 287 | var distances = new DoubleMatrix(data.Distances, @readonly: true);
|
---|
[7641] | 288 | Name = data.Name;
|
---|
| 289 | Description = data.Description;
|
---|
[7558] | 290 | Load(weights, distances);
|
---|
[13396] | 291 | if (data.BestKnownQuality.HasValue) BestKnownQuality = data.BestKnownQuality.Value;
|
---|
[7558] | 292 | EvaluateAndLoadAssignment(data.BestKnownAssignment);
|
---|
[5562] | 293 | OnReset();
|
---|
| 294 | }
|
---|
[5563] | 295 |
|
---|
[7558] | 296 | public void Load(TSPData data) {
|
---|
[17252] | 297 | if (data.Dimension > ProblemSizeLimit) throw new System.IO.InvalidDataException("The problem is limited to instance of size " + ProblemSizeLimit + ". You can change this limit by modifying " + nameof(QuadraticAssignmentProblem) + "." + nameof(ProblemSizeLimit) + "!");
|
---|
| 298 | var w = new double[data.Dimension, data.Dimension];
|
---|
[7558] | 299 | for (int i = 0; i < data.Dimension; i++)
|
---|
[17252] | 300 | w[i, (i + 1) % data.Dimension] = 1;
|
---|
| 301 | var weights = new DoubleMatrix(w, @readonly: true);
|
---|
| 302 | var distances = new DoubleMatrix(data.GetDistanceMatrix(), @readonly: true);
|
---|
[7641] | 303 | Name = data.Name;
|
---|
| 304 | Description = data.Description;
|
---|
[7558] | 305 | Load(weights, distances);
|
---|
[13396] | 306 | if (data.BestKnownQuality.HasValue) BestKnownQuality = data.BestKnownQuality.Value;
|
---|
[7558] | 307 | EvaluateAndLoadAssignment(data.BestKnownTour);
|
---|
[6891] | 308 | OnReset();
|
---|
[5563] | 309 | }
|
---|
[6952] | 310 |
|
---|
[7558] | 311 | public void Load(DoubleMatrix weights, DoubleMatrix distances) {
|
---|
| 312 | if (weights == null || weights.Rows == 0)
|
---|
| 313 | throw new System.IO.InvalidDataException("The given instance does not contain weights!");
|
---|
| 314 | if (weights.Rows != weights.Columns)
|
---|
| 315 | throw new System.IO.InvalidDataException("The weights matrix is not a square matrix!");
|
---|
| 316 | if (distances == null || distances.Rows == 0)
|
---|
| 317 | throw new System.IO.InvalidDataException("The given instance does not contain distances!");
|
---|
| 318 | if (distances.Rows != distances.Columns)
|
---|
| 319 | throw new System.IO.InvalidDataException("The distances matrix is not a square matrix!");
|
---|
| 320 | if (weights.Rows != distances.Columns)
|
---|
| 321 | throw new System.IO.InvalidDataException("The weights matrix and the distance matrix are not of equal size!");
|
---|
[6952] | 322 |
|
---|
[7558] | 323 | Weights = weights;
|
---|
| 324 | Distances = distances;
|
---|
[17544] | 325 | Dimension = weights.Rows;
|
---|
[6952] | 326 |
|
---|
[13396] | 327 | BestKnownQualityParameter.Value = null;
|
---|
[7558] | 328 | BestKnownSolution = null;
|
---|
| 329 | BestKnownSolutions = null;
|
---|
[7877] | 330 | UpdateParameterValues();
|
---|
[7558] | 331 | }
|
---|
[6952] | 332 |
|
---|
[7558] | 333 | public void EvaluateAndLoadAssignment(int[] assignment) {
|
---|
| 334 | if (assignment == null || assignment.Length == 0) return;
|
---|
| 335 | var vector = new Permutation(PermutationTypes.Absolute, assignment);
|
---|
[17320] | 336 | var result = Evaluate(vector, CancellationToken.None);
|
---|
[13396] | 337 | BestKnownQuality = result;
|
---|
[7558] | 338 | BestKnownSolution = vector;
|
---|
[13396] | 339 | BestKnownSolutions = new ItemSet<Permutation> { (Permutation)vector.Clone() };
|
---|
[6952] | 340 | }
|
---|
[17252] | 341 |
|
---|
| 342 | public QAPData Export() {
|
---|
| 343 | return new QAPData() {
|
---|
| 344 | Name = Name,
|
---|
| 345 | Description = Description,
|
---|
| 346 | Dimension = Weights.Rows,
|
---|
| 347 | Weights = Weights.CloneAsMatrix(),
|
---|
| 348 | Distances = Distances.CloneAsMatrix(),
|
---|
| 349 | BestKnownAssignment = BestKnownSolution?.ToArray(),
|
---|
| 350 | BestKnownQuality = !double.IsNaN(BestKnownQuality) ? BestKnownQuality : (double?)null
|
---|
| 351 | };
|
---|
| 352 | }
|
---|
[5558] | 353 | }
|
---|
| 354 | }
|
---|