Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MDCVRP/MDCVRPEvaluator.cs @ 6854

Last change on this file since 6854 was 6854, checked in by svonolfe, 13 years ago

Added support for the multi depot CVRP with time windows (#1177)

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Problems.VehicleRouting.Interfaces;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Core;
29using HeuristicLab.Parameters;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34using HeuristicLab.Problems.VehicleRouting.Encodings;
35using HeuristicLab.Common;
36
37namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
38  [Item("MDCVRPEvaluator", "Represents a multi depot CVRP evaluator.")]
39  [StorableClass]
40  public class MDCVRPEvaluator: VRPEvaluator {
41    public ILookupParameter<DoubleValue> OverloadParameter {
42      get { return (ILookupParameter<DoubleValue>)Parameters["Overload"]; }
43    }
44
45    protected override VRPEvaluation CreateTourEvaluation() {
46      return new CVRPEvaluation();
47    }
48
49    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
50      TourInsertionInfo tourInfo = new TourInsertionInfo();
51      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
52
53      IHeterogenousCapacitatedProblemInstance cvrpInstance = instance as IHeterogenousCapacitatedProblemInstance;
54
55      double delivered = 0.0;
56      double overweight = 0.0;
57      double distance = 0.0;
58
59      int tourIndex = solution.GetTourIndex(tour);
60      int vehicle = solution.GetVehicleAssignment(tourIndex);
61
62      double capacity = cvrpInstance.Capacity[vehicle];
63      for (int i = 0; i < tour.Stops.Count; i++) {
64        delivered += instance.GetDemand(tour.Stops[i]);
65      }
66
67      double spareCapacity = capacity - delivered;
68
69      //simulate a tour, start and end at depot
70      for (int i = 0; i <= tour.Stops.Count; i++) {
71        int start = 0;
72        if (i > 0)
73          start = tour.Stops[i - 1];
74        int end = 0;
75        if (i < tour.Stops.Count)
76          end = tour.Stops[i];
77
78        //drive there
79        double currentDistace = instance.GetDistance(start, end, solution);
80        distance += currentDistace;
81
82        CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
83        tourInfo.AddStopInsertionInfo(stopInfo);
84      }
85
86      eval.Quality += instance.FleetUsageFactor.Value;
87      eval.Quality += instance.DistanceFactor.Value * distance;
88
89      eval.Distance += distance;
90      eval.VehicleUtilization += 1;
91
92      if (delivered > capacity) {
93        overweight = delivered - capacity;
94      }
95
96      (eval as CVRPEvaluation).Overload += overweight;
97      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
98      eval.Penalty += penalty;
99      eval.Quality += penalty;
100      tourInfo.Penalty = penalty;
101    }
102
103    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
104      out bool feasible) {
105      CVRPInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPInsertionInfo;
106     
107      double costs = 0;
108      feasible = tourInsertionInfo.Penalty < double.Epsilon;
109
110      IHeterogenousCapacitatedProblemInstance cvrp = instance as IHeterogenousCapacitatedProblemInstance;
111      double overloadPenalty = cvrp.OverloadPenalty.Value;
112
113      double startDistance, endDistance;
114      costs += instance.GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance);
115
116      double demand = instance.GetDemand(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 MDCVRPEvaluator(bool deserializing) : base(deserializing) { }
143
144    public MDCVRPEvaluator() {
145      Parameters.Add(new LookupParameter<DoubleValue>("Overload", "The overload."));
146    }
147
148    public override IDeepCloneable Clone(Cloner cloner) {
149      return new MDCVRPEvaluator(this, cloner);
150    }
151
152    protected MDCVRPEvaluator(MDCVRPEvaluator original, Cloner cloner)
153      : base(original, cloner) {
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.