Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Analyzers/BestPTSPSolutionAnalyzer.cs @ 12962

Last change on this file since 12962 was 12799, checked in by apolidur, 9 years ago

#2221: Adding path Analyzers and some other fixes

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.PTSP {
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("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
43    public LookupParameter<BoolValue> MaximizationParameter {
44      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public LookupParameter<DoubleMatrix> CoordinatesParameter {
47      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
48    }
49    public ScopeTreeLookupParameter<Permutation> PermutationParameter {
50      get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
51    }
52    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
54    }
55    public LookupParameter<DoubleArray> ProbabilityMatrixParameter {
56      get { return (LookupParameter<DoubleArray>)Parameters["ProbabilityMatrix"]; }
57    }
58    public LookupParameter<PathPTSPTour> BestSolutionParameter {
59      get { return (LookupParameter<PathPTSPTour>)Parameters["BestSolution"]; }
60    }
61    public ValueLookupParameter<ResultCollection> ResultsParameter {
62      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
63    }
64    public LookupParameter<DoubleValue> BestKnownQualityParameter {
65      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
66    }
67    public LookupParameter<Permutation> BestKnownSolutionParameter {
68      get { return (LookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
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."));
83      Parameters.Add(new LookupParameter<DoubleArray>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
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      MaximizationParameter.Hidden = true;
90      CoordinatesParameter.Hidden = true;
91      PermutationParameter.Hidden = true;
92      QualityParameter.Hidden = true;
93      ProbabilityMatrixParameter.Hidden = true;
94      BestSolutionParameter.Hidden = true;
95      ResultsParameter.Hidden = true;
96      BestKnownQualityParameter.Hidden = true;
97      BestKnownSolutionParameter.Hidden = true;
98    }
99
100    public override IOperation Apply() {
101      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
102      ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
103      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
104      DoubleArray probabilities = ProbabilityMatrixParameter.ActualValue;
105      ResultCollection results = ResultsParameter.ActualValue;
106      bool max = MaximizationParameter.ActualValue.Value;
107      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
108
109      int i = -1;
110      if (!max)
111        i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
112      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
113
114      if (bestKnownQuality == null ||
115          max && qualities[i].Value > bestKnownQuality.Value ||
116          !max && qualities[i].Value < bestKnownQuality.Value) {
117        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
118        BestKnownSolutionParameter.ActualValue = (Permutation)permutations[i].Clone();
119      }
120
121      PathPTSPTour tour = BestSolutionParameter.ActualValue;
122      if (tour == null) {
123        tour = new PathPTSPTour(coordinates,probabilities, (Permutation)permutations[i].Clone(), new DoubleValue(qualities[i].Value));
124        BestSolutionParameter.ActualValue = tour;
125        results.Add(new Result("Best PTSP Solution", tour));
126      } else {
127        if (max && tour.Quality.Value < qualities[i].Value ||
128          !max && tour.Quality.Value > qualities[i].Value) {
129          tour.Coordinates = coordinates;
130          tour.Permutation = (Permutation)permutations[i].Clone();
131          tour.Quality.Value = qualities[i].Value;
132        }
133      }
134
135      return base.Apply();
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.