Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.VehicleRouting/3.3/VRPOperator.cs @ 4691

Last change on this file since 4691 was 4690, checked in by gkronber, 14 years ago

Refactored cloning in HeuristicLab.Problems.VehicleRouting. #922

File size: 5.9 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 HeuristicLab.Core;
23using HeuristicLab.Operators;
24using HeuristicLab.Parameters;
25using HeuristicLab.Data;
26using HeuristicLab.Problems.VehicleRouting.Encodings;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using System;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.Problems.VehicleRouting {
32  [Item("VRPOperator", "A VRP operator.")]
33  [StorableClass]
34  public abstract class VRPOperator : SingleSuccessorOperator, IVRPOperator {
35    public int Cities {
36      get { return CoordinatesParameter.ActualValue.Rows - 1; }
37    }
38    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
39      get {
40        if (Parameters.ContainsKey("Coordinates"))
41          return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"];
42        else
43          return null;
44      }
45    }
46    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
47      get {
48        if (Parameters.ContainsKey("DistanceMatrix"))
49          return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"];
50        else
51          return null;
52      }
53    }
54    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
55      get {
56        if (Parameters.ContainsKey("UseDistanceMatrix"))
57          return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"];
58        else
59          return null;
60      }
61    }
62    public ILookupParameter<IntValue> VehiclesParameter {
63      get {
64        if (Parameters.ContainsKey("Vehicles"))
65          return (ILookupParameter<IntValue>)Parameters["Vehicles"];
66        else
67          return null;
68      }
69    }
70    public ILookupParameter<DoubleValue> CapacityParameter {
71      get {
72        if (Parameters.ContainsKey("Capacity"))
73          return (ILookupParameter<DoubleValue>)Parameters["Capacity"];
74        else
75          return null;
76      }
77    }
78    public ILookupParameter<DoubleArray> DemandParameter {
79      get {
80        if (Parameters.ContainsKey("Demand"))
81          return (ILookupParameter<DoubleArray>)Parameters["Demand"];
82        else
83          return null;
84      }
85    }
86    public ILookupParameter<DoubleArray> ReadyTimeParameter {
87      get {
88        if (Parameters.ContainsKey("ReadyTime"))
89          return (ILookupParameter<DoubleArray>)Parameters["ReadyTime"];
90        else
91          return null;
92      }
93    }
94    public ILookupParameter<DoubleArray> DueTimeParameter {
95      get {
96        if (Parameters.ContainsKey("DueTime"))
97          return (ILookupParameter<DoubleArray>)Parameters["DueTime"];
98        else
99          return null;
100      }
101    }
102    public ILookupParameter<DoubleArray> ServiceTimeParameter {
103      get {
104        if (Parameters.ContainsKey("ServiceTime"))
105          return (ILookupParameter<DoubleArray>)Parameters["ServiceTime"];
106        else
107          return null;
108      }
109    }
110
111    [StorableConstructor]
112    protected VRPOperator(bool deserializing) : base(deserializing) { }
113    protected VRPOperator(VRPOperator original, Cloner cloner) : base(original, cloner) { }
114    public VRPOperator()
115      : base() {
116      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The coordinates of the cities."));
117      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
118      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
119      Parameters.Add(new LookupParameter<IntValue>("Vehicles", "The number of vehicles."));
120      Parameters.Add(new LookupParameter<DoubleValue>("Capacity", "The capacity of each vehicle."));
121      Parameters.Add(new LookupParameter<DoubleArray>("Demand", "The demand of each customer."));
122      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
123      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
124      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
125    }
126
127    protected bool Feasible(Tour tour) {
128      return tour.Feasible(
129                  DueTimeParameter.ActualValue,
130                  ServiceTimeParameter.ActualValue,
131                  ReadyTimeParameter.ActualValue,
132                  DemandParameter.ActualValue,
133                  CapacityParameter.ActualValue,
134                  CoordinatesParameter.ActualValue,
135                  DistanceMatrixParameter,
136                  UseDistanceMatrixParameter.ActualValue);
137    }
138
139    protected bool Feasible(IVRPEncoding solution) {
140      bool feasible = true;
141
142      foreach (Tour tour in solution.GetTours(DistanceMatrixParameter)) {
143        if (!Feasible(tour)) {
144          feasible = false;
145          break;
146        }
147      }
148
149      return feasible;
150    }
151
152    protected double GetLength(Tour tour) {
153      return tour.GetLength(
154                CoordinatesParameter.ActualValue,
155                DistanceMatrixParameter,
156                UseDistanceMatrixParameter.ActualValue);
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.