Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPEvaluator.cs @ 14677

Last change on this file since 14677 was 14677, checked in by abeham, 7 years ago

#2205: added results tab to solution view to analyze vrp solution in more detail

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