Changeset 14653 for branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3
- Timestamp:
- 02/07/17 21:54:23 (8 years ago)
- Location:
- branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Problems/LootProfitProblem.cs
r14629 r14653 47 47 public Permutation FixedTspSolution { get; set; } 48 48 [Storable] 49 public int[]Availability { get; set; }49 public Dictionary<int, int[]> Availability { get; set; } 50 50 [Storable] 51 51 public double RentingRatio { get; set; } … … 54 54 [Storable] 55 55 public double MaxSpeed { get; set; } 56 [Storable]57 public TtpUtils.DistanceType DistanceType { get; set; }58 56 59 57 [StorableConstructor] … … 63 61 Ksp = cloner.Clone(original.Ksp); 64 62 FixedTspSolution = cloner.Clone(original.FixedTspSolution); 65 Availability = original.Availability != null ? (int[])original.Availability.Clone() : null;63 Availability = original.Availability != null ? original.Availability.ToDictionary(k => k.Key, v => (int[])v.Value.Clone()) : null; 66 64 RentingRatio = original.RentingRatio; 67 65 MinSpeed = original.MinSpeed; 68 66 MaxSpeed = original.MaxSpeed; 69 DistanceType = original.DistanceType;70 67 } 71 68 public LootProfitProblem() : base() { … … 80 77 return TtpUtils.Evaluate(Tsp, FixedTspSolution.ToArray(), 81 78 Ksp, vector.ToArray(), 82 Availability, RentingRatio, MinSpeed, MaxSpeed , DistanceType);79 Availability, RentingRatio, MinSpeed, MaxSpeed); 83 80 } 84 81 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Problems/TourProfitProblem.cs
r14629 r14653 46 46 public BinaryVector FixedKspSolution { get; set; } 47 47 [Storable] 48 public int[]Availability { get; set; }48 public Dictionary<int, int[]> Availability { get; set; } 49 49 [Storable] 50 50 public double RentingRatio { get; set; } … … 53 53 [Storable] 54 54 public double MaxSpeed { get; set; } 55 [Storable]56 public TtpUtils.DistanceType DistanceType { get; set; }57 55 58 56 [StorableConstructor] … … 62 60 Ksp = cloner.Clone(original.Ksp); 63 61 FixedKspSolution = cloner.Clone(original.FixedKspSolution); 64 Availability = original.Availability != null ? (int[])original.Availability.Clone() : null;62 Availability = original.Availability != null ? original.Availability.ToDictionary(k => k.Key, v => (int[])v.Value.Clone()) : null; 65 63 RentingRatio = original.RentingRatio; 66 64 MinSpeed = original.MinSpeed; 67 65 MaxSpeed = original.MaxSpeed; 68 DistanceType = original.DistanceType;69 66 } 70 67 public TourProfitProblem() : base() { … … 79 76 return TtpUtils.Evaluate(Tsp, individual.Permutation().ToArray(), 80 77 Ksp, FixedKspSolution.ToArray(), 81 Availability, RentingRatio, MinSpeed, MaxSpeed , DistanceType);78 Availability, RentingRatio, MinSpeed, MaxSpeed); 82 79 } 83 80 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode.cs
r14628 r14653 207 207 var tsp = TspParameter.Value; 208 208 tsp.Coordinates = new DoubleMatrix(tspCoordinates); 209 tsp.DistanceMatrix = new DistanceMatrix(TtpUtils.GetDistances(tsp.Coordinates, distanceType)); 209 210 210 211 var ksp = KspParameter.Value; -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode1.cs
r14628 r14653 90 90 var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues); 91 91 92 var availability = TtpUtils.GetAvailability(AvailabilityParameter.Value.ToArray()); 93 92 94 var tspMsg = TspSolverOrchestrationPort.PrepareMessage(); 93 95 tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start); … … 96 98 Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(), 97 99 FixedKspSolution = bestKspSolution, 98 Availability = AvailabilityParameter.Value.ToArray(),100 Availability = availability, 99 101 RentingRatio = RentingRatioParameter.Value.Value, 100 102 MinSpeed = MinSpeedParameter.Value.Value, 101 103 MaxSpeed = MaxSpeedParameter.Value.Value, 102 DistanceType = distanceType103 104 }; 104 105 tpp.Encoding.Length = TspParameter.Value.Coordinates.Rows; … … 114 115 #region Analyze 115 116 double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(), 116 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);117 availability, RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value); 117 118 ((DoubleValue)message["Quality"]).Value = objectiveValue; 118 119 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode2.cs
r14628 r14653 75 75 tsp.Coordinates[i, 1] = (int)Math.Ceiling(tsp.Coordinates[i, 1] * factors[i * 2 + 1]); 76 76 } 77 tsp.DistanceMatrix = new DistanceMatrix(TtpUtils.GetDistances(tsp.Coordinates, distanceType)); 77 78 78 79 var tspMsg = TspSolverOrchestrationPort.PrepareMessage(); … … 87 88 var tour = new PathTSPTour(coordinates, bestTspSolution.Permutation, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution.Permutation))); 88 89 90 var availability = TtpUtils.GetAvailability(AvailabilityParameter.Value.ToArray()); 91 89 92 var kspMsg = KspSolverOrchestrationPort.PrepareMessage(); 90 93 kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.ClearRuns | OrchestrationMessage.Start); … … 93 96 Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(), 94 97 FixedTspSolution = bestTspSolution.Permutation, 95 Availability = AvailabilityParameter.Value.ToArray(),98 Availability = availability, 96 99 RentingRatio = RentingRatioParameter.Value.Value, 97 100 MinSpeed = MinSpeedParameter.Value.Value, 98 101 MaxSpeed = MaxSpeedParameter.Value.Value, 99 DistanceType = distanceType100 102 }; 101 103 lpp.Encoding.Length = KspParameter.Value.Length; … … 115 117 #region Analyze 116 118 double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(), 117 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);119 availability, RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value); 118 120 ((DoubleValue)message["Quality"]).Value = objectiveValue; 119 121 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode3.cs
r14629 r14653 97 97 tsp.Coordinates[j, 1] = (int)Math.Ceiling(tsp.Coordinates[j, 1] * factors[fi + j * 2 + 1]); 98 98 } 99 tsp.DistanceMatrix = new DistanceMatrix(TtpUtils.GetDistances(tsp.Coordinates, distanceType)); 99 100 100 101 var tspMsg = TspSolverOrchestrationPort.PrepareMessage(); … … 111 112 #region Analyze 112 113 double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(), 113 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType);114 TtpUtils.GetAvailability(AvailabilityParameter.Value.ToArray()), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value); 114 115 ((DoubleValue)message["Quality"]).Value = objectiveValue; 115 116 -
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.