Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15802 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 5.7 KB
RevLine 
[11186]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11186]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
[11311]22using System.Linq;
[11186]23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
[11187]26using HeuristicLab.Encodings.IntegerVectorEncoding;
[11186]27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
[14927]29using HeuristicLab.Persistence;
[11186]30
31namespace HeuristicLab.Problems.Orienteering {
[12721]32  [Item("OrienteeringEvaluator", "Operator to evaluate a solution to the orienteering problem.")]
[14927]33  [StorableType("7631d1b0-b623-48ff-92eb-2fb4b5d78705")]
[11186]34  public class OrienteeringEvaluator : InstrumentedOperator, IOrienteeringEvaluator {
[11311]35
36    #region ParameterProperties
[11186]37    public ILookupParameter<DoubleValue> QualityParameter {
38      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
39    }
[11311]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    }
[11194]46    public ILookupParameter<IntegerVector> IntegerVectorParameter {
[11191]47      get { return (ILookupParameter<IntegerVector>)Parameters["IntegerVector"]; }
[11187]48    }
49    public ILookupParameter<DoubleArray> ScoresParameter {
50      get { return (ILookupParameter<DoubleArray>)Parameters["Scores"]; }
51    }
[11311]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    }
[11320]58    public ILookupParameter<DoubleValue> PointVisitingCostsParameter {
59      get { return (ILookupParameter<DoubleValue>)Parameters["PointVisitingCosts"]; }
[11311]60    }
61    #endregion
[11186]62
63    [StorableConstructor]
[15018]64    protected OrienteeringEvaluator(StorableConstructorFlag deserializing)
[11186]65      : base(deserializing) {
66    }
67    protected OrienteeringEvaluator(OrienteeringEvaluator original, Cloner cloner)
68      : base(original, cloner) {
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new OrienteeringEvaluator(this, cloner);
72    }
73    public OrienteeringEvaluator()
74      : base() {
75      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the Orienteering solution."));
[11311]76      Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The applied penalty"));
77      Parameters.Add(new ValueLookupParameter<DoubleValue>("DistancePenaltyFactor", "The penalty applied per distance violation.", new DoubleValue(0)));
78
[11191]79      Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The Orienteering Solution given in path representation."));
[11187]80      Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
[11311]81      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the points."));
82      Parameters.Add(new LookupParameter<DoubleValue>("MaximumDistance", "The maximum distance constraint for a Orienteering solution."));
[11320]83      Parameters.Add(new LookupParameter<DoubleValue>("PointVisitingCosts", "The costs for visiting a point."));
[11186]84    }
85
[12721]86    public static OrienteeringEvaluationResult Apply(IntegerVector solution, DoubleArray scores,
[11327]87      DistanceMatrix distances, double maximumDistance, double pointVisitingCosts, double distancePenaltyFactor) {
[11187]88
[11311]89      double score = solution.Sum(t => scores[t]);
[11327]90      double distance = distances.CalculateTourLength(solution.ToList(), pointVisitingCosts);
[11187]91
[11327]92      double distanceViolation = distance - maximumDistance;
[11311]93
94      double penalty = 0.0;
[11327]95      penalty += distanceViolation > 0 ? distanceViolation * distancePenaltyFactor : 0;
[11311]96
97      double quality = score - penalty;
98
[12721]99      return new OrienteeringEvaluationResult {
[11311]100        Quality = new DoubleValue(quality),
[11327]101        Penalty = new DoubleValue(penalty),
102        Distance = new DoubleValue(distance)
[11187]103      };
[11186]104    }
105
106    public override IOperation InstrumentedApply() {
[11311]107      var evaluation = Apply(IntegerVectorParameter.ActualValue, ScoresParameter.ActualValue,
[11327]108        DistanceMatrixParameter.ActualValue, MaximumDistanceParameter.ActualValue.Value,
109        PointVisitingCostsParameter.ActualValue.Value, DistancePenaltyFactorParameter.ActualValue.Value);
[11186]110
[11187]111      QualityParameter.ActualValue = evaluation.Quality;
[11311]112      PenaltyParameter.ActualValue = evaluation.Penalty;
[11187]113
[11186]114      return base.InstrumentedApply();
115    }
[11327]116
[12721]117    public OrienteeringEvaluationResult Evaluate(IntegerVector solution, DoubleArray scores,
[11327]118      DistanceMatrix distances, double maximumDistance, double pointVisitingCosts) {
119      return Apply(solution, scores, distances, maximumDistance, pointVisitingCosts,
120        ((IValueParameter<DoubleValue>)DistancePenaltyFactorParameter).Value.Value);
121    }
[11186]122  }
123}
Note: See TracBrowser for help on using the repository browser.