Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization/3.3/TtpUtils.cs @ 14586

Last change on this file since 14586 was 14586, checked in by jkarder, 7 years ago

#2205: worked on optimization networks

  • added projects for integrated optimization (orchestration)
File size: 2.5 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Problems.TravelingSalesman;
4
5namespace 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}
Note: See TracBrowser for help on using the repository browser.