Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.PTSP/3.3/Analyzers/BestPTSPSolutionAnalyzer.cs @ 17399

Last change on this file since 17399 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
31
32namespace HeuristicLab.Problems.PTSP {
33  /// <summary>
34  /// An operator for analyzing the best solution of probabilistic traveling salesman problems given in path representation.
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  [StorableType("459505F0-379B-4903-A388-8612FD732EE7")]
38  public sealed class BestPTSPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
39    public bool EnabledByDefault {
40      get { return true; }
41    }
42
43    public ILookupParameter<BoolValue> MaximizationParameter {
44      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
47      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
48    }
49    public IScopeTreeLookupParameter<Permutation> PermutationParameter {
50      get { return (IScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
51    }
52    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
54    }
55    public ILookupParameter<DoubleArray> ProbabilitiesParameter {
56      get { return (ILookupParameter<DoubleArray>)Parameters["Probabilities"]; }
57    }
58    public ILookupParameter<PathPTSPTour> BestSolutionParameter {
59      get { return (ILookupParameter<PathPTSPTour>)Parameters["BestSolution"]; }
60    }
61    public IValueLookupParameter<ResultCollection> ResultsParameter {
62      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
63    }
64    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
66    }
67    public ILookupParameter<Permutation> BestKnownSolutionParameter {
68      get { return (ILookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
69    }
70
71    [StorableConstructor]
72    private BestPTSPSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
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."));
83      Parameters.Add(new LookupParameter<DoubleArray>("Probabilities", "This list describes for each city the probability of appearing in a realized instance."));
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() {
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;
98
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;
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
109      var tour = BestSolutionParameter.ActualValue;
110      if (tour == null) {
111        tour = new PathPTSPTour(coordinates, probabilities, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
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.