Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 4.2 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, IBestTSPSolutionAnalyzer, ISolutionAnalyzer {
39    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
40      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
41    }
42    public ILookupParameter<Permutation> PermutationParameter {
43      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
44    }
45    ILookupParameter IBestTSPSolutionAnalyzer.PermutationParameter {
46      get { return PermutationParameter; }
47    }
48    public ILookupParameter<DoubleValue> QualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
50    }
51    ILookupParameter IBestTSPSolutionAnalyzer.QualityParameter {
52      get { return QualityParameter; }
53    }
54    public ILookupParameter<PathTSPTour> BestSolutionParameter {
55      get { return (ILookupParameter<PathTSPTour>)Parameters["BestSolution"]; }
56    }
57    public IValueLookupParameter<ResultCollection> ResultsParameter {
58      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
59    }
60
61    public BestTSPSolutionAnalyzer()
62      : base() {
63      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
64      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be analyzed."));
65      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the TSP solution which should be analyzed."));
66      Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution."));
67      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the TSP solution should be stored."));
68    }
69
70    public override IOperation Apply() {
71      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
72      Permutation permutation = PermutationParameter.ActualValue;
73      DoubleValue quality = QualityParameter.ActualValue;
74      ResultCollection results = ResultsParameter.ActualValue;
75
76      PathTSPTour tour = BestSolutionParameter.ActualValue;
77      if (tour == null) {
78        tour = new PathTSPTour(coordinates, permutation, quality);
79        BestSolutionParameter.ActualValue = tour;
80        results.Add(new Result("Best TSP Solution", tour));
81      } else {
82        if (tour.Quality.Value > quality.Value) {
83          tour.Coordinates = coordinates;
84          tour.Permutation = permutation;
85          tour.Quality = quality;
86          results["Best TSP Solution"].Value = tour;
87        }
88      }
89
90      return base.Apply();
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.