#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Problems.VehicleRouting.Interfaces; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Core; using HeuristicLab.Parameters; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Problems.VehicleRouting.Variants; using HeuristicLab.Problems.VehicleRouting.Encodings; using HeuristicLab.Common; namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances { [Item("VRPEvaluator", "Represents a VRP evaluator.")] [StorableClass] public abstract class VRPEvaluator: VRPOperator, IVRPEvaluator { public ILookupParameter VRPToursParameter { get { return (ILookupParameter)Parameters["VRPTours"]; } } #region ISingleObjectiveEvaluator Members public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } #endregion public ILookupParameter DistanceParameter { get { return (ILookupParameter)Parameters["Distance"]; } } public ILookupParameter VehcilesUtilizedParameter { get { return (ILookupParameter)Parameters["VehiclesUtilized"]; } } public ILookupParameter PenaltyParameter { get { return (ILookupParameter)Parameters["Penalty"]; } } [StorableConstructor] protected VRPEvaluator(bool deserializing) : base(deserializing) { } public VRPEvaluator() { Parameters.Add(new LookupParameter("VRPTours", "The VRP tours which should be evaluated.")); Parameters.Add(new LookupParameter("Quality", "The evaluated quality of the VRP solution.")); Parameters.Add(new LookupParameter("Distance", "The distance.")); Parameters.Add(new LookupParameter("VehiclesUtilized", "The number of vehicles utilized.")); Parameters.Add(new LookupParameter("Penalty", "The applied penalty.")); } protected VRPEvaluator(VRPEvaluator original, Cloner cloner) : base(original, cloner) { } protected virtual VRPEvaluation CreateTourEvaluation() { return new VRPEvaluation(); } protected abstract void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour); protected virtual void InitResultParameters() { QualityParameter.ActualValue = new DoubleValue(0); VehcilesUtilizedParameter.ActualValue = new DoubleValue(0); DistanceParameter.ActualValue = new DoubleValue(0); PenaltyParameter.ActualValue = new DoubleValue(0); } protected virtual void SetResultParameters(VRPEvaluation tourEvaluation) { QualityParameter.ActualValue.Value = tourEvaluation.Quality; VehcilesUtilizedParameter.ActualValue.Value = tourEvaluation.VehicleUtilization; DistanceParameter.ActualValue.Value = tourEvaluation.Distance; PenaltyParameter.ActualValue.Value = tourEvaluation.Penalty; } private VRPEvaluation EvaluateTour(IVRPProblemInstance instance, Tour tour) { VRPEvaluation evaluation = CreateTourEvaluation(); EvaluateTour(evaluation, instance, tour); return evaluation; } #region IVRPEvaluator Members public bool Feasible(VRPEvaluation evaluation) { return evaluation.Penalty < double.Epsilon; } public VRPEvaluation Evaluate(IVRPProblemInstance instance, IVRPEncoding solution) { VRPEvaluation evaluation = CreateTourEvaluation(); foreach (Tour tour in solution.GetTours()) { EvaluateTour(evaluation, instance, tour); } return evaluation; } public VRPEvaluation Evaluate(IVRPProblemInstance instance, Tour tour) { return EvaluateTour(instance, tour); } public override IOperation Apply() { InitResultParameters(); VRPEvaluation evaluation = CreateTourEvaluation(); foreach (Tour tour in VRPToursParameter.ActualValue.GetTours()) { EvaluateTour(evaluation, ProblemInstance, tour); } SetResultParameters(evaluation); QualityParameter.ActualValue = new DoubleValue(evaluation.Quality); return base.Apply(); } #endregion } }