Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPEvaluator.cs @ 17209

Last change on this file since 17209 was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
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  [StorableType("7253D8EC-5F91-4E5F-99E0-45AF10AEA9AF")]
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      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour))); ;
44      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
45      double originalQuality = eval.Quality;
46
47      IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance;
48      DoubleArray demand = instance.Demand;
49
50      double delivered = 0.0;
51      double overweight = 0.0;
52      double distance = 0.0;
53
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
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
75        double currentDistace = instance.GetDistance(start, end, solution);
76        distance += currentDistace;
77
78        CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
79        tourInfo.AddStopInsertionInfo(stopInfo);
80      }
81
82      eval.Quality += instance.FleetUsageFactor.Value;
83      eval.Quality += instance.DistanceFactor.Value * distance;
84
85      eval.Distance += distance;
86      eval.VehicleUtilization += 1;
87
88      if (delivered > capacity) {
89        overweight = delivered - capacity;
90      }
91
92      (eval as CVRPEvaluation).Overload += overweight;
93      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
94      eval.Penalty += penalty;
95      eval.Quality += penalty;
96      tourInfo.Penalty = penalty;
97      tourInfo.Quality = eval.Quality - originalQuality;
98    }
99
100    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
101      out bool feasible) {
102      CVRPInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPInsertionInfo;
103
104      double costs = 0;
105      feasible = tourInsertionInfo.Penalty < double.Epsilon;
106
107      ICapacitatedProblemInstance cvrp = instance as ICapacitatedProblemInstance;
108      double overloadPenalty = cvrp.OverloadPenalty.Value;
109
110      double distance = instance.GetDistance(insertionInfo.Start, insertionInfo.End, solution);
111      double newDistance =
112        instance.GetDistance(insertionInfo.Start, customer, solution) +
113        instance.GetDistance(customer, insertionInfo.End, solution);
114      costs += instance.DistanceFactor.Value * (newDistance - distance);
115
116      double demand = instance.Demand[customer];
117      if (demand > insertionInfo.SpareCapacity) {
118        feasible = false;
119
120        if (insertionInfo.SpareCapacity >= 0)
121          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
122        else
123          costs += demand * overloadPenalty;
124      }
125
126      return costs;
127    }
128
129    protected override void InitResultParameters() {
130      base.InitResultParameters();
131
132      OverloadParameter.ActualValue = new DoubleValue(0);
133    }
134
135    protected override void SetResultParameters(VRPEvaluation tourEvaluation) {
136      base.SetResultParameters(tourEvaluation);
137
138      OverloadParameter.ActualValue.Value = (tourEvaluation as CVRPEvaluation).Overload;
139    }
140
141    [StorableConstructor]
142    protected CVRPEvaluator(StorableConstructorFlag _) : base(_) { }
143
144    public CVRPEvaluator() {
145      Parameters.Add(new LookupParameter<DoubleValue>("Overload", "The overload."));
146    }
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    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.