Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/07/20 17:41:18 (5 years ago)
Author:
abeham
Message:

#2521: working on porting orienteering problem
Open points include:

  • Visualization of OrienteeringProblemData
  • Fix visualization of solution
  • Cleanup unused classes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Orienteering/3.3/Improvers/OrienteeringLocalImprovementOperator.cs

    r17226 r17525  
    2222using System.Collections.Generic;
    2323using System.Linq;
     24using HEAL.Attic;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    2930using HeuristicLab.Optimization;
    3031using HeuristicLab.Parameters;
    31 using HEAL.Attic;
    3232
    3333namespace HeuristicLab.Problems.Orienteering {
     
    4545      get { return (ILookupParameter<IntegerVector>)Parameters["OrienteeringSolution"]; }
    4646    }
    47     public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
    48       get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
    49     }
    50     public ILookupParameter<DoubleArray> ScoresParameter {
    51       get { return (ILookupParameter<DoubleArray>)Parameters["Scores"]; }
    52     }
    53     public ILookupParameter<DoubleValue> MaximumDistanceParameter {
    54       get { return (ILookupParameter<DoubleValue>)Parameters["MaximumDistance"]; }
    55     }
    56     public ILookupParameter<IntValue> StartingPointParameter {
    57       get { return (ILookupParameter<IntValue>)Parameters["StartingPoint"]; }
    58     }
    59     public ILookupParameter<IntValue> TerminalPointParameter {
    60       get { return (ILookupParameter<IntValue>)Parameters["TerminalPoint"]; }
    61     }
    62     public ILookupParameter<DoubleValue> PointVisitingCostsParameter {
    63       get { return (ILookupParameter<DoubleValue>)Parameters["PointVisitingCosts"]; }
    64     }
     47    public ILookupParameter<IOrienteeringProblemData> OrienteeringProblemDataParameter {
     48      get { return (ILookupParameter<IOrienteeringProblemData>)Parameters["OrienteeringProblemData"]; }
     49    }
     50
    6551    #region ILocalImprovementOperator Parameters
    6652    public IValueLookupParameter<IntValue> LocalIterationsParameter {
     
    9682      : base() {
    9783      Parameters.Add(new LookupParameter<IntegerVector>("OrienteeringSolution", "The Orienteering Solution given in path representation."));
    98       Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the points."));
    99       Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
    100       Parameters.Add(new LookupParameter<DoubleValue>("MaximumDistance", "The maximum distance constraint for a Orienteering solution."));
    101       Parameters.Add(new LookupParameter<IntValue>("StartingPoint", "Index of the starting point."));
    102       Parameters.Add(new LookupParameter<IntValue>("TerminalPoint", "Index of the ending point."));
    103       Parameters.Add(new LookupParameter<DoubleValue>("PointVisitingCosts", "The costs for visiting a point."));
     84      Parameters.Add(new LookupParameter<IOrienteeringProblemData>("OrienteeringProblemData", "The main data that comprises the orienteering problem."));
    10485
    10586      Parameters.Add(new ValueLookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed.", new IntValue(0)));
     
    11899
    119100    public override IOperation Apply() {
    120       int numPoints = ScoresParameter.ActualValue.Length;
    121       var distances = DistanceMatrixParameter.ActualValue;
    122       var scores = ScoresParameter.ActualValue;
    123       double pointVisitingCosts = PointVisitingCostsParameter.ActualValue.Value;
    124       double maxLength = MaximumDistanceParameter.ActualValue.Value;
     101      var data = OrienteeringProblemDataParameter.ActualValue;
    125102      int maxIterations = MaximumIterationsParameter.ActualValue.Value;
    126103      int maxBlockLength = MaximumBlockLengthParmeter.Value.Value;
     
    132109
    133110      double tourLength = 0;
    134       double tourScore = tour.Sum(point => scores[point]);
     111      double tourScore = tour.Sum(point => data.GetScore(point));
    135112
    136113      var localIterations = LocalIterationsParameter.ActualValue;
     
    143120
    144121        if (localIterations.Value == 0)
    145           tourLength = distances.CalculateTourLength(tour, pointVisitingCosts);
     122          tourLength = OrienteeringProblem.CalculateTravelCosts(data, tour);
    146123
    147124        // Try to shorten the path
    148         ShortenPath(tour, distances, maxBlockLength, useMaxBlockLength, ref tourLength, ref evaluations);
     125        ShortenPath(tour, data, maxBlockLength, useMaxBlockLength, ref tourLength, ref evaluations);
    149126
    150127        // Determine all points that have not yet been visited by this tour
    151         var visitablePoints = Enumerable.Range(0, numPoints).Except(tour).ToList();
     128        var visitablePoints = Enumerable.Range(0, data.RoutingData.Cities).Except(tour).ToList();
    152129
    153130        // Determine if any of the visitable points can be included at any position within the tour
    154         IncludeNewPoints(tour, visitablePoints,
    155           distances, pointVisitingCosts, maxLength, scores,
    156           ref tourLength, ref tourScore, ref evaluations, ref solutionChanged);
     131        IncludeNewPoints(tour, visitablePoints, data, ref tourLength, ref tourScore, ref evaluations, ref solutionChanged);
    157132
    158133        // Determine if any of the visitable points can take the place of an already visited point in the tour to improve the scores
    159         ReplacePoints(tour, visitablePoints,
    160           distances, maxLength, scores,
    161           ref tourLength, ref tourScore, ref evaluations, ref solutionChanged);
     134        ReplacePoints(tour, visitablePoints, data, ref tourLength, ref tourScore, ref evaluations, ref solutionChanged);
    162135
    163136        localIterations.Value++;
     
    174147    }
    175148
    176     private void ShortenPath(List<int> tour, DistanceMatrix distances, int maxBlockLength, bool useMaxBlockLength, ref double tourLength, ref int evaluations) {
     149    private void ShortenPath(List<int> tour, IOrienteeringProblemData data, int maxBlockLength, bool useMaxBlockLength, ref double tourLength, ref int evaluations) {
    177150      bool solutionChanged;
    178151      int pathSize = tour.Count;
     
    195168            double newLength = tourLength;
    196169            // Recalculate length of whole swapped part, in case distances are not symmetric
    197             for (int index = position - 1; index < position + blockLength; index++) newLength -= distances[tour[index], tour[index + 1]];
    198             for (int index = position + blockLength - 1; index > position; index--) newLength += distances[tour[index], tour[index - 1]];
    199             newLength += distances[tour[position - 1], tour[position + blockLength - 1]];
    200             newLength += distances[tour[position], tour[position + blockLength]];
     170            for (int index = position - 1; index < position + blockLength; index++) newLength -= data.RoutingData.GetDistance(tour[index], tour[index + 1]);
     171            for (int index = position + blockLength - 1; index > position; index--) newLength += data.RoutingData.GetDistance(tour[index], tour[index - 1]);
     172            newLength += data.RoutingData.GetDistance(tour[position - 1], tour[position + blockLength - 1]);
     173            newLength += data.RoutingData.GetDistance(tour[position], tour[position + blockLength]);
    201174
    202175            if (newLength < tourLength - 0.00001) {
     
    217190    }
    218191
    219     private void IncludeNewPoints(List<int> tour, List<int> visitablePoints,
    220       DistanceMatrix distances, double pointVisitingCosts, double maxLength, DoubleArray scores,
     192    private void IncludeNewPoints(List<int> tour, List<int> visitablePoints, IOrienteeringProblemData data,
    221193      ref double tourLength, ref double tourScore, ref int evaluations, ref bool solutionChanged) {
    222194
     
    231203          evaluations++;
    232204
    233           double detour = distances.CalculateInsertionCosts(tour, tourPosition, visitablePoints[i], pointVisitingCosts);
     205          double detour = OrienteeringProblem.CalculateInsertionCosts(data, tour, tourPosition, visitablePoints[i]);
    234206
    235207          // Determine if including the point does not violate any constraint
    236           if (tourLength + detour <= maxLength) {
     208          if (tourLength + detour <= data.MaximumTravelCosts) {
    237209            // Insert the new point at this position
    238210            tour.Insert(tourPosition, visitablePoints[i]);
     
    240212            // Update the overall tour tourLength and score
    241213            tourLength += detour;
    242             tourScore += scores[visitablePoints[i]];
     214            tourScore += data.GetScore(visitablePoints[i]);
    243215
    244216            // Re-run this optimization
     
    249221    }
    250222
    251     private void ReplacePoints(List<int> tour, List<int> visitablePoints,
    252       DistanceMatrix distances, double maxLength, DoubleArray scores,
     223    private void ReplacePoints(List<int> tour, List<int> visitablePoints, IOrienteeringProblemData data,
    253224      ref double tourLength, ref double tourScore, ref int evaluations, ref bool solutionChanged) {
    254225
     
    263234          evaluations++;
    264235
    265           double detour = distances.CalculateReplacementCosts(tour, tourPosition, visitablePoints[i]);
    266 
    267           double oldPointScore = scores[tour[tourPosition]];
    268           double newPointScore = scores[visitablePoints[i]];
    269 
    270           if ((tourLength + detour <= maxLength) && (newPointScore > oldPointScore)) {
     236          double detour = OrienteeringProblem.CalculateReplacementCosts(data, tour, tourPosition, visitablePoints[i]);
     237
     238          double oldPointScore = data.GetScore(tour[tourPosition]);
     239          double newPointScore = data.GetScore(visitablePoints[i]);
     240
     241          if ((tourLength + detour <= data.MaximumTravelCosts) && (newPointScore > oldPointScore)) {
    271242            // Replace the old point by the new one
    272243            tour[tourPosition] = visitablePoints[i];
Note: See TracChangeset for help on using the changeset viewer.