Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MultiDepotVRPProblemInstance.cs @ 17717

Last change on this file since 17717 was 17717, checked in by abeham, 4 years ago

#2521: working on VRP (refactoring all the capabilities, features, and operator discovery)

File size: 8.4 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("MultiDepotVRPProblemInstance", "Represents a multi depot VRP instance.")]
34  [StorableType("20788CA4-8AA0-4A57-8118-0D7C3FEF1AA4")]
35  public class MultiDepotVRPProblemInstance : VRPProblemInstance, IMultiDepotProblemInstance {
36    protected IValueParameter<IntValue> DepotsParameter {
37      get { return (IValueParameter<IntValue>)Parameters["Depots"]; }
38    }
39
40    protected IValueParameter<IntArray> VehicleDepotAssignmentParameter {
41      get { return (IValueParameter<IntArray>)Parameters["VehicleDepotAssignment"]; }
42    }
43
44    public IntValue Depots {
45      get {
46        return DepotsParameter.Value;
47      }
48    }
49
50    public IntArray VehicleDepotAssignment {
51      get {
52        return VehicleDepotAssignmentParameter.Value;
53      }
54      set {
55        VehicleDepotAssignmentParameter.Value = value;
56      }
57    }
58
59    public override IEnumerable<IOperator> FilterOperators(IEnumerable<IOperator> operators) {
60      return base.FilterOperators(operators)
61        .Where(x => !(x is INotMultiDepotOperator))
62        .Union(operators.Where(x => x is IMultiDepotOperator));
63    }
64
65    public override IntValue Cities => new IntValue(Demand.Length);
66
67
68    public override double GetDemand(int city) {
69      return base.GetDemand(city - 1);
70    }
71
72    public override double[] GetCoordinates(int city) {
73      double[] coordinates;
74
75      if (city == 0) {
76        //calculate centroid
77        coordinates = new double[Coordinates.Columns];
78
79        for (int i = 0; i < Depots.Value; i++) {
80          for (int j = 0; j < coordinates.Length; j++) {
81            coordinates[j] += Coordinates[i, j];
82          }
83        }
84
85        for (int j = 0; j < coordinates.Length; j++) {
86          coordinates[j] /= (double)Depots.Value;
87        }
88      } else {
89        city += Depots.Value - 1;
90        coordinates = base.GetCoordinates(city);
91      }
92
93      return coordinates;
94    }
95
96    public int GetDepot(int customer, IVRPEncodedSolution solution) {
97      int depot = -1;
98
99      Tour tour =
100          solution.GetTours().FirstOrDefault(t => t.Stops.Contains(customer));
101
102      if (tour != null) {
103        int tourIndex = solution.GetTourIndex(tour);
104        int vehicle = solution.GetVehicleAssignment(tourIndex);
105        depot = VehicleDepotAssignment[vehicle];
106      }
107
108      return depot;
109    }
110
111    public override double GetDistance(int start, int end, IVRPEncodedSolution solution) {
112      if (start == 0 && end == 0)
113        return 0;
114
115      if (start == 0) {
116        start = GetDepot(end, solution);
117        end += Depots.Value - 1;
118      } else if (end == 0) {
119        end = GetDepot(start, solution);
120        start += Depots.Value - 1;
121      } else {
122        start += Depots.Value - 1;
123        end += Depots.Value - 1;
124      }
125
126      return base.GetDistance(start, end, solution);
127    }
128
129    public override double GetInsertionDistance(int start, int customer, int end, IVRPEncodedSolution solution,
130      out double startDistance, out double endDistance) {
131      if (start == 0) {
132        start = GetDepot(end, solution);
133        end += Depots.Value - 1;
134      } else if (end == 0) {
135        end = GetDepot(start, solution);
136        start += Depots.Value - 1;
137      } else {
138        start += Depots.Value - 1;
139        end += Depots.Value - 1;
140      }
141      customer += Depots.Value - 1;
142
143      double distance = base.GetDistance(start, end, solution);
144
145      startDistance = base.GetDistance(start, customer, solution);
146      endDistance = base.GetDistance(customer, end, solution);
147
148      double newDistance = startDistance + endDistance;
149
150      return newDistance - distance;
151    }
152    protected override void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution) {
153      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
154      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
155
156      double distance = 0.0;
157      double quality = 0.0;
158
159      //simulate a tour, start and end at depot
160      for (int i = 0; i <= tour.Stops.Count; i++) {
161        int start = 0;
162        if (i > 0)
163          start = tour.Stops[i - 1];
164        int end = 0;
165        if (i < tour.Stops.Count)
166          end = tour.Stops[i];
167
168        //drive there
169        double currentDistace = GetDistance(start, end, solution);
170        distance += currentDistace;
171
172        StopInsertionInfo stopInfo = new StopInsertionInfo(start, end);
173        tourInfo.AddStopInsertionInfo(stopInfo);
174      }
175
176      //Fleet usage
177      quality += FleetUsageFactor.Value;
178      //Distance
179      quality += DistanceFactor.Value * distance;
180
181      eval.Distance += distance;
182      eval.VehicleUtilization += 1;
183
184      eval.Quality += quality;
185      tourInfo.Quality = quality;
186    }
187
188    protected override double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
189      out bool feasible) {
190      StopInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index);
191
192      double costs = 0;
193      feasible = true;
194      double startDistance, endDistance;
195
196      costs += GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance);
197
198      return costs;
199    }
200
201    [StorableConstructor]
202    protected MultiDepotVRPProblemInstance(StorableConstructorFlag _) : base(_) { }
203    protected MultiDepotVRPProblemInstance(MultiDepotVRPProblemInstance original, Cloner cloner)
204      : base(original, cloner) {
205      AttachEventHandlers();
206    }
207    public MultiDepotVRPProblemInstance() {
208      Parameters.Add(new ValueParameter<IntValue>("Depots", "The number of depots", new IntValue(0)));
209      Parameters.Add(new ValueParameter<IntArray>("VehicleDepotAssignment", "The assignment of vehicles to depots", new IntArray()));
210      AttachEventHandlers();
211    }
212
213    public override IDeepCloneable Clone(Cloner cloner) {
214      return new MultiDepotVRPProblemInstance(this, cloner);
215    }
216
217    [StorableHook(HookType.AfterDeserialization)]
218    private void AfterDeserialization() {
219      AttachEventHandlers();
220    }
221
222    private void AttachEventHandlers() {
223      DepotsParameter.ValueChanged += DepotsParameter_ValueChanged;
224      Depots.ValueChanged += Depots_ValueChanged;
225      VehicleDepotAssignmentParameter.ValueChanged += VehicleDepotAssignmentParameter_ValueChanged;
226      VehicleDepotAssignment.Reset += VehicleDepotAssignment_Changed;
227      VehicleDepotAssignment.ItemChanged += VehicleDepotAssignment_Changed;
228    }
229
230    #region Event handlers
231    private void DepotsParameter_ValueChanged(object sender, EventArgs e) {
232      Depots.ValueChanged += Depots_ValueChanged;
233      EvalBestKnownSolution();
234    }
235    private void Depots_ValueChanged(object sender, EventArgs e) {
236      EvalBestKnownSolution();
237    }
238    private void VehicleDepotAssignmentParameter_ValueChanged(object sender, EventArgs e) {
239      VehicleDepotAssignment.Reset += VehicleDepotAssignment_Changed;
240      VehicleDepotAssignment.ItemChanged += VehicleDepotAssignment_Changed;
241      EvalBestKnownSolution();
242    }
243    private void VehicleDepotAssignment_Changed(object sender, EventArgs e) {
244      EvalBestKnownSolution();
245    }
246    #endregion
247  }
248}
Note: See TracBrowser for help on using the repository browser.