#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HEAL.Attic; namespace HeuristicLab.Problems.Orienteering { [Item("OrienteeringEvaluator", "Operator to evaluate a solution to the orienteering problem.")] [StorableType("ACAC24FD-9FBC-4722-BFB0-21BFEF02C2D1")] public class OrienteeringEvaluator : InstrumentedOperator, IOrienteeringEvaluator { #region ParameterProperties public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } public ILookupParameter PenaltyParameter { get { return (ILookupParameter)Parameters["Penalty"]; } } public ILookupParameter DistancePenaltyFactorParameter { get { return (ILookupParameter)Parameters["DistancePenaltyFactor"]; } } public ILookupParameter IntegerVectorParameter { get { return (ILookupParameter)Parameters["IntegerVector"]; } } public ILookupParameter ScoresParameter { get { return (ILookupParameter)Parameters["Scores"]; } } public ILookupParameter DistanceMatrixParameter { get { return (ILookupParameter)Parameters["DistanceMatrix"]; } } public ILookupParameter MaximumDistanceParameter { get { return (ILookupParameter)Parameters["MaximumDistance"]; } } public ILookupParameter PointVisitingCostsParameter { get { return (ILookupParameter)Parameters["PointVisitingCosts"]; } } #endregion [StorableConstructor] protected OrienteeringEvaluator(StorableConstructorFlag _) : base(_) { } protected OrienteeringEvaluator(OrienteeringEvaluator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new OrienteeringEvaluator(this, cloner); } public OrienteeringEvaluator() : base() { Parameters.Add(new LookupParameter("Quality", "The evaluated quality of the Orienteering solution.")); Parameters.Add(new LookupParameter("Penalty", "The applied penalty")); Parameters.Add(new ValueLookupParameter("DistancePenaltyFactor", "The penalty applied per distance violation.", new DoubleValue(0))); Parameters.Add(new LookupParameter("IntegerVector", "The Orienteering Solution given in path representation.")); Parameters.Add(new LookupParameter("Scores", "The scores of the points.")); Parameters.Add(new LookupParameter("DistanceMatrix", "The matrix which contains the distances between the points.")); Parameters.Add(new LookupParameter("MaximumDistance", "The maximum distance constraint for a Orienteering solution.")); Parameters.Add(new LookupParameter("PointVisitingCosts", "The costs for visiting a point.")); } public static OrienteeringEvaluationResult Apply(IntegerVector solution, DoubleArray scores, DistanceMatrix distances, double maximumDistance, double pointVisitingCosts, double distancePenaltyFactor) { double score = solution.Sum(t => scores[t]); double distance = distances.CalculateTourLength(solution.ToList(), pointVisitingCosts); double distanceViolation = distance - maximumDistance; double penalty = 0.0; penalty += distanceViolation > 0 ? distanceViolation * distancePenaltyFactor : 0; double quality = score - penalty; return new OrienteeringEvaluationResult { Quality = new DoubleValue(quality), Penalty = new DoubleValue(penalty), Distance = new DoubleValue(distance) }; } public override IOperation InstrumentedApply() { var evaluation = Apply(IntegerVectorParameter.ActualValue, ScoresParameter.ActualValue, DistanceMatrixParameter.ActualValue, MaximumDistanceParameter.ActualValue.Value, PointVisitingCostsParameter.ActualValue.Value, DistancePenaltyFactorParameter.ActualValue.Value); QualityParameter.ActualValue = evaluation.Quality; PenaltyParameter.ActualValue = evaluation.Penalty; return base.InstrumentedApply(); } public OrienteeringEvaluationResult Evaluate(IntegerVector solution, DoubleArray scores, DistanceMatrix distances, double maximumDistance, double pointVisitingCosts) { return Apply(solution, scores, distances, maximumDistance, pointVisitingCosts, ((IValueParameter)DistancePenaltyFactorParameter).Value.Value); } } }