Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11192


Ignore:
Timestamp:
07/16/14 10:02:10 (10 years ago)
Author:
pfleck
Message:

#2208 improved GreedyTourCreator by using an iterative length calculation instead of re-evaluation.

Location:
branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/Creators/GreedyOrienteeringTourCreator.cs

    r11191 r11192  
    9494        let distance = distances[startPoint, i] + distances[i, endPoint] + fixedPenalty
    9595        where distance <= maxDistance && i != startPoint && i != endPoint
    96         select new { Distance = distance, Index = i, Score = scores[i] };
     96        select new { Index = i, Score = scores[i], Distance = distance };
    9797
    9898      var orderedFeasiblePoints = feasiblePoints.OrderByDescending(p => p.Score).ToList();
     
    103103        endPoint
    104104      };
     105      double length = distances[startPoint, endPoint];
    105106
    106107      // Add points in a greedy way
     
    109110        insertionPerformed = false;
    110111
    111         for (int i = 0; i < orderedFeasiblePoints.Count; i++) {
    112           for (int position = 1; position < tour.Count; position++) {
     112        for (int candidatePoint = 0; candidatePoint < orderedFeasiblePoints.Count; candidatePoint++) {
     113          for (int insertPosition = 1; insertPosition < tour.Count; insertPosition++) {
    113114            // Create the candidate tour
    114             var candidateTour = new List<int>(tour);
    115             candidateTour.Insert(position, orderedFeasiblePoints[i].Index);
    116             double length = CalculateTourLength(candidateTour);
     115            double detour = distances.CalculateInsertionCost(tour, orderedFeasiblePoints[candidatePoint].Index, insertPosition, fixedPenalty);
    117116
    118117            // If the insertion would be feasible, perform it
    119             if (length <= maxDistance) {
    120               tour = candidateTour;
    121               orderedFeasiblePoints.RemoveAt(i);
     118            if (length + detour <= maxDistance) {
     119              tour.Insert(insertPosition, orderedFeasiblePoints[candidatePoint].Index);
     120              length += detour;
     121              orderedFeasiblePoints.RemoveAt(candidatePoint);
    122122              insertionPerformed = true;
    123123              break;
     
    130130      return new IntegerVector(tour.ToArray());
    131131    }
    132 
    133 
    134     private double CalculateTourLength(IList<int> path) {
    135       var distances = DistanceMatrixParameter.ActualValue;
    136       var fixedPenalty = FixedPenaltyParameter.ActualValue.Value;
    137 
    138       double length = 0.0;
    139 
    140       for (int i = 1; i < path.Count; i++) {
    141         length += distances[i - 1, i];
    142       }
    143       // Add the fixed penalty for every vertex except for the starting and ending vertex
    144       if (path.Count > 1) {
    145         if (path[path.Count - 1] == TerminusPointParameter.ActualValue.Value) length += ((path.Count - 2.0) * fixedPenalty);
    146         else length += ((path.Count - 1.0) * fixedPenalty);
    147       }
    148 
    149       return length;
    150     }
    151132  }
    152133}
  • branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/DistanceMatrix.cs

    r11187 r11192  
    5252      return this;
    5353    }
     54
     55    public double CalculateTourLength(IList<int> path, double fixedPenalty) {
     56      double length = 0.0;
     57
     58      for (int i = 1; i < path.Count; i++) {
     59        length += this[i - 1, i];
     60      }
     61
     62      // Add the fixed penalty for every vertex except for the starting and ending vertex
     63      length += (path.Count - 2) * fixedPenalty;
     64
     65      return length;
     66    }
     67
     68    public double CalculateInsertionCost(IList<int> path, int point, int insertPosition, double fixedPenalty) {
     69      double detour = this[path[insertPosition - 1], point] + this[point, path[insertPosition]];
     70      detour += fixedPenalty;
     71      detour -= this[path[insertPosition - 1], path[insertPosition]];
     72      return detour;
     73    }
    5474  }
    5575}
Note: See TracChangeset for help on using the changeset viewer.