Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/Evaluators/OrienteeringEvaluator.cs @ 11327

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

#2208

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