Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2695_dataset-ids/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs @ 16854

Last change on this file since 16854 was 15069, checked in by abeham, 7 years ago

#2706:

  • Added or updated similarity calculators and population similarity analysis for several problems (BinPacking, LAP, Orienteering, Parameter optimization, PTSP, QAP, TF, TSP, VRP)
  • Made TSPSimilarityCalculator obsolete since it's essentially the same as the one in the permutation plugin
  • Made QAPPopulationDiversityAnalyzer obsolete as it is replaced by the newer PopulationSimilarityAnalyzer
  • Removed genotype specific similarity code in QAPPermutationProximityCalculator (again identical to the permutation plugin)
  • Changed QAPSimilarityCalculator to perform phenotype similarity instead of genotype similarity (has not been previously used)
File size: 22.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;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.PermutationEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Optimization.Operators;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.PluginInfrastructure;
36using HeuristicLab.Problems.Instances;
37
38namespace HeuristicLab.Problems.TravelingSalesman {
39  [Item("Traveling Salesman Problem (TSP)", "Represents a symmetric Traveling Salesman Problem.")]
40  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 100)]
41  [StorableClass]
42  public sealed class TravelingSalesmanProblem : SingleObjectiveHeuristicOptimizationProblem<ITSPEvaluator, IPermutationCreator>, IStorableContent,
43    IProblemInstanceConsumer<TSPData> {
44    private static readonly int DistanceMatrixSizeLimit = 1000;
45    public string Filename { get; set; }
46
47    #region Parameter Properties
48    public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
49      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
50    }
51    public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
52      get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
53    }
54    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
55      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
56    }
57    public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
58      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
59    }
60    #endregion
61
62    #region Properties
63    public DoubleMatrix Coordinates {
64      get { return CoordinatesParameter.Value; }
65      set { CoordinatesParameter.Value = value; }
66    }
67    public DistanceMatrix DistanceMatrix {
68      get { return DistanceMatrixParameter.Value; }
69      set { DistanceMatrixParameter.Value = value; }
70    }
71    public BoolValue UseDistanceMatrix {
72      get { return UseDistanceMatrixParameter.Value; }
73      set { UseDistanceMatrixParameter.Value = value; }
74    }
75    public Permutation BestKnownSolution {
76      get { return BestKnownSolutionParameter.Value; }
77      set { BestKnownSolutionParameter.Value = value; }
78    }
79    private BestTSPSolutionAnalyzer BestTSPSolutionAnalyzer {
80      get { return Operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
81    }
82    private TSPAlleleFrequencyAnalyzer TSPAlleleFrequencyAnalyzer {
83      get { return Operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
84    }
85    #endregion
86
87    // BackwardsCompatibility3.3
88    #region Backwards compatible code, remove with 3.4
89    [Obsolete]
90    [Storable(Name = "operators")]
91    private IEnumerable<IOperator> oldOperators {
92      get { return null; }
93      set {
94        if (value != null && value.Any())
95          Operators.AddRange(value);
96      }
97    }
98    #endregion
99
100    [StorableConstructor]
101    private TravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
102    private TravelingSalesmanProblem(TravelingSalesmanProblem original, Cloner cloner)
103      : base(original, cloner) {
104      RegisterEventHandlers();
105    }
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new TravelingSalesmanProblem(this, cloner);
108    }
109    public TravelingSalesmanProblem()
110      : base(new TSPRoundedEuclideanPathEvaluator(), new RandomPermutationCreator()) {
111      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
112      Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
113      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if the coordinates based evaluators should calculate the distance matrix from the coordinates and use it for evaluation similar to the distance matrix evaluator, otherwise false.", new BoolValue(true)));
114      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
115
116      Maximization.Value = false;
117      MaximizationParameter.Hidden = true;
118      UseDistanceMatrixParameter.Hidden = true;
119      DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
120
121      Coordinates = new DoubleMatrix(new double[,] {
122        { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
123        { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
124        { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
125        { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
126      });
127
128      SolutionCreator.PermutationParameter.ActualName = "TSPTour";
129      Evaluator.QualityParameter.ActualName = "TSPTourLength";
130      ParameterizeSolutionCreator();
131      ParameterizeEvaluator();
132
133      InitializeOperators();
134      RegisterEventHandlers();
135    }
136
137    #region Events
138    protected override void OnSolutionCreatorChanged() {
139      base.OnSolutionCreatorChanged();
140      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
141      ParameterizeSolutionCreator();
142      ParameterizeEvaluator();
143      ParameterizeAnalyzers();
144      ParameterizeOperators();
145    }
146    protected override void OnEvaluatorChanged() {
147      base.OnEvaluatorChanged();
148      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
149      ParameterizeEvaluator();
150      ParameterizeSolutionCreator();
151      UpdateMoveEvaluators();
152      ParameterizeAnalyzers();
153      if (Evaluator is ITSPCoordinatesPathEvaluator && Coordinates != null)
154        ClearDistanceMatrix();
155    }
156    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
157      if (Coordinates != null) {
158        Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
159        Coordinates.Reset += new EventHandler(Coordinates_Reset);
160      }
161      if (Evaluator is ITSPCoordinatesPathEvaluator) {
162        ParameterizeSolutionCreator();
163        ClearDistanceMatrix();
164      }
165    }
166    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
167      if (Evaluator is ITSPCoordinatesPathEvaluator) {
168        ClearDistanceMatrix();
169      }
170    }
171    private void Coordinates_Reset(object sender, EventArgs e) {
172      if (Evaluator is ITSPCoordinatesPathEvaluator) {
173        ParameterizeSolutionCreator();
174        ClearDistanceMatrix();
175      }
176    }
177    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
178      ParameterizeEvaluator();
179      ParameterizeAnalyzers();
180      ParameterizeOperators();
181    }
182    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
183      ParameterizeAnalyzers();
184    }
185    #endregion
186
187    #region Helpers
188    [StorableHook(HookType.AfterDeserialization)]
189    private void AfterDeserialization() {
190      // BackwardsCompatibility3.3
191      #region Backwards compatible code (remove with 3.4)
192      OptionalValueParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as OptionalValueParameter<DoubleMatrix>;
193      if (oldDistanceMatrixParameter != null) {
194        Parameters.Remove(oldDistanceMatrixParameter);
195        Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
196        DistanceMatrixParameter.GetsCollected = oldDistanceMatrixParameter.GetsCollected;
197        DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
198        if (oldDistanceMatrixParameter.Value != null) {
199          DoubleMatrix oldDM = oldDistanceMatrixParameter.Value;
200          DistanceMatrix newDM = new DistanceMatrix(oldDM.Rows, oldDM.Columns, oldDM.ColumnNames, oldDM.RowNames);
201          newDM.SortableView = oldDM.SortableView;
202          for (int i = 0; i < newDM.Rows; i++)
203            for (int j = 0; j < newDM.Columns; j++)
204              newDM[i, j] = oldDM[i, j];
205          DistanceMatrixParameter.Value = (DistanceMatrix)newDM.AsReadOnly();
206        }
207      }
208
209      ValueParameter<DoubleMatrix> oldCoordinates = (Parameters["Coordinates"] as ValueParameter<DoubleMatrix>);
210      if (oldCoordinates != null) {
211        Parameters.Remove(oldCoordinates);
212        Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", oldCoordinates.Value, oldCoordinates.GetsCollected));
213      }
214
215      if (Operators.Count == 0) InitializeOperators();
216      #endregion
217      RegisterEventHandlers();
218    }
219
220    private void RegisterEventHandlers() {
221      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
222      if (Coordinates != null) {
223        Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
224        Coordinates.Reset += new EventHandler(Coordinates_Reset);
225      }
226      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
227      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
228    }
229
230    private void InitializeOperators() {
231      Operators.Add(new TSPImprovementOperator());
232      Operators.Add(new TSPMultipleGuidesPathRelinker());
233      Operators.Add(new TSPPathRelinker());
234      Operators.Add(new TSPSimultaneousPathRelinker());
235
236      Operators.Add(new HammingSimilarityCalculator());
237      Operators.Add(new QualitySimilarityCalculator());
238      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
239
240      Operators.Add(new BestTSPSolutionAnalyzer());
241      Operators.Add(new TSPAlleleFrequencyAnalyzer());
242      ParameterizeAnalyzers();
243      var operators = new HashSet<IPermutationOperator>(new IPermutationOperator[] {
244        new OrderCrossover2(),
245        new InversionManipulator(),
246        new StochasticInversionMultiMoveGenerator()
247      }, new TypeEqualityComparer<IPermutationOperator>());
248      foreach (var op in ApplicationManager.Manager.GetInstances<IPermutationOperator>())
249        operators.Add(op);
250      Operators.AddRange(operators);
251      ParameterizeOperators();
252      UpdateMoveEvaluators();
253    }
254    private void UpdateMoveEvaluators() {
255      Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
256      foreach (var op in ApplicationManager.Manager.GetInstances<ITSPMoveEvaluator>())
257        if (op.EvaluatorType == Evaluator.GetType()) {
258          Operators.Add(op);
259        }
260      ParameterizeOperators();
261      OnOperatorsChanged();
262    }
263    private void ParameterizeSolutionCreator() {
264      if (Evaluator is ITSPDistanceMatrixEvaluator && DistanceMatrix != null)
265        SolutionCreator.LengthParameter.Value = new IntValue(DistanceMatrix.Rows);
266      else if (Evaluator is ITSPCoordinatesPathEvaluator && Coordinates != null)
267        SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
268      else {
269        SolutionCreator.LengthParameter.Value = null;
270        string error = "The given problem does not support the selected evaluator.";
271        if (Evaluator is ITSPDistanceMatrixEvaluator)
272          error += Environment.NewLine + "Please review that the " + DistanceMatrixParameter.Name + " parameter is defined or choose another evaluator.";
273        else error += Environment.NewLine + "Please review that the " + CoordinatesParameter.Name + " parameter is defined or choose another evaluator.";
274        PluginInfrastructure.ErrorHandling.ShowErrorDialog(error, null);
275      }
276      SolutionCreator.LengthParameter.Hidden = SolutionCreator.LengthParameter.Value != null;
277      SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
278      SolutionCreator.PermutationTypeParameter.Hidden = true;
279    }
280    private void ParameterizeEvaluator() {
281      if (Evaluator is ITSPPathEvaluator) {
282        ITSPPathEvaluator evaluator = (ITSPPathEvaluator)Evaluator;
283        evaluator.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
284        evaluator.PermutationParameter.Hidden = true;
285      }
286      if (Evaluator is ITSPCoordinatesPathEvaluator) {
287        ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
288        evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
289        evaluator.CoordinatesParameter.Hidden = true;
290        evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
291        evaluator.DistanceMatrixParameter.Hidden = true;
292        evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
293        evaluator.UseDistanceMatrixParameter.Hidden = true;
294      }
295      if (Evaluator is ITSPDistanceMatrixEvaluator) {
296        var evaluator = (ITSPDistanceMatrixEvaluator)Evaluator;
297        evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
298        evaluator.DistanceMatrixParameter.Hidden = true;
299      }
300    }
301    private void ParameterizeAnalyzers() {
302      if (BestTSPSolutionAnalyzer != null) {
303        BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
304        BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
305        BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
306        BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
307        BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
308        BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
309        BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
310      }
311
312      if (TSPAlleleFrequencyAnalyzer != null) {
313        TSPAlleleFrequencyAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
314        TSPAlleleFrequencyAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
315        TSPAlleleFrequencyAnalyzer.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
316        TSPAlleleFrequencyAnalyzer.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
317        TSPAlleleFrequencyAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
318        TSPAlleleFrequencyAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
319        TSPAlleleFrequencyAnalyzer.ResultsParameter.ActualName = "Results";
320      }
321    }
322    private void ParameterizeOperators() {
323      foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
324        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
325        op.ParentsParameter.Hidden = true;
326        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
327        op.ChildParameter.Hidden = true;
328      }
329      foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
330        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
331        op.PermutationParameter.Hidden = true;
332      }
333      foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
334        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
335        op.PermutationParameter.Hidden = true;
336      }
337      foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
338        op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
339        op.CoordinatesParameter.Hidden = true;
340        op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
341        op.DistanceMatrixParameter.Hidden = true;
342        op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
343        op.UseDistanceMatrixParameter.Hidden = true;
344        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
345        op.QualityParameter.Hidden = true;
346        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
347        op.PermutationParameter.Hidden = true;
348      }
349      foreach (IPermutationMultiNeighborhoodShakingOperator op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
350        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
351        op.PermutationParameter.Hidden = true;
352      }
353      foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
354        op.SolutionParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
355        op.SolutionParameter.Hidden = true;
356      }
357      foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) {
358        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
359        op.ParentsParameter.Hidden = true;
360      }
361      foreach (ISolutionSimilarityCalculator op in Operators.OfType<ISolutionSimilarityCalculator>()) {
362        op.SolutionVariableName = SolutionCreator.PermutationParameter.ActualName;
363        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
364      }
365    }
366
367    private void ClearDistanceMatrix() {
368      DistanceMatrixParameter.Value = null;
369    }
370    #endregion
371
372    public void Load(TSPData data) {
373      if (data.Coordinates == null && data.Distances == null)
374        throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
375      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
376        || data.DistanceMeasure == DistanceMeasure.Manhattan
377        || data.DistanceMeasure == DistanceMeasure.Maximum))
378        throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
379      if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
380        throw new System.IO.InvalidDataException("The coordinates of the given instance are not in the right format, there need to be one row for each customer and two columns for the x and y coordinates.");
381
382      Name = data.Name;
383      Description = data.Description;
384
385      bool clearCoordinates = false, clearDistanceMatrix = false;
386      if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
387        Coordinates = new DoubleMatrix(data.Coordinates);
388      else clearCoordinates = true;
389
390      TSPEvaluator evaluator;
391      if (data.DistanceMeasure == DistanceMeasure.Att
392        || data.DistanceMeasure == DistanceMeasure.Manhattan
393        || data.DistanceMeasure == DistanceMeasure.Maximum) {
394        evaluator = new TSPDistanceMatrixEvaluator();
395        UseDistanceMatrix = new BoolValue(true);
396        DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
397      } else if (data.DistanceMeasure == DistanceMeasure.Direct && data.Distances != null) {
398        evaluator = new TSPDistanceMatrixEvaluator();
399        UseDistanceMatrix = new BoolValue(true);
400        DistanceMatrix = new DistanceMatrix(data.Distances);
401      } else {
402        clearDistanceMatrix = true;
403        UseDistanceMatrix = new BoolValue(data.Dimension <= DistanceMatrixSizeLimit);
404        switch (data.DistanceMeasure) {
405          case DistanceMeasure.Euclidean:
406            evaluator = new TSPEuclideanPathEvaluator();
407            break;
408          case DistanceMeasure.RoundedEuclidean:
409            evaluator = new TSPRoundedEuclideanPathEvaluator();
410            break;
411          case DistanceMeasure.UpperEuclidean:
412            evaluator = new TSPUpperEuclideanPathEvaluator();
413            break;
414          case DistanceMeasure.Geo:
415            evaluator = new TSPGeoPathEvaluator();
416            break;
417          default:
418            throw new InvalidDataException("An unknown distance measure is given in the instance!");
419        }
420      }
421      evaluator.QualityParameter.ActualName = "TSPTourLength";
422      Evaluator = evaluator;
423
424      // reset them after assigning the evaluator
425      if (clearCoordinates) Coordinates = null;
426      if (clearDistanceMatrix) DistanceMatrix = null;
427
428      BestKnownSolution = null;
429      BestKnownQuality = null;
430
431      if (data.BestKnownTour != null) {
432        try {
433          EvaluateAndLoadTour(data.BestKnownTour);
434        } catch (InvalidOperationException) {
435          if (data.BestKnownQuality.HasValue)
436            BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
437        }
438      } else if (data.BestKnownQuality.HasValue) {
439        BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
440      }
441      OnReset();
442    }
443
444    public void EvaluateAndLoadTour(int[] tour) {
445      var route = new Permutation(PermutationTypes.RelativeUndirected, tour);
446      BestKnownSolution = route;
447
448      double quality;
449      if (Evaluator is ITSPDistanceMatrixEvaluator) {
450        quality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, route);
451      } else if (Evaluator is ITSPCoordinatesPathEvaluator) {
452        quality = TSPCoordinatesPathEvaluator.Apply((TSPCoordinatesPathEvaluator)Evaluator, Coordinates, route);
453      } else {
454        throw new InvalidOperationException("Cannot calculate solution quality, evaluator type is unknown.");
455      }
456      BestKnownQuality = new DoubleValue(quality);
457    }
458  }
459}
Note: See TracBrowser for help on using the repository browser.