Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 5.7 KB
RevLine 
[3097]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
[3107]22using System.Linq;
[3097]23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Operators;
[3107]27using HeuristicLab.Optimization;
[3097]28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
[3158]31namespace HeuristicLab.Problems.TravelingSalesman {
[3097]32  /// <summary>
[3616]33  /// An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
[3097]34  /// </summary>
[3663]35  [Item("BestTSPSolutionAnalyzer", "An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
[3097]36  [StorableClass]
[3663]37  public sealed class BestTSPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
[3787]38    public LookupParameter<BoolValue> MaximizationParameter {
39      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
[3662]41    public LookupParameter<DoubleMatrix> CoordinatesParameter {
42      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
[3097]43    }
[3662]44    public ScopeTreeLookupParameter<Permutation> PermutationParameter {
45      get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
[3097]46    }
[3662]47    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3097]49    }
[3662]50    public LookupParameter<PathTSPTour> BestSolutionParameter {
51      get { return (LookupParameter<PathTSPTour>)Parameters["BestSolution"]; }
[3107]52    }
[3662]53    public ValueLookupParameter<ResultCollection> ResultsParameter {
54      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
[3107]55    }
[3787]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    }
[3097]62
[3663]63    public BestTSPSolutionAnalyzer()
[3097]64      : base() {
[3787]65      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
[3097]66      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
[3659]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."));
[3616]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."));
[3787]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."));
[3097]73    }
74
75    public override IOperation Apply() {
76      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
[3107]77      ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
78      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
[3616]79      ResultCollection results = ResultsParameter.ActualValue;
[3787]80      bool max = MaximizationParameter.ActualValue.Value;
81      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
[3107]82
[3787]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;
[4068]87
[3787]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      }
[3107]94
[3616]95      PathTSPTour tour = BestSolutionParameter.ActualValue;
96      if (tour == null) {
[3692]97        tour = new PathTSPTour(coordinates, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
[3616]98        BestSolutionParameter.ActualValue = tour;
99        results.Add(new Result("Best TSP Solution", tour));
100      } else {
[3787]101        if (max && tour.Quality.Value < qualities[i].Value ||
102          !max && tour.Quality.Value > qualities[i].Value) {
[3616]103          tour.Coordinates = coordinates;
[3692]104          tour.Permutation = (Permutation)permutations[i].Clone();
105          tour.Quality.Value = qualities[i].Value;
[3616]106        }
[3107]107      }
[3616]108
[3097]109      return base.Apply();
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.