#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; namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances { [Item("CVRPTWEvaluator", "Represents a single depot CVRPTW evaluator.")] [StorableClass] public class CVRPTWEvaluator: CVRPEvaluator { public ILookupParameter TardinessParameter { get { return (ILookupParameter)Parameters["Tardiness"]; } } public ILookupParameter TravelTimeParameter { get { return (ILookupParameter)Parameters["TravelTime"]; } } protected override VRPEvaluation CreateTourEvaluation() { return new CVRPTWEvaluation(); } protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour) { base.EvaluateTour(eval, instance, tour); ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance; double time = 0.0; double waitingTime = 0.0; double serviceTime = 0.0; double tardiness = 0.0; //simulate a tour, start and end at depot for (int i = 0; i <= tour.Stops.Count; i++) { int start = 0; if (i > 0) start = tour.Stops[i - 1]; int end = 0; if (i < tour.Stops.Count) end = tour.Stops[i]; //drive there double currentDistace = vrptw.GetDistance(start, end); time += currentDistace; //check if it was serviced on time if (time > vrptw.DueTime[end]) tardiness += time - vrptw.DueTime[end]; //wait double currentWaitingTime = 0.0; if (time < vrptw.ReadyTime[end]) currentWaitingTime = vrptw.ReadyTime[end] - time; waitingTime += currentWaitingTime; time += currentWaitingTime; //service double currentServiceTime = vrptw.ServiceTime[end]; serviceTime += currentServiceTime; time += currentServiceTime; } (eval as CVRPTWEvaluation).Tardiness = tardiness; (eval as CVRPTWEvaluation).TravelTime = time; double penalty = tardiness * vrptw.TardinessPenalty.Value; eval.Penalty += penalty; eval.Quality += penalty; eval.Quality += time * vrptw.TimeFactor.Value; } protected override void InitResultParameters() { base.InitResultParameters(); TardinessParameter.ActualValue = new DoubleValue(0); TravelTimeParameter.ActualValue = new DoubleValue(0); } protected override void UpdateResultParameters(VRPEvaluation tourEvaluation) { base.UpdateResultParameters(tourEvaluation); TardinessParameter.ActualValue.Value += (tourEvaluation as CVRPTWEvaluation).Tardiness; TravelTimeParameter.ActualValue.Value += (tourEvaluation as CVRPTWEvaluation).TravelTime; } [StorableConstructor] protected CVRPTWEvaluator(bool deserializing) : base(deserializing) { } public CVRPTWEvaluator() { Parameters.Add(new LookupParameter("Tardiness", "The tardiness.")); Parameters.Add(new LookupParameter("TravelTime", "The travel time.")); } } }