Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/07/17 21:54:23 (7 years ago)
Author:
jkarder
Message:

#2205: worked on optimization networks

  • improved ttp evaluation
File:
1 edited

Legend:

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

    r14604 r14653  
    11using System;
     2using System.Collections.Generic;
    23using System.IO;
    34using System.Linq;
     5using HeuristicLab.Data;
    46using HeuristicLab.Problems.TravelingSalesman;
    57
     
    6870    }
    6971
    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) {
    7673      double collectedWeight = 0.0;
    7774      double objectiveValue = 0.0;
     
    8683      while (cityIdx != hideoutIdx) {
    8784        double oldCollectedWeight = collectedWeight;
    88         var availableItems = availability.Select((c, i) => new { CityIdx = c, ItemIdx = i }).Where(x => x.CityIdx == tour[cityIdx]);
    8985
    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];
    9490        }
    9591
    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);
    9893        lastCityIdx = cityIdx;
    9994        cityIdx = (cityIdx + 1) % tour.Length;
    10095      }
    10196
    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);
    10498
    105       feasible = collectedWeight <= ksp.KnapsackCapacity.Value;
     99      bool feasible = collectedWeight <= ksp.KnapsackCapacity.Value;
    106100      if (!feasible) objectiveValue = infeasibleBaseLine - collectedWeight;
    107101
     
    109103    }
    110104
    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
    114126      double distance = Math.Sqrt((toX - fromX) * (toX - fromX) + (toY - fromY) * (toY - fromY));
     127
    115128      switch (distanceType) {
    116129        case DistanceType.CEIL_2D: return (int)Math.Ceiling(distance);
    117130        case DistanceType.EUC_2D: return distance;
    118         default: return 0.0;
     131        default: return double.NaN;
    119132      }
    120133    }
Note: See TracChangeset for help on using the changeset viewer.