Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/PopulationBestTSPSolutionAnalyzer.cs @ 3658

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

Worked on refactoring of algorithm analysis and tracing (#999)

  • removed specific analyzer interfaces
  • adapted MultiAnalyzer
File size: 4.5 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("PopulationBestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
37  [StorableClass]
38  public sealed class PopulationBestTSPSolutionAnalyzer : SingleSuccessorOperator, IBestTSPSolutionAnalyzer, IAnalyzer {
39    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
40      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
41    }
42    public ILookupParameter<ItemArray<Permutation>> PermutationParameter {
43      get { return (ILookupParameter<ItemArray<Permutation>>)Parameters["Permutation"]; }
44    }
45    ILookupParameter IBestTSPSolutionAnalyzer.PermutationParameter {
46      get { return PermutationParameter; }
47    }
48    public ILookupParameter<ItemArray<DoubleValue>> QualityParameter {
49      get { return (ILookupParameter<ItemArray<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 PopulationBestTSPSolutionAnalyzer()
62      : base() {
63      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
64      Parameters.Add(new SubScopesLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
65      Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions 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 best TSP solution should be stored."));
68    }
69
70    public override IOperation Apply() {
71      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
72      ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
73      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
74      ResultCollection results = ResultsParameter.ActualValue;
75
76      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
77
78      PathTSPTour tour = BestSolutionParameter.ActualValue;
79      if (tour == null) {
80        tour = new PathTSPTour(coordinates, permutations[i], qualities[i]);
81        BestSolutionParameter.ActualValue = tour;
82        results.Add(new Result("Best TSP Solution", tour));
83      } else {
84        if (tour.Quality.Value > qualities[i].Value) {
85          tour.Coordinates = coordinates;
86          tour.Permutation = permutations[i];
87          tour.Quality = qualities[i];
88          results["Best TSP Solution"].Value = tour;
89        }
90      }
91
92      return base.Apply();
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.