Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Orienteering/3.3/Evaluators/OrienteeringEvaluator.cs @ 17751

Last change on this file since 17751 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 5.6 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.IntegerVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HEAL.Attic;
30
31namespace HeuristicLab.Problems.Orienteering {
32  [Item("OrienteeringEvaluator", "Operator to evaluate a solution to the orienteering problem.")]
33  [StorableType("ACAC24FD-9FBC-4722-BFB0-21BFEF02C2D1")]
34  public class OrienteeringEvaluator : InstrumentedOperator, IOrienteeringEvaluator {
35
36    #region ParameterProperties
37    public ILookupParameter<DoubleValue> QualityParameter {
38      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
39    }
40    public ILookupParameter<DoubleValue> PenaltyParameter {
41      get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
42    }
43    public ILookupParameter<DoubleValue> DistancePenaltyFactorParameter {
44      get { return (ILookupParameter<DoubleValue>)Parameters["DistancePenaltyFactor"]; }
45    }
46    public ILookupParameter<IntegerVector> IntegerVectorParameter {
47      get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
48    }
49    public ILookupParameter<DoubleArray> ScoresParameter {
50      get { return (ILookupParameter<DoubleArray>)Parameters["Scores"]; }
51    }
52    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
53      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
54    }
55    public ILookupParameter<DoubleValue> MaximumDistanceParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters["MaximumDistance"]; }
57    }
58    public ILookupParameter<DoubleValue> PointVisitingCostsParameter {
59      get { return (ILookupParameter<DoubleValue>)Parameters["PointVisitingCosts"]; }
60    }
61    #endregion
62
63    [StorableConstructor]
64    protected OrienteeringEvaluator(StorableConstructorFlag _) : base(_) {
65    }
66    protected OrienteeringEvaluator(OrienteeringEvaluator original, Cloner cloner)
67      : base(original, cloner) {
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new OrienteeringEvaluator(this, cloner);
71    }
72    public OrienteeringEvaluator()
73      : base() {
74      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the Orienteering solution."));
75      Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The applied penalty"));
76      Parameters.Add(new ValueLookupParameter<DoubleValue>("DistancePenaltyFactor", "The penalty applied per distance violation.", new DoubleValue(0)));
77
78      Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The Orienteering Solution given in path representation."));
79      Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
80      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the points."));
81      Parameters.Add(new LookupParameter<DoubleValue>("MaximumDistance", "The maximum distance constraint for a Orienteering solution."));
82      Parameters.Add(new LookupParameter<DoubleValue>("PointVisitingCosts", "The costs for visiting a point."));
83    }
84
85    public static OrienteeringEvaluationResult Apply(IntegerVector solution, DoubleArray scores,
86      DistanceMatrix distances, double maximumDistance, double pointVisitingCosts, double distancePenaltyFactor) {
87
88      double score = solution.Sum(t => scores[t]);
89      double distance = distances.CalculateTourLength(solution.ToList(), pointVisitingCosts);
90
91      double distanceViolation = distance - maximumDistance;
92
93      double penalty = 0.0;
94      penalty += distanceViolation > 0 ? distanceViolation * distancePenaltyFactor : 0;
95
96      double quality = score - penalty;
97
98      return new OrienteeringEvaluationResult {
99        Quality = new DoubleValue(quality),
100        Penalty = new DoubleValue(penalty),
101        Distance = new DoubleValue(distance)
102      };
103    }
104
105    public override IOperation InstrumentedApply() {
106      var evaluation = Apply(IntegerVectorParameter.ActualValue, ScoresParameter.ActualValue,
107        DistanceMatrixParameter.ActualValue, MaximumDistanceParameter.ActualValue.Value,
108        PointVisitingCostsParameter.ActualValue.Value, DistancePenaltyFactorParameter.ActualValue.Value);
109
110      QualityParameter.ActualValue = evaluation.Quality;
111      PenaltyParameter.ActualValue = evaluation.Penalty;
112
113      return base.InstrumentedApply();
114    }
115
116    public OrienteeringEvaluationResult Evaluate(IntegerVector solution, DoubleArray scores,
117      DistanceMatrix distances, double maximumDistance, double pointVisitingCosts) {
118      return Apply(solution, scores, distances, maximumDistance, pointVisitingCosts,
119        ((IValueParameter<DoubleValue>)DistancePenaltyFactorParameter).Value.Value);
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.