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