Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MultiDepotVRPProblemInstance.cs @ 12440

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

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

File size: 6.1 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.Common;
35
36namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
37  [Item("MultiDepotVRPProblemInstance", "Represents a multi depot VRP instance.")]
38  [StorableClass]
39  public class MultiDepotVRPProblemInstance: VRPProblemInstance, IMultiDepotProblemInstance {
40    protected IValueParameter<IntValue> DepotsParameter {
41      get { return (IValueParameter<IntValue>)Parameters["Depots"]; }
42    }
43
44    protected IValueParameter<IntArray> VehicleDepotAssignmentParameter {
45      get { return (IValueParameter<IntArray>)Parameters["VehicleDepotAssignment"]; }
46    }
47
48    public IntValue Depots {
49      get {
50        return DepotsParameter.Value;
51      }
52    }
53
54    public IntArray VehicleDepotAssignment {
55      get {
56        return VehicleDepotAssignmentParameter.Value;
57      }
58      set {
59        VehicleDepotAssignmentParameter.Value = value;
60      }
61    }
62   
63    protected override IEnumerable<IOperator> GetOperators() {
64      return ApplicationManager.Manager.GetInstances<IMultiDepotOperator>().Cast<IOperator>();
65    }
66
67    protected override IEnumerable<IOperator> GetAnalyzers() {
68      return ApplicationManager.Manager.GetInstances<IMultiDepotOperator>()
69        .Where(o => o is IAnalyzer)
70        .Cast<IOperator>();
71    }
72
73    public override IntValue Cities {
74      get {
75        return new IntValue(Demand.Length);
76      }
77    }
78
79    protected override IVRPEvaluator Evaluator {
80      get {
81        return new MultiDepotVRPEvaluator();
82      }
83    }
84
85    protected override IVRPCreator Creator {
86      get {
87        return new HeuristicLab.Problems.VehicleRouting.Encodings.Alba.RandomCreator();
88      }
89    }
90
91    public override double GetDemand(int city) {
92      return base.GetDemand(city - 1);
93    }
94
95    public override double[] GetCoordinates(int city) {
96      double[] coordinates;
97     
98      if (city == 0) {
99        //calculate centroid
100        coordinates = new double[Coordinates.Columns];
101
102        for (int i = 0; i < Depots.Value; i++) {
103          for (int j = 0; j < coordinates.Length; j++) {
104            coordinates[j] += Coordinates[i, j];
105          }
106        }
107
108        for (int j = 0; j < coordinates.Length; j++) {
109          coordinates[j] /= (double)Depots.Value;
110        }
111      } else {
112        city += Depots.Value - 1;
113        coordinates = base.GetCoordinates(city);
114      }
115
116      return coordinates;
117    }
118
119    public int GetDepot(int customer, IVRPEncoding solution) {
120      int depot = -1;
121     
122      Tour tour =
123          solution.GetTours().FirstOrDefault(t => t.Stops.Contains(customer));
124
125      if (tour != null) {
126        int tourIndex = solution.GetTourIndex(tour);
127        int vehicle = solution.GetVehicleAssignment(tourIndex);
128        depot = VehicleDepotAssignment[vehicle];
129      }
130
131      return depot;
132    }
133
134    public override double GetDistance(int start, int end, IVRPEncoding solution) {
135      if (start == 0 && end == 0)
136        return 0;
137     
138      if (start == 0) {
139        start = GetDepot(end, solution);
140        end += Depots.Value - 1;
141      } else if (end == 0) {
142        end = GetDepot(start, solution);
143        start += Depots.Value - 1;
144      } else {
145        start += Depots.Value - 1;
146        end += Depots.Value - 1;
147      }
148     
149      return base.GetDistance(start, end, solution);
150    }
151
152    public override double GetInsertionDistance(int start, int customer, int end, IVRPEncoding solution,
153      out double startDistance, out double endDistance) {
154      if (start == 0) {
155        start = GetDepot(end, solution);
156        end += Depots.Value - 1;
157      } else if (end == 0) {
158        end = GetDepot(start, solution);
159        start += Depots.Value - 1;
160      } else {
161        start += Depots.Value - 1;
162        end += Depots.Value - 1;
163      }
164      customer += Depots.Value - 1;
165     
166      double distance = base.GetDistance(start, end, solution);
167
168      startDistance = base.GetDistance(start, customer, solution);
169      endDistance = base.GetDistance(customer, end, solution);
170
171      double newDistance = startDistance + endDistance;
172
173      return newDistance - distance;
174    }
175   
176    [StorableConstructor]
177    protected MultiDepotVRPProblemInstance(bool deserializing) : base(deserializing) { }
178
179    public MultiDepotVRPProblemInstance() {
180      Parameters.Add(new ValueParameter<IntValue>("Depots", "The number of depots", new IntValue(0)));
181      Parameters.Add(new ValueParameter<IntArray>("VehicleDepotAssignment", "The assignment of vehicles to depots", new IntArray()));
182    }
183
184    public override IDeepCloneable Clone(Cloner cloner) {
185      return new MultiDepotVRPProblemInstance(this, cloner);
186    }
187
188    protected MultiDepotVRPProblemInstance(MultiDepotVRPProblemInstance original, Cloner cloner)
189      : base(original, cloner) {
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.