Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2845_EnhancedProgress/HeuristicLab.Problems.PTSP/3.3/Analyzers/BestPTSPSolutionAnalyzer.cs @ 16386

Last change on this file since 16386 was 16311, checked in by pfleck, 6 years ago

#2845 Merged trunk changes into branch (15406-15681, 15683-16308)

File size: 6.5 KB
RevLine 
[12799]1#region License Information
2/* HeuristicLab
[16311]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12799]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.PTSP {
33  /// <summary>
[13412]34  /// An operator for analyzing the best solution of probabilistic traveling salesman problems given in path representation.
[12799]35  /// </summary>
36  [Item("BestPTSPSolutionAnalyzer", "An operator for analyzing the best solution of Probabilistic Traveling Salesman Problems given in path representation using city coordinates.")]
37  [StorableClass]
38  public sealed class BestPTSPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
39    public bool EnabledByDefault {
40      get { return true; }
41    }
42
[13412]43    public ILookupParameter<BoolValue> MaximizationParameter {
44      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
[12799]45    }
[13412]46    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
47      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
[12799]48    }
[13412]49    public IScopeTreeLookupParameter<Permutation> PermutationParameter {
50      get { return (IScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
[12799]51    }
[13412]52    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[12799]54    }
[13412]55    public ILookupParameter<DoubleArray> ProbabilitiesParameter {
56      get { return (ILookupParameter<DoubleArray>)Parameters["Probabilities"]; }
[12799]57    }
[13412]58    public ILookupParameter<PathPTSPTour> BestSolutionParameter {
59      get { return (ILookupParameter<PathPTSPTour>)Parameters["BestSolution"]; }
[12799]60    }
[13412]61    public IValueLookupParameter<ResultCollection> ResultsParameter {
62      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
[12799]63    }
[13412]64    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
[12799]66    }
[13412]67    public ILookupParameter<Permutation> BestKnownSolutionParameter {
68      get { return (ILookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
[12799]69    }
70
71    [StorableConstructor]
72    private BestPTSPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
73    private BestPTSPSolutionAnalyzer(BestPTSPSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new BestPTSPSolutionAnalyzer(this, cloner);
76    }
77    public BestPTSPSolutionAnalyzer()
78      : base() {
79      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
80      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
81      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Permutation", "The PTSP solutions given in path representation from which the best solution should be analyzed."));
82      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the PTSP solutions which should be analyzed."));
[13412]83      Parameters.Add(new LookupParameter<DoubleArray>("Probabilities", "This list describes for each city the probability of appearing in a realized instance."));
[12799]84      Parameters.Add(new LookupParameter<PathPTSPTour>("BestSolution", "The best PTSP solution."));
85      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best PTSP solution should be stored."));
86      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this PTSP instance."));
87      Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution of this PTSP instance."));
88    }
89
90    public override IOperation Apply() {
[13412]91      var coordinates = CoordinatesParameter.ActualValue;
92      var permutations = PermutationParameter.ActualValue;
93      var qualities = QualityParameter.ActualValue;
94      var probabilities = ProbabilitiesParameter.ActualValue;
95      var results = ResultsParameter.ActualValue;
96      var max = MaximizationParameter.ActualValue.Value;
97      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
[12799]98
[13412]99      var i = !max ? qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index
100                   : qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
[12799]101
102      if (bestKnownQuality == null ||
103          max && qualities[i].Value > bestKnownQuality.Value ||
104          !max && qualities[i].Value < bestKnownQuality.Value) {
105        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
106        BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
107      }
108
[13412]109      var tour = BestSolutionParameter.ActualValue;
[12799]110      if (tour == null) {
[13412]111        tour = new PathPTSPTour(coordinates, probabilities, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
[12799]112        BestSolutionParameter.ActualValue = tour;
113        results.Add(new Result("Best PTSP Solution", tour));
114      } else {
115        if (max && tour.Quality.Value < qualities[i].Value ||
116          !max && tour.Quality.Value > qualities[i].Value) {
117          tour.Coordinates = coordinates;
118          tour.Permutation = (Permutation)permutations[i].Clone();
119          tour.Quality.Value = qualities[i].Value;
120        }
121      }
122
123      return base.Apply();
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.