- Timestamp:
- 03/05/12 16:30:27 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GeneralizedQuadraticAssignmentProblem.cs
r7538 r7548 39 39 [StorableClass] 40 40 public sealed class GeneralizedQuadraticAssignmentProblem : SingleObjectiveHeuristicOptimizationProblem<IGQAPEvaluator, IGQAPSolutionCreator>, IStorableContent, 41 I Consumable<QAPInstance>,42 I Consumable<CTAPInstance>,43 I Consumable<TSPInstance>,44 I Consumable<ATSPInstance>,45 I Consumable<GQAPInstance> {41 IProblemInstanceConsumer<QAPData>, 42 IProblemInstanceConsumer<CTAPData>, 43 IProblemInstanceConsumer<TSPData>, 44 IProblemInstanceConsumer<ATSPData>, 45 IProblemInstanceConsumer<GQAPData> { 46 46 47 47 public override Image ItemImage { … … 169 169 Parameters.Add(new ValueParameter<DoubleMatrix>("InstallationCosts", InstallationCostsDescription, new DoubleMatrix(), false)); 170 170 Parameters.Add(new FixedValueParameter<DoubleValue>("TransportationCosts", TransportationCostsDescription, new DoubleValue(1))); 171 Parameters.Add(new FixedValueParameter<DoubleValue>("OverbookedCapacityPenalty", OverbookedCapacityPenaltyDescription, new DoubleValue(1000 )));171 Parameters.Add(new FixedValueParameter<DoubleValue>("OverbookedCapacityPenalty", OverbookedCapacityPenaltyDescription, new DoubleValue(100000))); 172 172 Parameters.Add(new ValueParameter<DoubleArray>("Demands", DemandsDescription, new DoubleArray(), false)); 173 173 Parameters.Add(new ValueParameter<DoubleArray>("Capacities", CapacitiesDescription, new DoubleArray(), false)); … … 213 213 } 214 214 215 #region Problem Instance Consumptions 216 public bool LoadFrom(QAPInstance instance) { 217 try { 218 Name = instance.Name; 219 Description = instance.Description; 220 221 Weights = new DoubleMatrix(instance.Weights); 222 Distances = new DoubleMatrix(instance.Distances); 223 InstallationCosts = new DoubleMatrix(Weights.Rows, Distances.Columns); // all zero 224 Capacities = new DoubleArray(Enumerable.Range(0, Distances.Rows).Select(x => 1.0).ToArray()); 225 Demands = new DoubleArray(Enumerable.Range(0, Weights.Rows).Select(x => 1.0).ToArray()); 226 227 TransportationCosts.Value = 1; 228 229 if (instance.BestKnownAssignment != null) { 230 EvaluateAndLoadAssignment(instance.BestKnownAssignment); 231 } else { 232 BestKnownQuality = null; 233 BestKnownSolution = null; 234 BestKnownSolutions = null; 215 #region Problem Instance Loading 216 public void Load(QAPData data) { 217 Name = data.Name; 218 Description = data.Description; 219 220 var weights = new DoubleMatrix(data.Weights); 221 var distances = new DoubleMatrix(data.Distances); 222 var installationCosts = new DoubleMatrix(weights.Rows, distances.Columns); // all zero 223 var capacities = new DoubleArray(Enumerable.Range(0, distances.Rows).Select(x => 1.0).ToArray()); 224 var demands = new DoubleArray(Enumerable.Range(0, weights.Rows).Select(x => 1.0).ToArray()); 225 226 Load(demands, capacities, weights, distances, installationCosts, new DoubleValue(1)); 227 EvaluateAndLoadAssignment(data.BestKnownAssignment); 228 } 229 230 public void Load(CTAPData data) { 231 Name = data.Name; 232 Description = data.Description; 233 234 var capacities = new DoubleArray(data.MemoryCapacities); 235 var demands = new DoubleArray(data.MemoryRequirements); 236 var weights = new DoubleMatrix(data.CommunicationCosts); 237 var installationCosts = new DoubleMatrix(data.ExecutionCosts.Transpose()); 238 var distances = new DoubleMatrix(capacities.Length, capacities.Length); 239 for (int i = 0; i < capacities.Length - 1; i++) 240 for (int j = i + 1; j < capacities.Length; j++) { 241 distances[i, j] = 1; 235 242 } 236 } catch { 237 return false; 238 } 239 return true; 240 } 241 242 public bool LoadFrom(CTAPInstance instance) { 243 try { 244 Name = instance.Name; 245 Description = instance.Description; 246 247 Capacities = new DoubleArray(instance.MemoryCapacities); 248 Demands = new DoubleArray(instance.MemoryRequirements); 249 Weights = new DoubleMatrix(instance.CommunicationCosts); 250 InstallationCosts = new DoubleMatrix(instance.ExecutionCosts.Transpose()); 251 Distances = new DoubleMatrix(Capacities.Length, Capacities.Length); 252 for (int i = 0; i < Capacities.Length - 1; i++) 253 for (int j = i + 1; j < Capacities.Length; j++) { 254 Distances[i, j] = 1; 255 } 256 257 TransportationCosts.Value = 1; 258 259 if (instance.BestKnownAssignment != null) { 260 EvaluateAndLoadAssignment(instance.BestKnownAssignment); 261 } else { 262 BestKnownQuality = null; 263 BestKnownSolution = null; 264 BestKnownSolutions = null; 265 } 266 } catch { 267 return false; 268 } 269 return true; 270 } 271 272 public bool LoadFrom(TSPInstance instance) { 273 try { 274 if (instance.Dimension > 1000) return false; 275 276 Name = instance.Name; 277 Description = instance.Description; 278 279 Capacities = new DoubleArray(instance.Dimension); 280 Demands = new DoubleArray(instance.Dimension); 281 for (int i = 0; i < instance.Dimension; i++) { 282 Capacities[i] = 1; 283 Demands[i] = 1; 284 } 285 InstallationCosts = new DoubleMatrix(instance.Dimension, instance.Dimension); 286 Weights = new DoubleMatrix(instance.Dimension, instance.Dimension); 287 for (int i = 0; i < instance.Dimension; i++) 288 Weights[i, (i + 1) % instance.Dimension] = 1; 289 Distances = new DoubleMatrix(instance.GetDistanceMatrix()); 290 291 TransportationCosts.Value = 1; 292 293 if (instance.BestKnownTour != null) { 294 EvaluateAndLoadAssignment(instance.BestKnownTour); 295 } else { 296 BestKnownQuality = null; 297 BestKnownSolution = null; 298 BestKnownSolutions = null; 299 } 300 } catch { 301 return false; 302 } 303 return true; 304 } 305 306 public bool LoadFrom(ATSPInstance instance) { 307 try { 308 Name = instance.Name; 309 Description = instance.Description; 310 311 Capacities = new DoubleArray(instance.Dimension); 312 Demands = new DoubleArray(instance.Dimension); 313 for (int i = 0; i < instance.Dimension; i++) { 314 Capacities[i] = 1; 315 Demands[i] = 1; 316 } 317 InstallationCosts = new DoubleMatrix(instance.Dimension, instance.Dimension); 318 Weights = new DoubleMatrix(instance.Dimension, instance.Dimension); 319 for (int i = 0; i < instance.Dimension; i++) 320 Weights[i, (i + 1) % instance.Dimension] = 1; 321 Distances = new DoubleMatrix(instance.Distances); 322 323 TransportationCosts.Value = 1; 324 325 if (instance.BestKnownTour != null) { 326 EvaluateAndLoadAssignment(instance.BestKnownTour); 327 } else { 328 BestKnownQuality = null; 329 BestKnownSolution = null; 330 BestKnownSolutions = null; 331 } 332 } catch { 333 return false; 334 } 335 return true; 336 } 337 338 public bool LoadFrom(GQAPInstance instance) { 339 try { 340 Name = instance.Name; 341 Description = instance.Description; 342 343 Capacities = new DoubleArray(instance.Capacities); 344 Demands = new DoubleArray(instance.Demands); 345 InstallationCosts = new DoubleMatrix(instance.InstallationCosts); 346 Weights = new DoubleMatrix(instance.Weights); 347 Distances = new DoubleMatrix(instance.Distances); 348 TransportationCosts.Value = instance.TransportationCosts; 349 350 if (instance.BestKnownAssignment != null) { 351 EvaluateAndLoadAssignment(instance.BestKnownAssignment); 352 } else if (instance.BestKnownQuality.HasValue) { 353 BestKnownQuality = new DoubleValue(instance.BestKnownQuality.Value); 354 BestKnownSolution = null; 355 BestKnownSolutions = null; 356 } else { 357 BestKnownQuality = null; 358 BestKnownSolution = null; 359 BestKnownSolutions = null; 360 } 361 } catch { 362 return false; 363 } 364 return true; 365 } 366 #endregion 243 244 Load(demands, capacities, weights, distances, installationCosts, new DoubleValue(1)); 245 EvaluateAndLoadAssignment(data.BestKnownAssignment); 246 } 247 248 public void Load(TSPData data) { 249 if (data.Dimension > 1000) 250 throw new System.IO.InvalidDataException("Instances with more than 1000 cities are not supported."); 251 252 Name = data.Name; 253 Description = data.Description; 254 255 var capacities = new DoubleArray(data.Dimension); 256 var demands = new DoubleArray(data.Dimension); 257 for (int i = 0; i < data.Dimension; i++) { 258 capacities[i] = 1; 259 demands[i] = 1; 260 } 261 var installationCosts = new DoubleMatrix(data.Dimension, data.Dimension); 262 var weights = new DoubleMatrix(data.Dimension, data.Dimension); 263 for (int i = 0; i < data.Dimension; i++) 264 weights[i, (i + 1) % data.Dimension] = 1; 265 var distances = new DoubleMatrix(data.GetDistanceMatrix()); 266 267 Load(demands, capacities, weights, distances, installationCosts, new DoubleValue(1)); 268 EvaluateAndLoadAssignment(data.BestKnownTour); 269 } 270 271 public void Load(ATSPData data) { 272 Name = data.Name; 273 Description = data.Description; 274 275 var capacities = new DoubleArray(data.Dimension); 276 var demands = new DoubleArray(data.Dimension); 277 for (int i = 0; i < data.Dimension; i++) { 278 capacities[i] = 1; 279 demands[i] = 1; 280 } 281 var installationCosts = new DoubleMatrix(data.Dimension, data.Dimension); 282 var weights = new DoubleMatrix(data.Dimension, data.Dimension); 283 for (int i = 0; i < data.Dimension; i++) 284 weights[i, (i + 1) % data.Dimension] = 1; 285 var distances = new DoubleMatrix(data.Distances); 286 287 Load(demands, capacities, weights, distances, installationCosts, new DoubleValue(1)); 288 EvaluateAndLoadAssignment(data.BestKnownTour); 289 } 290 291 public void Load(GQAPData data) { 292 Name = data.Name; 293 Description = data.Description; 294 295 var capacities = new DoubleArray(data.Capacities); 296 var demands = new DoubleArray(data.Demands); 297 var installationCosts = new DoubleMatrix(data.InstallationCosts); 298 var weights = new DoubleMatrix(data.Weights); 299 var distances = new DoubleMatrix(data.Distances); 300 var transportationCosts = new DoubleValue(data.TransportationCosts); 301 302 Load(demands, capacities, weights, distances, installationCosts, transportationCosts); 303 EvaluateAndLoadAssignment(data.BestKnownAssignment); 304 305 if (data.BestKnownAssignment == null && data.BestKnownQuality.HasValue) { 306 BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value); 307 } 308 } 309 310 public void Load(DoubleArray demands, DoubleArray capacities, 311 DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, 312 DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty = null) { 313 if (weights == null || weights.Rows == 0) 314 throw new System.IO.InvalidDataException( 315 @"The given instance does not contain weights!"); 316 if (weights.Rows != weights.Columns) 317 throw new System.IO.InvalidDataException( 318 @"The weights matrix of the given instance contains an unequal number of rows 319 and columns."); 320 Weights = weights; 321 322 if (distances == null || distances.Rows == 0) 323 throw new System.IO.InvalidDataException( 324 @"The given instance does not contain distances!"); 325 if (distances.Rows != distances.Columns) 326 throw new System.IO.InvalidDataException( 327 @"The distances matrix of the given instance contains an unequal number of rows 328 and columns."); 329 Distances = distances; 330 331 if (installationCosts == null || installationCosts.Rows == 0) 332 throw new System.IO.InvalidDataException( 333 @"The given instance does not contain installation costs!"); 334 if (installationCosts.Rows != weights.Rows 335 || installationCosts.Columns != distances.Columns) 336 throw new System.IO.InvalidDataException( 337 @"The installation costs matrix of the given instance contains different number 338 of rows than given in the weights matrix and a different number of columns than 339 given in the distances matrix."); 340 InstallationCosts = installationCosts; 341 342 if (capacities == null || capacities.Length == 0) 343 throw new System.IO.InvalidDataException( 344 @"The given instance does not contain capacities!"); 345 if (capacities.Length != distances.Rows) 346 throw new System.IO.InvalidDataException( 347 @"The given instance contains a different number of capacities than rows given in 348 the distances matrix."); 349 Capacities = capacities; 350 351 if (demands == null || demands.Length == 0) 352 throw new System.IO.InvalidDataException( 353 @"The given instance does not contain demands!"); 354 if (demands.Length != weights.Rows) 355 throw new System.IO.InvalidDataException( 356 @"The given instance contains a different number of demands than rows given in 357 the weights matrix."); 358 Demands = demands; 359 360 if (transportationCosts == null) 361 throw new System.IO.InvalidDataException( 362 @"The given instance does not contain transportation costs."); 363 TransportationCosts.Value = transportationCosts.Value; 364 365 if (overbookedCapacityPenalty != null) 366 OverbookedCapacityPenalty.Value = overbookedCapacityPenalty.Value; 367 368 BestKnownQuality = null; 369 BestKnownSolution = null; 370 BestKnownSolutions = null; 371 } 367 372 368 373 private void EvaluateAndLoadAssignment(int[] vector) { 374 if (vector == null || vector.Length == 0) return; 369 375 EvaluateAndLoadAssignment(new IntegerVector(vector)); 370 376 } 371 p rivatevoid EvaluateAndLoadAssignment(IntegerVector assignment) {377 public void EvaluateAndLoadAssignment(IntegerVector assignment) { 372 378 if (!IsConfigurationValid()) { 373 379 BestKnownQuality = null; … … 387 393 BestKnownSolutions.Solutions.Add(new GQAPSolution((IntegerVector)assignment.Clone(), new DoubleValue(quality), new DoubleValue(flowDistanceQuality), new DoubleValue(installationQuality), new DoubleValue(overbookedCapacity))); 388 394 } 395 #endregion 389 396 390 397 #region Events … … 525 532 #endregion 526 533 527 p rivatebool IsConfigurationValid() {534 public bool IsConfigurationValid() { 528 535 return Weights != null && Distances != null && Demands != null && Capacities != null && InstallationCosts != null 529 536 && Weights.Rows == Weights.Columns && Weights.Rows == InstallationCosts.Rows
Note: See TracChangeset
for help on using the changeset viewer.