- Timestamp:
- 06/20/11 13:36:49 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/PushForwardInsertionCreator.cs
r5445 r6449 91 91 } 92 92 93 private double CalculateDistance(int start, int end ) {93 private double CalculateDistance(int start, int end, DoubleMatrix coordinates) { 94 94 double distance = 0.0; 95 DoubleMatrix coordinates = CoordinatesParameter.ActualValue;96 95 97 96 distance = … … 103 102 } 104 103 105 private DoubleMatrix CreateDistanceMatrix() { 106 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 104 private DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) { 107 105 DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows); 108 106 109 107 for (int i = 0; i < distanceMatrix.Rows; i++) { 110 108 for (int j = i; j < distanceMatrix.Columns; j++) { 111 double distance = CalculateDistance(i, j );109 double distance = CalculateDistance(i, j, coordinates); 112 110 113 111 distanceMatrix[i, j] = distance; … … 119 117 } 120 118 121 private double Distance(int start, int end ) {119 private double Distance(int start, int end, DoubleMatrix coordinates, bool useDistanceMatrix) { 122 120 double distance = 0.0; 123 121 124 if (UseDistanceMatrixParameter.ActualValue.Value) { 125 if (DistanceMatrixParameter.ActualValue == null) { 126 DistanceMatrixParameter.ActualValue = CreateDistanceMatrix(); 127 } 128 129 distance = DistanceMatrixParameter.ActualValue[start, end]; 130 } else { 131 distance = CalculateDistance(start, end); 122 if (useDistanceMatrix) { 123 distance = coordinates[start, end]; 124 } else { 125 distance = CalculateDistance(start, end, coordinates); 132 126 } 133 127 … … 135 129 } 136 130 137 private double TravelDistance(List<int> route, int begin ) {131 private double TravelDistance(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix) { 138 132 double distance = 0; 139 133 for (int i = begin; i < route.Count - 1 && (i == begin || route[i] != 0); i++) { 140 distance += Distance(route[i], route[i + 1] );134 distance += Distance(route[i], route[i + 1], coordinates, useDistanceMatrix); 141 135 } 142 136 return distance; 143 137 } 144 138 145 private bool SubrouteConstraintsOK(List<int> route, int begin) { 139 private bool SubrouteConstraintsOK(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix, 140 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity) { 146 141 double t = 0.0, o = 0.0; 147 142 for (int i = begin + 1; i < route.Count; i++) { 148 t += Distance(route[i - 1], route[i] );143 t += Distance(route[i - 1], route[i], coordinates, useDistanceMatrix); 149 144 if (route[i] == 0) return (t < DueTimeParameter.ActualValue[0]); // violation on capacity constraint is handled below 150 145 else { 151 if (t > DueTimeParameter.ActualValue[route[i]]) return false;152 t = Math.Max( ReadyTimeParameter.ActualValue[route[i]], t);153 t += ServiceTimeParameter.ActualValue[route[i]];154 o += DemandParameter.ActualValue[route[i]];155 if (o > CapacityParameter.ActualValue.Value) return false; // premature exit on capacity constraint violation146 if (t > dueTime[route[i]]) return false; 147 t = Math.Max(readyTime[route[i]], t); 148 t += serviceTime[route[i]]; 149 o += demand[route[i]]; 150 if (o > capacity.Value) return false; // premature exit on capacity constraint violation 156 151 } 157 152 } … … 159 154 } 160 155 161 private bool SubrouteTardinessOK(List<int> route, int begin) { 156 private bool SubrouteTardinessOK(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix, 157 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime) { 162 158 double t = 0.0; 163 159 for (int i = begin + 1; i < route.Count; i++) { 164 t += Distance(route[i - 1], route[i] );160 t += Distance(route[i - 1], route[i], coordinates, useDistanceMatrix); 165 161 if (route[i] == 0) { 166 if (t < DueTimeParameter.ActualValue[0]) return true;162 if (t < dueTime[0]) return true; 167 163 else return false; 168 164 } else { 169 if (t > DueTimeParameter.ActualValue[route[i]]) return false;170 t = Math.Max( ReadyTimeParameter.ActualValue[route[i]], t);171 t += ServiceTimeParameter.ActualValue[route[i]];165 if (t > dueTime[route[i]]) return false; 166 t = Math.Max(readyTime[route[i]], t); 167 t += serviceTime[route[i]]; 172 168 } 173 169 } … … 175 171 } 176 172 177 private bool SubrouteLoadOK(List<int> route, int begin ) {173 private bool SubrouteLoadOK(List<int> route, int begin, DoubleValue capacity, DoubleArray demand) { 178 174 double o = 0.0; 179 175 for (int i = begin + 1; i < route.Count; i++) { 180 if (route[i] == 0) return (o < CapacityParameter.ActualValue.Value);176 if (route[i] == 0) return (o < capacity.Value); 181 177 else { 182 o += DemandParameter.ActualValue[route[i]];183 } 184 } 185 return (o < CapacityParameter.ActualValue.Value);178 o += demand[route[i]]; 179 } 180 } 181 return (o < capacity.Value); 186 182 } 187 183 188 184 protected override List<int> CreateSolution() { 189 double alpha, beta, gamma;190 alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue);191 beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue);192 gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue);185 //double alpha, beta, gamma; 186 double alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue); 187 double beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue); 188 double gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue); 193 189 194 190 double x0 = CoordinatesParameter.ActualValue[0, 0]; … … 203 199 int indexOfCustomer = -1; 204 200 201 int vehicles = VehiclesParameter.ActualValue.Value; 202 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 203 DoubleArray dueTime = DueTimeParameter.ActualValue; 204 DoubleArray serviceTime = ServiceTimeParameter.ActualValue; 205 DoubleArray readyTime = ReadyTimeParameter.ActualValue; 206 DoubleArray demand = DemandParameter.ActualValue; 207 DoubleValue capacity = CapacityParameter.ActualValue; 208 209 bool useDistanceMatrix = UseDistanceMatrixParameter.ActualValue.Value; 210 if (useDistanceMatrix) { 211 if (DistanceMatrixParameter.ActualValue == null) { 212 DistanceMatrixParameter.ActualValue = CreateDistanceMatrix(coordinates); 213 } 214 215 coordinates = DistanceMatrixParameter.ActualValue; 216 } 217 205 218 /*----------------------------------------------------------------------------- 206 219 * generate cost list … … 208 221 */ 209 222 for (int i = 1; i <= Cities; i++) { 210 distance = Distance(i, 0 );211 if ( CoordinatesParameter.ActualValue[i, 0] < x0) distance = -distance;223 distance = Distance(i, 0, coordinates, useDistanceMatrix); 224 if (coordinates[i, 0] < x0) distance = -distance; 212 225 cost = -alpha * distance + // distance 0 <-> City[i] 213 226 beta * (DueTimeParameter.ActualValue[i]) + // latest arrival time 214 gamma * (Math.Asin(( CoordinatesParameter.ActualValue[i, 1] - y0) / distance) / 360 * distance); // polar angle227 gamma * (Math.Asin((coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle 215 228 216 229 index = 0; … … 229 242 int customer = -1; 230 243 int subTourCount = 1; 231 List<int> route = new List<int>(Cities + VehiclesParameter.ActualValue.Value - 1); 244 245 List<int> route = new List<int>(Cities + vehicles - 1); 232 246 minimumCost = double.MaxValue; 233 247 indexOfMinimumCost = -1; … … 244 258 route.Insert(i, (int)unroutedList[c]); 245 259 if (route[currentRoute] != 0) { throw new Exception("currentRoute not depot"); } 246 cost = TravelDistance(route, currentRoute );247 if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute )) {260 cost = TravelDistance(route, currentRoute, coordinates, useDistanceMatrix); 261 if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute, coordinates, useDistanceMatrix, dueTime, readyTime, serviceTime, demand, capacity)) { 248 262 minimumCost = cost; 249 263 indexOfMinimumCost = i; … … 275 289 customer = -1; 276 290 } while (unroutedList.Count > 0); 277 while (route.Count < Cities + VehiclesParameter.ActualValue.Value - 1)291 while (route.Count < Cities + vehicles) 278 292 route.Add(0); 279 293
Note: See TracChangeset
for help on using the changeset viewer.