[14586] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
| 4 |
|
---|
| 5 | namespace HeuristicLab.Networks.IntegratedOptimization {
|
---|
| 6 | public static class TtpUtils {
|
---|
| 7 | public static double EvaluateTtp(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed) {
|
---|
| 8 | bool feasible;
|
---|
| 9 | return EvaluateTtp(tsp, tour, ksp, loot, availability, rentingRatio, minSpeed, maxSpeed, out feasible);
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | public static double EvaluateTtp(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed, out bool feasible) {
|
---|
| 13 | double collectedWeight = 0.0;
|
---|
| 14 | double objectiveValue = 0.0;
|
---|
| 15 | double infeasibleBaseLine = -1000000.0;
|
---|
| 16 | double speedCoefficient = (maxSpeed - minSpeed) / ksp.KnapsackCapacity.Value;
|
---|
| 17 |
|
---|
| 18 | int hideoutIdx = 0;
|
---|
| 19 | while (tour[hideoutIdx] != 0) hideoutIdx++;
|
---|
| 20 | int cityIdx = (hideoutIdx + 1) % tour.Length;
|
---|
| 21 | int lastCityIdx = hideoutIdx;
|
---|
| 22 |
|
---|
| 23 | while (cityIdx != hideoutIdx) {
|
---|
| 24 | double oldCollectedWeight = collectedWeight;
|
---|
| 25 | var availableItems = availability.Select((c, i) => new { CityIdx = c, ItemIdx = i }).Where(x => x.CityIdx == tour[cityIdx]);
|
---|
| 26 |
|
---|
| 27 | foreach (var item in availableItems) {
|
---|
| 28 | if (!loot[item.ItemIdx]) continue;
|
---|
| 29 | collectedWeight += ksp.Weights[item.ItemIdx];
|
---|
| 30 | objectiveValue += ksp.Values[item.ItemIdx];
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[cityIdx]) * rentingRatio /
|
---|
| 34 | (maxSpeed - speedCoefficient * oldCollectedWeight);
|
---|
| 35 | lastCityIdx = cityIdx;
|
---|
| 36 | cityIdx = (cityIdx + 1) % tour.Length;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[hideoutIdx]) * rentingRatio /
|
---|
| 40 | (maxSpeed - speedCoefficient * collectedWeight);
|
---|
| 41 |
|
---|
| 42 | feasible = collectedWeight <= ksp.KnapsackCapacity.Value;
|
---|
| 43 | if (!feasible) objectiveValue = infeasibleBaseLine - collectedWeight;
|
---|
| 44 |
|
---|
| 45 | return objectiveValue;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | private static double Distance(double[,] coords, int fromIdx, int toIdx) {
|
---|
| 49 | double fromX = coords[fromIdx, 0], fromY = coords[fromIdx, 1],
|
---|
| 50 | toX = coords[toIdx, 0], toY = coords[toIdx, 1];
|
---|
| 51 | return (int)Math.Ceiling(Math.Sqrt((toX - fromX) * (toX - fromX) + (toY - fromY) * (toY - fromY)));
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|