Changeset 14653 for branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpUtils.cs
- Timestamp:
- 02/07/17 21:54:23 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpUtils.cs
r14604 r14653 1 1 using System; 2 using System.Collections.Generic; 2 3 using System.IO; 3 4 using System.Linq; 5 using HeuristicLab.Data; 4 6 using HeuristicLab.Problems.TravelingSalesman; 5 7 … … 68 70 } 69 71 70 public static double Evaluate(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed, DistanceType distanceType) { 71 bool feasible; 72 return Evaluate(tsp, tour, ksp, loot, availability, rentingRatio, minSpeed, maxSpeed, distanceType, out feasible); 73 } 74 75 public static double Evaluate(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed, DistanceType distanceType, out bool feasible) { 72 public static double Evaluate(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, Dictionary<int, int[]> availability, double rentingRatio, double minSpeed, double maxSpeed) { 76 73 double collectedWeight = 0.0; 77 74 double objectiveValue = 0.0; … … 86 83 while (cityIdx != hideoutIdx) { 87 84 double oldCollectedWeight = collectedWeight; 88 var availableItems = availability.Select((c, i) => new { CityIdx = c, ItemIdx = i }).Where(x => x.CityIdx == tour[cityIdx]);89 85 90 foreach (var item in availableItems) {91 if (!loot[item .ItemIdx]) continue;92 collectedWeight += ksp.Weights[item .ItemIdx];93 objectiveValue += ksp.Values[item .ItemIdx];86 foreach (var itemIdx in availability[tour[cityIdx]]) { 87 if (!loot[itemIdx]) continue; 88 collectedWeight += ksp.Weights[itemIdx]; 89 objectiveValue += ksp.Values[itemIdx]; 94 90 } 95 91 96 objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[cityIdx], distanceType) * rentingRatio / 97 (maxSpeed - speedCoefficient * oldCollectedWeight); 92 objectiveValue -= tsp.DistanceMatrix[tour[lastCityIdx], tour[cityIdx]] * rentingRatio / (maxSpeed - speedCoefficient * oldCollectedWeight); 98 93 lastCityIdx = cityIdx; 99 94 cityIdx = (cityIdx + 1) % tour.Length; 100 95 } 101 96 102 objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[hideoutIdx], distanceType) * rentingRatio / 103 (maxSpeed - speedCoefficient * collectedWeight); 97 objectiveValue -= tsp.DistanceMatrix[tour[lastCityIdx], tour[hideoutIdx]] * rentingRatio / (maxSpeed - speedCoefficient * collectedWeight); 104 98 105 feasible = collectedWeight <= ksp.KnapsackCapacity.Value;99 bool feasible = collectedWeight <= ksp.KnapsackCapacity.Value; 106 100 if (!feasible) objectiveValue = infeasibleBaseLine - collectedWeight; 107 101 … … 109 103 } 110 104 111 private static double Distance(double[,] coords, int fromIdx, int toIdx, DistanceType distanceType) { 112 double fromX = coords[fromIdx, 0], fromY = coords[fromIdx, 1], 113 toX = coords[toIdx, 0], toY = coords[toIdx, 1]; 105 public static Dictionary<int, int[]> GetAvailability(int[] availability) { 106 return availability.Select((c, i) => Tuple.Create(c, i)).GroupBy(x => x.Item1).ToDictionary(k => k.Key, v => v.Select(x => x.Item2).ToArray()); 107 } 108 109 public static double[,] GetDistances(DoubleMatrix coordinates, DistanceType distanceType) { 110 int nrOfNodes = coordinates.Rows; 111 var distances = new double[nrOfNodes, nrOfNodes]; 112 113 for (int i = 1; i < nrOfNodes; i++) 114 for (int j = 0; j < i; j++) 115 distances[i, j] = distances[j, i] = Distance(coordinates, i, j, distanceType); 116 117 return distances; 118 } 119 120 private static double Distance(DoubleMatrix coordinates, int fromIdx, int toIdx, DistanceType distanceType) { 121 double fromX = coordinates[fromIdx, 0]; 122 double fromY = coordinates[fromIdx, 1]; 123 double toX = coordinates[toIdx, 0]; 124 double toY = coordinates[toIdx, 1]; 125 114 126 double distance = Math.Sqrt((toX - fromX) * (toX - fromX) + (toY - fromY) * (toY - fromY)); 127 115 128 switch (distanceType) { 116 129 case DistanceType.CEIL_2D: return (int)Math.Ceiling(distance); 117 130 case DistanceType.EUC_2D: return distance; 118 default: return 0.0;131 default: return double.NaN; 119 132 } 120 133 }
Note: See TracChangeset
for help on using the changeset viewer.