Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs @ 12069

Last change on this file since 12069 was 12069, checked in by jkarder, 9 years ago

#2332: refactored operators and analyzers

  • removed quality and maximization parameters in SingleObjectivePopulationDiversityAnalyzer
  • renamed SingleObjectivePopulationDiversityAnalyzer to PopulationSimilarityAnalyzer
  • added ConstrainedValueParameter for similarity calculators of analyzer
  • added ValueLookupParameter for similarity calculator of the following operators:
    • DuplicatesSelector, ProgressiveOffspringPreserver, ReferenceSetUpdateMethod, SolutionPoolUpdateMethod
  • removed some wiring code in specific problems
File size: 22.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Problems.Instances;
36
37namespace HeuristicLab.Problems.TravelingSalesman {
38  [Item("Traveling Salesman Problem", "Represents a symmetric Traveling Salesman Problem.")]
39  [Creatable("Problems")]
40  [StorableClass]
41  public sealed class TravelingSalesmanProblem : SingleObjectiveHeuristicOptimizationProblem<ITSPEvaluator, IPermutationCreator>, IStorableContent,
42    IProblemInstanceConsumer<TSPData> {
43    private static readonly int DistanceMatrixSizeLimit = 1000;
44    public string Filename { get; set; }
45
46    #region Parameter Properties
47    public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
48      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
49    }
50    public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
51      get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
52    }
53    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
54      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
55    }
56    public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
57      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
58    }
59    #endregion
60
61    #region Properties
62    public DoubleMatrix Coordinates {
63      get { return CoordinatesParameter.Value; }
64      set { CoordinatesParameter.Value = value; }
65    }
66    public DistanceMatrix DistanceMatrix {
67      get { return DistanceMatrixParameter.Value; }
68      set { DistanceMatrixParameter.Value = value; }
69    }
70    public BoolValue UseDistanceMatrix {
71      get { return UseDistanceMatrixParameter.Value; }
72      set { UseDistanceMatrixParameter.Value = value; }
73    }
74    public Permutation BestKnownSolution {
75      get { return BestKnownSolutionParameter.Value; }
76      set { BestKnownSolutionParameter.Value = value; }
77    }
78    private BestTSPSolutionAnalyzer BestTSPSolutionAnalyzer {
79      get { return Operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }
80    }
81    private TSPAlleleFrequencyAnalyzer TSPAlleleFrequencyAnalyzer {
82      get { return Operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }
83    }
84    private PopulationSimilarityAnalyzer PopulationSimilarityAnalyzer {
85      get { return Operators.OfType<PopulationSimilarityAnalyzer>().FirstOrDefault(); }
86    }
87    #endregion
88
89    // BackwardsCompatibility3.3
90    #region Backwards compatible code, remove with 3.4
91    [Obsolete]
92    [Storable(Name = "operators")]
93    private IEnumerable<IOperator> oldOperators {
94      get { return null; }
95      set {
96        if (value != null && value.Any())
97          Operators.AddRange(value);
98      }
99    }
100    #endregion
101
102    [StorableConstructor]
103    private TravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
104    private TravelingSalesmanProblem(TravelingSalesmanProblem original, Cloner cloner)
105      : base(original, cloner) {
106      RegisterEventHandlers();
107    }
108    public override IDeepCloneable Clone(Cloner cloner) {
109      return new TravelingSalesmanProblem(this, cloner);
110    }
111    public TravelingSalesmanProblem()
112      : base(new TSPRoundedEuclideanPathEvaluator(), new RandomPermutationCreator()) {
113      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
114      Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
115      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)));
116      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
117
118      Maximization.Value = false;
119      MaximizationParameter.Hidden = true;
120      UseDistanceMatrixParameter.Hidden = true;
121      DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
122
123      Coordinates = new DoubleMatrix(new double[,] {
124        { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
125        { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
126        { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
127        { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
128      });
129
130      SolutionCreator.PermutationParameter.ActualName = "TSPTour";
131      Evaluator.QualityParameter.ActualName = "TSPTourLength";
132      ParameterizeSolutionCreator();
133      ParameterizeEvaluator();
134
135      InitializeOperators();
136      RegisterEventHandlers();
137    }
138
139    #region Events
140    protected override void OnSolutionCreatorChanged() {
141      base.OnSolutionCreatorChanged();
142      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
143      ParameterizeSolutionCreator();
144      ParameterizeEvaluator();
145      ParameterizeAnalyzers();
146      ParameterizeOperators();
147    }
148    protected override void OnEvaluatorChanged() {
149      base.OnEvaluatorChanged();
150      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
151      ParameterizeEvaluator();
152      ParameterizeSolutionCreator();
153      UpdateMoveEvaluators();
154      ParameterizeAnalyzers();
155      if (Evaluator is ITSPCoordinatesPathEvaluator && Coordinates != null)
156        ClearDistanceMatrix();
157    }
158    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
159      if (Coordinates != null) {
160        Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
161        Coordinates.Reset += new EventHandler(Coordinates_Reset);
162      }
163      if (Evaluator is ITSPCoordinatesPathEvaluator) {
164        ParameterizeSolutionCreator();
165        ClearDistanceMatrix();
166      }
167    }
168    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
169      if (Evaluator is ITSPCoordinatesPathEvaluator) {
170        ClearDistanceMatrix();
171      }
172    }
173    private void Coordinates_Reset(object sender, EventArgs e) {
174      if (Evaluator is ITSPCoordinatesPathEvaluator) {
175        ParameterizeSolutionCreator();
176        ClearDistanceMatrix();
177      }
178    }
179    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
180      ParameterizeEvaluator();
181      ParameterizeAnalyzers();
182      ParameterizeOperators();
183    }
184    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
185      ParameterizeAnalyzers();
186    }
187    #endregion
188
189    #region Helpers
190    [StorableHook(HookType.AfterDeserialization)]
191    private void AfterDeserialization() {
192      // BackwardsCompatibility3.3
193      #region Backwards compatible code (remove with 3.4)
194      OptionalValueParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as OptionalValueParameter<DoubleMatrix>;
195      if (oldDistanceMatrixParameter != null) {
196        Parameters.Remove(oldDistanceMatrixParameter);
197        Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
198        DistanceMatrixParameter.GetsCollected = oldDistanceMatrixParameter.GetsCollected;
199        DistanceMatrixParameter.ReactOnValueToStringChangedAndValueItemImageChanged = false;
200        if (oldDistanceMatrixParameter.Value != null) {
201          DoubleMatrix oldDM = oldDistanceMatrixParameter.Value;
202          DistanceMatrix newDM = new DistanceMatrix(oldDM.Rows, oldDM.Columns, oldDM.ColumnNames, oldDM.RowNames);
203          newDM.SortableView = oldDM.SortableView;
204          for (int i = 0; i < newDM.Rows; i++)
205            for (int j = 0; j < newDM.Columns; j++)
206              newDM[i, j] = oldDM[i, j];
207          DistanceMatrixParameter.Value = (DistanceMatrix)newDM.AsReadOnly();
208        }
209      }
210
211      ValueParameter<DoubleMatrix> oldCoordinates = (Parameters["Coordinates"] as ValueParameter<DoubleMatrix>);
212      if (oldCoordinates != null) {
213        Parameters.Remove(oldCoordinates);
214        Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", oldCoordinates.Value, oldCoordinates.GetsCollected));
215      }
216
217      if (Operators.Count == 0) InitializeOperators();
218      #endregion
219      RegisterEventHandlers();
220    }
221
222    private void RegisterEventHandlers() {
223      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
224      if (Coordinates != null) {
225        Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
226        Coordinates.Reset += new EventHandler(Coordinates_Reset);
227      }
228      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
229      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
230    }
231
232    private void InitializeOperators() {
233      Operators.Add(new TSPImprovementOperator());
234      Operators.Add(new TSPMultipleGuidesPathRelinker());
235      Operators.Add(new TSPPathRelinker());
236      Operators.Add(new TSPSimultaneousPathRelinker());
237      Operators.Add(new TSPSimilarityCalculator());
238
239      Operators.Add(new BestTSPSolutionAnalyzer());
240      Operators.Add(new TSPAlleleFrequencyAnalyzer());
241      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
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 (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
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 (TSPSimilarityCalculator op in Operators.OfType<TSPSimilarityCalculator>()) {
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        }
435        catch (InvalidOperationException) {
436          if (data.BestKnownQuality.HasValue)
437            BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
438        }
439      } else if (data.BestKnownQuality.HasValue) {
440        BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
441      }
442      OnReset();
443    }
444
445    public void EvaluateAndLoadTour(int[] tour) {
446      var route = new Permutation(PermutationTypes.RelativeUndirected, tour);
447      BestKnownSolution = route;
448
449      double quality;
450      if (Evaluator is ITSPDistanceMatrixEvaluator) {
451        quality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, route);
452      } else if (Evaluator is ITSPCoordinatesPathEvaluator) {
453        quality = TSPCoordinatesPathEvaluator.Apply((TSPCoordinatesPathEvaluator)Evaluator, Coordinates, route);
454      } else {
455        throw new InvalidOperationException("Cannot calculate solution quality, evaluator type is unknown.");
456      }
457      BestKnownQuality = new DoubleValue(quality);
458    }
459  }
460}
Note: See TracBrowser for help on using the repository browser.