Changeset 4098 for trunk/sources/HeuristicLab.Problems.VehicleRouting
- Timestamp:
- 07/25/10 01:04:14 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs
r4068 r4098 170 170 get { return EvaluatorParameter.Value; } 171 171 } 172 private List<IOperator> operators;173 172 public IEnumerable<IOperator> Operators { 174 173 get { return operators; } … … 179 178 #endregion 180 179 180 [Storable] 181 private List<IOperator> operators; 182 183 [StorableConstructor] 184 private VehicleRoutingProblem(bool deserializing) : base() { } 181 185 public VehicleRoutingProblem() 182 186 : base() { … … 209 213 ParameterizeEvaluator(); 210 214 211 Initialize(); 212 } 213 [StorableConstructor] 214 private VehicleRoutingProblem(bool deserializing) : base() { } 215 InitializeOperators(); 216 AttachEventHandlers(); 217 } 215 218 216 219 public override IDeepCloneable Clone(Cloner cloner) { 217 220 VehicleRoutingProblem clone = (VehicleRoutingProblem)base.Clone(cloner); 221 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 218 222 clone.DistanceMatrixParameter.Value = DistanceMatrixParameter.Value; 219 clone. Initialize();223 clone.AttachEventHandlers(); 220 224 return clone; 221 }222 223 private static double CalculateDistance(int start, int end, DoubleMatrix coordinates) {224 double distance = 0.0;225 226 distance =227 Math.Sqrt(228 Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) +229 Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2));230 231 return distance;232 }233 234 private static DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) {235 DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows);236 237 for (int i = 0; i < distanceMatrix.Rows; i++) {238 for (int j = i; j < distanceMatrix.Columns; j++) {239 double distance = CalculateDistance(i, j, coordinates);240 241 distanceMatrix[i, j] = distance;242 distanceMatrix[j, i] = distance;243 }244 }245 246 return distanceMatrix;247 }248 249 public static double GetDistance(int start, int end,250 DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {251 double distance = 0.0;252 253 if (useDistanceMatrix.Value) {254 if (distanceMatrix.ActualValue == null) {255 distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates);256 }257 258 distance = distanceMatrix.ActualValue[start, end];259 } else {260 distance = CalculateDistance(start, end, coordinates);261 }262 263 return distance;264 }265 266 public static double GetDistance(int start, int end,267 DoubleMatrix coordinates, DoubleMatrix distanceMatrix, BoolValue useDistanceMatrix) {268 double distance = 0.0;269 270 if (useDistanceMatrix.Value) {271 distance = distanceMatrix[start, end];272 } else {273 distance = CalculateDistance(start, end, coordinates);274 }275 276 return distance;277 }278 279 public void ImportFromSolomon(string solomonFileName) {280 SolomonParser parser = new SolomonParser(solomonFileName);281 parser.Parse();282 283 this.Name = parser.ProblemName;284 285 Coordinates = new DoubleMatrix(parser.Coordinates);286 Vehicles.Value = parser.Vehicles;287 Capacity.Value = parser.Capacity;288 Demand = new DoubleArray(parser.Demands);289 ReadyTime = new DoubleArray(parser.Readytimes);290 DueTime = new DoubleArray(parser.Duetimes);291 ServiceTime = new DoubleArray(parser.Servicetimes);292 293 OnReset();294 225 } 295 226 … … 364 295 #region Helpers 365 296 [StorableHook(HookType.AfterDeserialization)] 366 private void Initialize() { 367 InitializeOperators(); 297 private void AttachEventHandlers() { 298 // Start BackwardsCompatibility3.3 (remove with 3.4) 299 if (operators == null) InitializeOperators(); 300 // End BackwardsCompatibility3.3 368 301 CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged); 369 302 Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged); … … 471 404 } 472 405 #endregion 406 407 private static double CalculateDistance(int start, int end, DoubleMatrix coordinates) { 408 double distance = 0.0; 409 410 distance = 411 Math.Sqrt( 412 Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) + 413 Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2)); 414 415 return distance; 416 } 417 418 private static DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) { 419 DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows); 420 421 for (int i = 0; i < distanceMatrix.Rows; i++) { 422 for (int j = i; j < distanceMatrix.Columns; j++) { 423 double distance = CalculateDistance(i, j, coordinates); 424 425 distanceMatrix[i, j] = distance; 426 distanceMatrix[j, i] = distance; 427 } 428 } 429 430 return distanceMatrix; 431 } 432 433 public static double GetDistance(int start, int end, 434 DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) { 435 double distance = 0.0; 436 437 if (useDistanceMatrix.Value) { 438 if (distanceMatrix.ActualValue == null) { 439 distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates); 440 } 441 442 distance = distanceMatrix.ActualValue[start, end]; 443 } else { 444 distance = CalculateDistance(start, end, coordinates); 445 } 446 447 return distance; 448 } 449 450 public static double GetDistance(int start, int end, 451 DoubleMatrix coordinates, DoubleMatrix distanceMatrix, BoolValue useDistanceMatrix) { 452 double distance = 0.0; 453 454 if (useDistanceMatrix.Value) { 455 distance = distanceMatrix[start, end]; 456 } else { 457 distance = CalculateDistance(start, end, coordinates); 458 } 459 460 return distance; 461 } 462 463 public void ImportFromSolomon(string solomonFileName) { 464 SolomonParser parser = new SolomonParser(solomonFileName); 465 parser.Parse(); 466 467 this.Name = parser.ProblemName; 468 469 Coordinates = new DoubleMatrix(parser.Coordinates); 470 Vehicles.Value = parser.Vehicles; 471 Capacity.Value = parser.Capacity; 472 Demand = new DoubleArray(parser.Demands); 473 ReadyTime = new DoubleArray(parser.Readytimes); 474 DueTime = new DoubleArray(parser.Duetimes); 475 ServiceTime = new DoubleArray(parser.Servicetimes); 476 477 OnReset(); 478 } 473 479 } 474 480 }
Note: See TracChangeset
for help on using the changeset viewer.