Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/BestTSPSolutionAnalyzer.cs @ 3634

Last change on this file since 3634 was 3634, checked in by swagner, 14 years ago

Worked on best solution analysis for the TSP (#999)

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.TravelingSalesman {
33  /// <summary>
34  /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
35  /// </summary>
36  [Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
37  [StorableClass]
38  public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, ISolutionAnalyzer {
39    public LookupParameter<DoubleMatrix> CoordinatesParameter {
40      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
41    }
42    public LookupParameter<Permutation> PermutationParameter {
43      get { return (LookupParameter<Permutation>)Parameters["Permutation"]; }
44    }
45    public LookupParameter<DoubleValue> QualityParameter {
46      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
47    }
48    public LookupParameter<PathTSPTour> BestSolutionParameter {
49      get { return (LookupParameter<PathTSPTour>)Parameters["BestSolution"]; }
50    }
51    public ValueLookupParameter<ResultCollection> ResultsParameter {
52      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
53    }
54
55    public BestTSPSolutionAnalyzer()
56      : base() {
57      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
58      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be analyzed."));
59      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the TSP solution which should be analyzed."));
60      Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution."));
61      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the TSP solution should be stored."));
62    }
63
64    public override IOperation Apply() {
65      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
66      Permutation permutation = PermutationParameter.ActualValue;
67      DoubleValue quality = QualityParameter.ActualValue;
68      ResultCollection results = ResultsParameter.ActualValue;
69
70      PathTSPTour tour = BestSolutionParameter.ActualValue;
71      if (tour == null) {
72        tour = new PathTSPTour(coordinates, permutation, quality);
73        BestSolutionParameter.ActualValue = tour;
74        results.Add(new Result("Best TSP Solution", tour));
75      } else {
76        if (tour.Quality.Value > quality.Value) {
77          tour.Coordinates = coordinates;
78          tour.Permutation = permutation;
79          tour.Quality = quality;
80          results["Best TSP Solution"].Value = tour;
81        }
82      }
83
84      return base.Apply();
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.