Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/25/17 14:30:18 (7 years ago)
Author:
jkarder
Message:

#2205: worked on optimization networks

  • updated ttp networks (1, 2, 3)
  • added lrp network (1)
  • fixed plugin dependencies
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpUtils.cs

    r14601 r14604  
    11using System;
     2using System.IO;
    23using System.Linq;
    34using HeuristicLab.Problems.TravelingSalesman;
     
    56namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
    67  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);
     8    public enum DistanceType {
     9      Unknown = int.MinValue,
     10      CEIL_2D = 0,
     11      EUC_2D = 1,
    1012    }
    1113
    12     public static double EvaluateTtp(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed, out bool feasible) {
     14    public static void Import(string filePath, out double[,] tspCoordinates,
     15                                               out DistanceType distanceType,
     16                                               out int kspCapacity,
     17                                               out int[] kspItemValues,
     18                                               out int[] kspItemWeights,
     19                                               out int[] ttpAvailability,
     20                                               out double ttpMinSpeed,
     21                                               out double ttpMaxSpeed,
     22                                               out double ttpRentingRatio) {
     23      int nrOfCities = 0, nrOfItems = 0; kspCapacity = 0;
     24      ttpMinSpeed = 0.0; ttpMaxSpeed = 0.0; ttpRentingRatio = 0.0;
     25      distanceType = DistanceType.Unknown;
     26
     27      using (var fs = new FileStream(filePath, FileMode.Open))
     28      using (var sr = new StreamReader(fs)) {
     29        string input;
     30        while ((input = sr.ReadLine()) != null && !input.Contains("NODE_COORD_SECTION")) {
     31          if (input.Contains("DIMENSION:")) int.TryParse(input.Replace("DIMENSION:", string.Empty), out nrOfCities);
     32          if (input.Contains("NUMBER OF ITEMS:")) int.TryParse(input.Replace("NUMBER OF ITEMS:", string.Empty), out nrOfItems);
     33          if (input.Contains("CAPACITY OF KNAPSACK:")) int.TryParse(input.Replace("CAPACITY OF KNAPSACK:", string.Empty), out kspCapacity);
     34          if (input.Contains("MIN SPEED:")) double.TryParse(input.Replace("MIN SPEED:", string.Empty), out ttpMinSpeed);
     35          if (input.Contains("MAX SPEED:")) double.TryParse(input.Replace("MAX SPEED:", string.Empty), out ttpMaxSpeed);
     36          if (input.Contains("RENTING RATIO:")) double.TryParse(input.Replace("RENTING RATIO:", string.Empty), out ttpRentingRatio);
     37          if (input.Contains("EDGE_WEIGHT_TYPE:")) Enum.TryParse(input.Replace("EDGE_WEIGHT_TYPE:", string.Empty), out distanceType);
     38        }
     39
     40        tspCoordinates = new double[nrOfCities, 2];
     41        kspItemValues = new int[nrOfItems];
     42        kspItemWeights = new int[nrOfItems];
     43        ttpAvailability = new int[nrOfItems];
     44
     45        // read cities
     46        while ((input = sr.ReadLine()) != null && !input.Contains("ITEMS SECTION")) {
     47          string[] data = input.Split('\t');
     48          int index; double x, y;
     49          int.TryParse(data[0], out index);
     50          double.TryParse(data[1], out x);
     51          double.TryParse(data[2], out y);
     52          tspCoordinates[index - 1, 0] = x;
     53          tspCoordinates[index - 1, 1] = y;
     54        }
     55        // read items
     56        while ((input = sr.ReadLine()) != null) {
     57          string[] data = input.Split('\t');
     58          int index, value, weight, city;
     59          int.TryParse(data[0], out index);
     60          int.TryParse(data[1], out value);
     61          int.TryParse(data[2], out weight);
     62          int.TryParse(data[3], out city);
     63          kspItemValues[index - 1] = value;
     64          kspItemWeights[index - 1] = weight;
     65          ttpAvailability[index - 1] = city - 1;
     66        }
     67      }
     68    }
     69
     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) {
    1376      double collectedWeight = 0.0;
    1477      double objectiveValue = 0.0;
     
    3194        }
    3295
    33         objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[cityIdx]) * rentingRatio /
     96        objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[cityIdx], distanceType) * rentingRatio /
    3497                          (maxSpeed - speedCoefficient * oldCollectedWeight);
    3598        lastCityIdx = cityIdx;
     
    37100      }
    38101
    39       objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[hideoutIdx]) * rentingRatio /
     102      objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[hideoutIdx], distanceType) * rentingRatio /
    40103                        (maxSpeed - speedCoefficient * collectedWeight);
    41104
     
    46109    }
    47110
    48     private static double Distance(double[,] coords, int fromIdx, int toIdx) {
     111    private static double Distance(double[,] coords, int fromIdx, int toIdx, DistanceType distanceType) {
    49112      double fromX = coords[fromIdx, 0], fromY = coords[fromIdx, 1],
    50113             toX = coords[toIdx, 0], toY = coords[toIdx, 1];
    51       return (int)Math.Ceiling(Math.Sqrt((toX - fromX) * (toX - fromX) + (toY - fromY) * (toY - fromY)));
     114      double distance = Math.Sqrt((toX - fromX) * (toX - fromX) + (toY - fromY) * (toY - fromY));
     115      switch (distanceType) {
     116        case DistanceType.CEIL_2D: return (int)Math.Ceiling(distance);
     117        case DistanceType.EUC_2D: return distance;
     118        default: return 0.0;
     119      }
    52120    }
    53121  }
Note: See TracChangeset for help on using the changeset viewer.