Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2208 Renamed FixedPenalty to PointVisitingCosts

File size: 5.3 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 System.Security.Permissions;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.Orienteering {
33  public class OrienteeringEvaluator : InstrumentedOperator, IOrienteeringEvaluator {
34
35    #region ParameterProperties
36    public ILookupParameter<DoubleValue> QualityParameter {
37      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
38    }
39    public ILookupParameter<DoubleValue> PenaltyParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
41    }
42    public ILookupParameter<DoubleValue> DistancePenaltyFactorParameter {
43      get { return (ILookupParameter<DoubleValue>)Parameters["DistancePenaltyFactor"]; }
44    }
45    public ILookupParameter<IntegerVector> IntegerVectorParameter {
46      get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
47    }
48    public ILookupParameter<DoubleArray> ScoresParameter {
49      get { return (ILookupParameter<DoubleArray>)Parameters["Scores"]; }
50    }
51    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
52      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
53    }
54    public ILookupParameter<DoubleValue> MaximumDistanceParameter {
55      get { return (ILookupParameter<DoubleValue>)Parameters["MaximumDistance"]; }
56    }
57    public ILookupParameter<DoubleValue> PointVisitingCostsParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["PointVisitingCosts"]; }
59    }
60    #endregion
61
62    [StorableConstructor]
63    protected OrienteeringEvaluator(bool deserializing)
64      : base(deserializing) {
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 class OrienteeringEvaluation {
86      public DoubleValue Quality;
87      public DoubleValue Penalty;
88    }
89
90    public static OrienteeringEvaluation Apply(IntegerVector solution, DoubleArray scores,
91      DistanceMatrix distances, DoubleValue maximumDistance, DoubleValue pointVisitingCosts, DoubleValue distancePenaltyFactor) {
92
93      double score = solution.Sum(t => scores[t]);
94      double distance = distances.CalculateTourLength(solution.ToList(), pointVisitingCosts.Value);
95
96      double distanceViolation = distance - maximumDistance.Value;
97
98      double penalty = 0.0;
99      penalty += distanceViolation > 0 ? distanceViolation * distancePenaltyFactor.Value : 0;
100
101      double quality = score - penalty;
102
103      return new OrienteeringEvaluation {
104        Quality = new DoubleValue(quality),
105        Penalty = new DoubleValue(penalty)
106      };
107    }
108
109    public override IOperation InstrumentedApply() {
110      var evaluation = Apply(IntegerVectorParameter.ActualValue, ScoresParameter.ActualValue,
111        DistanceMatrixParameter.ActualValue, MaximumDistanceParameter.ActualValue,
112        PointVisitingCostsParameter.ActualValue, DistancePenaltyFactorParameter.ActualValue);
113
114      QualityParameter.ActualValue = evaluation.Quality;
115      PenaltyParameter.ActualValue = evaluation.Penalty;
116
117      return base.InstrumentedApply();
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.