Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MDCVRPProblemInstance.cs @ 17954

Last change on this file since 17954 was 17954, checked in by abeham, 3 years ago

#2521: updated samples (except for GPR - unit test needs to be refactored first)

  • Fixed a few bugs in VRP
  • Fixed the GAGroupingProblemSampleTest
File size: 7.9 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Problems.VehicleRouting.Interfaces;
31
32namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
33  [Item("MDCVRPProblemInstance", "Represents a multi depot CVRP instance.")]
34  [StorableType("19F824AB-6C66-4FB6-88D5-3C163BF789E6")]
35  public class MDCVRPProblemInstance : MultiDepotVRPProblemInstance, IHeterogenousCapacitatedProblemInstance {
36    protected IValueParameter<DoubleArray> CapacityParameter {
37      get { return (IValueParameter<DoubleArray>)Parameters["Capacity"]; }
38    }
39    protected IValueParameter<DoubleValue> OverloadPenaltyParameter {
40      get { return (IValueParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
41    }
42
43    public DoubleArray Capacity {
44      get { return CapacityParameter.Value; }
45      set { CapacityParameter.Value = value; }
46    }
47
48    protected IValueParameter<DoubleValue> CurrentOverloadPenaltyParameter {
49      get { return (IValueParameter<DoubleValue>)Parameters["CurrentOverloadPenalty"]; }
50    }
51
52    public DoubleValue OverloadPenalty {
53      get {
54        DoubleValue currentOverloadPenalty = CurrentOverloadPenaltyParameter.Value;
55        if (currentOverloadPenalty != null)
56          return currentOverloadPenalty;
57        else
58          return OverloadPenaltyParameter.Value;
59      }
60    }
61    DoubleValue ICapacitatedProblemInstance.CurrentOverloadPenalty {
62      get { return CurrentOverloadPenaltyParameter.Value; }
63      set { CurrentOverloadPenaltyParameter.Value = value; }
64    }
65
66    public override IEnumerable<IOperator> FilterOperators(IEnumerable<IOperator> operators) {
67      return base.FilterOperators(operators)
68        .Where(x => !(x is INotCapacitatedOperator))
69        .Union(operators.Where(x => x is IHeterogenousCapacitatedOperator
70                                 || x is ICapacitatedOperator && !(x is IHomogenousCapacitatedOperator)));
71    }
72
73    protected override VRPEvaluation CreateTourEvaluation() {
74      return new CVRPEvaluation();
75    }
76
77    protected override void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution) {
78      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
79      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
80      double originalQuality = eval.Quality;
81
82      double delivered = 0.0;
83      double overweight = 0.0;
84      double distance = 0.0;
85
86      int tourIndex = solution.GetTourIndex(tour);
87      int vehicle = solution.GetVehicleAssignment(tourIndex);
88
89      double capacity = Capacity[vehicle];
90      for (int i = 0; i < tour.Stops.Count; i++) {
91        delivered += GetDemand(tour.Stops[i]);
92      }
93
94      double spareCapacity = capacity - delivered;
95
96      //simulate a tour, start and end at depot
97      for (int i = 0; i <= tour.Stops.Count; i++) {
98        int start = 0;
99        if (i > 0)
100          start = tour.Stops[i - 1];
101        int end = 0;
102        if (i < tour.Stops.Count)
103          end = tour.Stops[i];
104
105        //drive there
106        double currentDistace = GetDistance(start, end, solution);
107        distance += currentDistace;
108
109        CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
110        tourInfo.AddStopInsertionInfo(stopInfo);
111      }
112
113      eval.Quality += FleetUsageFactor.Value;
114      eval.Quality += DistanceFactor.Value * distance;
115
116      eval.Distance += distance;
117      eval.VehicleUtilization += 1;
118
119      if (delivered > capacity) {
120        overweight = delivered - capacity;
121      }
122
123      (eval as CVRPEvaluation).Overload += overweight;
124      double penalty = overweight * OverloadPenalty.Value;
125      eval.Penalty += penalty;
126      eval.Quality += penalty;
127      tourInfo.Penalty = penalty;
128      tourInfo.Quality = eval.Quality - originalQuality;
129    }
130
131    protected override double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
132      out bool feasible) {
133      CVRPInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPInsertionInfo;
134
135      double costs = 0;
136      feasible = tourInsertionInfo.Penalty < double.Epsilon;
137
138      double overloadPenalty = OverloadPenalty.Value;
139
140      double startDistance, endDistance;
141      costs += GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance);
142
143      double demand = GetDemand(customer);
144      if (demand > insertionInfo.SpareCapacity) {
145        feasible = false;
146
147        if (insertionInfo.SpareCapacity >= 0)
148          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
149        else
150          costs += demand * overloadPenalty;
151      }
152
153      return costs;
154    }
155
156    [StorableConstructor]
157    protected MDCVRPProblemInstance(StorableConstructorFlag _) : base(_) { }
158
159    public MDCVRPProblemInstance() {
160      Parameters.Add(new ValueParameter<DoubleArray>("Capacity", "The capacity of each vehicle.", new DoubleArray()));
161      Parameters.Add(new ValueParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(100)));
162      Parameters.Add(new OptionalValueParameter<DoubleValue>("CurrentOverloadPenalty", "The current overload penalty considered in the evaluation.") { Hidden = true });
163
164      AttachEventHandlers();
165    }
166
167    public override IDeepCloneable Clone(Cloner cloner) {
168      return new MDCVRPProblemInstance(this, cloner);
169    }
170
171    protected MDCVRPProblemInstance(MDCVRPProblemInstance original, Cloner cloner)
172      : base(original, cloner) {
173      AttachEventHandlers();
174    }
175
176    [StorableHook(HookType.AfterDeserialization)]
177    private void AfterDeserialization() {
178      AttachEventHandlers();
179    }
180
181    private void AttachEventHandlers() {
182      CapacityParameter.ValueChanged += CapacityParameter_ValueChanged;
183      Capacity.Reset += Capacity_Changed;
184      Capacity.ItemChanged += Capacity_Changed;
185      OverloadPenaltyParameter.ValueChanged += OverloadPenaltyParameter_ValueChanged;
186      OverloadPenalty.ValueChanged += OverloadPenalty_ValueChanged;
187    }
188
189    public override void InitializeState() {
190      base.InitializeState();
191
192      CurrentOverloadPenaltyParameter.Value = null;
193    }
194
195    #region Event handlers
196    void CapacityParameter_ValueChanged(object sender, EventArgs e) {
197      Capacity.Reset += Capacity_Changed;
198      Capacity.ItemChanged += Capacity_Changed;
199      EvalBestKnownSolution();
200    }
201    private void Capacity_Changed(object sender, EventArgs e) {
202      EvalBestKnownSolution();
203    }
204    void OverloadPenaltyParameter_ValueChanged(object sender, EventArgs e) {
205      OverloadPenalty.ValueChanged += OverloadPenalty_ValueChanged;
206      EvalBestKnownSolution();
207    }
208    void OverloadPenalty_ValueChanged(object sender, EventArgs e) {
209      EvalBestKnownSolution();
210    }
211    #endregion
212  }
213}
Note: See TracBrowser for help on using the repository browser.