Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DiversityAnalysis/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/BestTSPSolutionAnalyzer.cs @ 4544

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

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.7 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.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.TravelingSalesman {
32  /// <summary>
33  /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
34  /// </summary>
35  [Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
36  [StorableClass]
37  public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
38    public LookupParameter<BoolValue> MaximizationParameter {
39      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    public LookupParameter<DoubleMatrix> CoordinatesParameter {
42      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
43    }
44    public ScopeTreeLookupParameter<Permutation> PermutationParameter {
45      get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
46    }
47    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
49    }
50    public LookupParameter<PathTSPTour> BestSolutionParameter {
51      get { return (LookupParameter<PathTSPTour>)Parameters["BestSolution"]; }
52    }
53    public ValueLookupParameter<ResultCollection> ResultsParameter {
54      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
55    }
56    public LookupParameter<DoubleValue> BestKnownQualityParameter {
57      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
58    }
59    public LookupParameter<Permutation> BestKnownSolutionParameter {
60      get { return (LookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
61    }
62
63    public BestTSPSolutionAnalyzer()
64      : base() {
65      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
66      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
67      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Permutation", "The TSP solutions given in path representation from which the best solution should be analyzed."));
68      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the TSP solutions which should be analyzed."));
69      Parameters.Add(new LookupParameter<PathTSPTour>("BestSolution", "The best TSP solution."));
70      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best TSP solution should be stored."));
71      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
72      Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
73    }
74
75    public override IOperation Apply() {
76      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
77      ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
78      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
79      ResultCollection results = ResultsParameter.ActualValue;
80      bool max = MaximizationParameter.ActualValue.Value;
81      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
82
83      int i = -1;
84      if (!max)
85        i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
86      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
87
88      if (bestKnownQuality == null ||
89          max && qualities[i].Value > bestKnownQuality.Value ||
90          !max && qualities[i].Value < bestKnownQuality.Value) {
91        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
92        BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
93      }
94
95      PathTSPTour tour = BestSolutionParameter.ActualValue;
96      if (tour == null) {
97        tour = new PathTSPTour(coordinates, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
98        BestSolutionParameter.ActualValue = tour;
99        results.Add(new Result("Best TSP Solution", tour));
100      } else {
101        if (max && tour.Quality.Value < qualities[i].Value ||
102          !max && tour.Quality.Value > qualities[i].Value) {
103          tour.Coordinates = coordinates;
104          tour.Permutation = (Permutation)permutations[i].Clone();
105          tour.Quality.Value = qualities[i].Value;
106        }
107      }
108
109      return base.Apply();
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.