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