Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/Analyzers/BestOrienteeringSolutionAnalyser.cs @ 11319

Last change on this file since 11319 was 11319, checked in by pfleck, 10 years ago

#2208 Renamed TerminusPoint to TerminalPoint

File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.IntegerVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.Orienteering {
33  public sealed class BestOrienteeringSolutionAnalyser : SingleSuccessorOperator, IAnalyzer {
34    public bool EnabledByDefault {
35      get { return true; }
36    }
37
38    public IScopeTreeLookupParameter<IntegerVector> IntegerVector {
39      get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
40    }
41    public LookupParameter<DoubleMatrix> CoordinatesParameter {
42      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
43    }
44    public ILookupParameter<IntValue> StartingPointParameter {
45      get { return (ILookupParameter<IntValue>)Parameters["StartingPoint"]; }
46    }
47    public ILookupParameter<IntValue> TerminalPointParameter {
48      get { return (ILookupParameter<IntValue>)Parameters["TerminalPoint"]; }
49    }
50    public ILookupParameter<DoubleArray> ScoresParameter {
51      get { return (ILookupParameter<DoubleArray>)Parameters["Scores"]; }
52    }
53
54    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
55      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
56    }
57    public ScopeTreeLookupParameter<DoubleValue> PenaltyParameter {
58      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Penalty"]; }
59    }
60    public LookupParameter<OrienteeringSolution> BestSolutionParameter {
61      get { return (LookupParameter<OrienteeringSolution>)Parameters["BestSolution"]; }
62    }
63    public ValueLookupParameter<ResultCollection> ResultsParameter {
64      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
65    }
66    public LookupParameter<DoubleValue> BestKnownQualityParameter {
67      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
68    }
69    public LookupParameter<IntegerVector> BestKnownSolutionParameter {
70      get { return (LookupParameter<IntegerVector>)Parameters["BestKnownSolution"]; }
71    }
72    public LookupParameter<DoubleValue> BestKnownSolutionPenaltyParameter {
73      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownSolutionPenalty"]; }
74    }
75
76    [StorableConstructor]
77    private BestOrienteeringSolutionAnalyser(bool deserializing) : base(deserializing) { }
78    private BestOrienteeringSolutionAnalyser(BestOrienteeringSolutionAnalyser original, Cloner cloner) : base(original, cloner) { }
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new BestOrienteeringSolutionAnalyser(this, cloner);
81    }
82    public BestOrienteeringSolutionAnalyser()
83      : base() {
84      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("IntegerVector", "The Orienteering solutions which should be analysed."));
85      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the points."));
86      Parameters.Add(new LookupParameter<IntValue>("StartingPoint", "Index of the starting point."));
87      Parameters.Add(new LookupParameter<IntValue>("TerminalPoint", "Index of the ending point."));
88      Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
89      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the Orienteering solutions which should be analyzed."));
90      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Penalty", "The applied penalty of the Orienteering solutions."));
91      Parameters.Add(new LookupParameter<OrienteeringSolution>("BestSolution", "The best Orienteering solution."));
92      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best Orienteering solution should be stored."));
93      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this Orienteering instance."));
94      Parameters.Add(new LookupParameter<IntegerVector>("BestKnownSolution", "The best known solution of this Orienteering instance."));
95      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownSolutionPenalty", "The penalty of the best known solution of this Orienteering instance."));
96    }
97
98    public override IOperation Apply() {
99      var solutions = IntegerVector.ActualValue;
100      var qualities = QualityParameter.ActualValue;
101      var penalties = PenaltyParameter.ActualValue;
102      var results = ResultsParameter.ActualValue;
103      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
104
105      int bestIndex = qualities.Select((quality, index) => new { index, quality.Value }).OrderByDescending(x => x.Value).First().index;
106
107      if (bestKnownQuality == null || qualities[bestIndex].Value > bestKnownQuality.Value) {
108        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
109        BestKnownSolutionParameter.ActualValue = (IntegerVector)solutions[bestIndex].Clone();
110        BestKnownSolutionPenaltyParameter.ActualValue = new DoubleValue(penalties[bestIndex].Value);
111      }
112
113      var solution = BestSolutionParameter.ActualValue;
114      var coordinates = CoordinatesParameter.ActualValue;
115      var startingPoint = StartingPointParameter.ActualValue;
116      var terminalPoint = TerminalPointParameter.ActualValue;
117      var scores = ScoresParameter.ActualValue;
118      if (solution == null) {
119        solution = new OrienteeringSolution(
120          (IntegerVector)solutions[bestIndex].Clone(),
121          coordinates,
122          startingPoint,
123          terminalPoint,
124          scores,
125          new DoubleValue(qualities[bestIndex].Value),
126          new DoubleValue(penalties[bestIndex].Value));
127        BestSolutionParameter.ActualValue = solution;
128        results.Add(new Result("Best Orienteering Solution", solution));
129        results.Add(new Result("BestKnownSolutionPenalty", solution.Penalty));
130      } else {
131        if (solution.Quality.Value < qualities[bestIndex].Value) {
132          solution.Coordinates = coordinates;
133          solution.Scores = scores;
134          solution.IntegerVector = (IntegerVector)solutions[bestIndex].Clone();
135          solution.Quality.Value = qualities[bestIndex].Value;
136          solution.Penalty.Value = penalties[bestIndex].Value;
137          results["BestKnownSolutionPenalty"].Value = solution.Penalty;
138        }
139      }
140
141      return base.Apply();
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.