Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MultiDepotVRPProblemInstance.cs @ 14676

Last change on this file since 14676 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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