Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPEvaluator.cs @ 17609

Last change on this file since 17609 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.6 KB
RevLine 
[4363]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4363]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
[8053]22using HeuristicLab.Common;
[4363]23using HeuristicLab.Core;
[8053]24using HeuristicLab.Data;
[4363]25using HeuristicLab.Parameters;
[16565]26using HEAL.Attic;
[8053]27using HeuristicLab.Problems.VehicleRouting.Interfaces;
[4363]28using HeuristicLab.Problems.VehicleRouting.Variants;
29
30namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
31  [Item("CVRPEvaluator", "Represents a single depot CVRP evaluator.")]
[16565]32  [StorableType("7253D8EC-5F91-4E5F-99E0-45AF10AEA9AF")]
[8053]33  public class CVRPEvaluator : VRPEvaluator {
[4363]34    public ILookupParameter<DoubleValue> OverloadParameter {
35      get { return (ILookupParameter<DoubleValue>)Parameters["Overload"]; }
36    }
37
38    protected override VRPEvaluation CreateTourEvaluation() {
39      return new CVRPEvaluation();
40    }
41
[6838]42    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
[6883]43      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour))); ;
[6752]44      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
[7276]45      double originalQuality = eval.Quality;
[8053]46
[4376]47      IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance;
[4377]48      DoubleArray demand = instance.Demand;
[4363]49
50      double delivered = 0.0;
51      double overweight = 0.0;
[4377]52      double distance = 0.0;
[4363]53
[6752]54      double capacity = cvrpInstance.Capacity.Value;
55      for (int i = 0; i <= tour.Stops.Count; i++) {
56        int end = 0;
57        if (i < tour.Stops.Count)
58          end = tour.Stops[i];
59
60        delivered += demand[end];
61      }
62
63      double spareCapacity = capacity - delivered;
64
[4377]65      //simulate a tour, start and end at depot
66      for (int i = 0; i <= tour.Stops.Count; i++) {
67        int start = 0;
68        if (i > 0)
69          start = tour.Stops[i - 1];
70        int end = 0;
71        if (i < tour.Stops.Count)
72          end = tour.Stops[i];
73
74        //drive there
[6851]75        double currentDistace = instance.GetDistance(start, end, solution);
[4377]76        distance += currentDistace;
77
[6752]78        CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
79        tourInfo.AddStopInsertionInfo(stopInfo);
[4363]80      }
81
[4377]82      eval.Quality += instance.FleetUsageFactor.Value;
83      eval.Quality += instance.DistanceFactor.Value * distance;
84
[4378]85      eval.Distance += distance;
86      eval.VehicleUtilization += 1;
[4377]87
[4363]88      if (delivered > capacity) {
89        overweight = delivered - capacity;
90      }
91
[4378]92      (eval as CVRPEvaluation).Overload += overweight;
[4363]93      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
94      eval.Penalty += penalty;
95      eval.Quality += penalty;
[6752]96      tourInfo.Penalty = penalty;
[7276]97      tourInfo.Quality = eval.Quality - originalQuality;
[4363]98    }
99
[8053]100    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
[6752]101      out bool feasible) {
102      CVRPInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPInsertionInfo;
[8053]103
[6752]104      double costs = 0;
105      feasible = tourInsertionInfo.Penalty < double.Epsilon;
106
107      ICapacitatedProblemInstance cvrp = instance as ICapacitatedProblemInstance;
108      double overloadPenalty = cvrp.OverloadPenalty.Value;
109
[6851]110      double distance = instance.GetDistance(insertionInfo.Start, insertionInfo.End, solution);
[6752]111      double newDistance =
[6851]112        instance.GetDistance(insertionInfo.Start, customer, solution) +
113        instance.GetDistance(customer, insertionInfo.End, solution);
[6752]114      costs += instance.DistanceFactor.Value * (newDistance - distance);
115
116      double demand = instance.Demand[customer];
117      if (demand > insertionInfo.SpareCapacity) {
118        feasible = false;
119
[8053]120        if (insertionInfo.SpareCapacity >= 0)
[6752]121          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
122        else
123          costs += demand * overloadPenalty;
124      }
[8053]125
[6752]126      return costs;
127    }
128
[4363]129    protected override void InitResultParameters() {
130      base.InitResultParameters();
131
132      OverloadParameter.ActualValue = new DoubleValue(0);
133    }
134
[4520]135    protected override void SetResultParameters(VRPEvaluation tourEvaluation) {
136      base.SetResultParameters(tourEvaluation);
[4363]137
[4520]138      OverloadParameter.ActualValue.Value = (tourEvaluation as CVRPEvaluation).Overload;
[4363]139    }
[8053]140
[4363]141    [StorableConstructor]
[16565]142    protected CVRPEvaluator(StorableConstructorFlag _) : base(_) { }
[4363]143
144    public CVRPEvaluator() {
145      Parameters.Add(new LookupParameter<DoubleValue>("Overload", "The overload."));
146    }
[4752]147
148    public override IDeepCloneable Clone(Cloner cloner) {
149      return new CVRPEvaluator(this, cloner);
150    }
151
152    protected CVRPEvaluator(CVRPEvaluator original, Cloner cloner)
153      : base(original, cloner) {
154    }
[4363]155  }
156}
Note: See TracBrowser for help on using the repository browser.