Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/29/14 15:43:47 (10 years ago)
Author:
pfleck
Message:

#2208:

  • Fixed bugs in cost calculation of insertion and replacement
  • Rewritten Cleanup of infeasible tours
  • Small refactoring
File:
1 edited

Legend:

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

    r11226 r11228  
    5151          if (value != null && !(value is OrienteeringProblem))
    5252            throw new ArgumentException("Only problems of type " + ProblemType.ToString() + " can be assigned.");
    53           //if (problem != null) DeregisterProblemEventHandlers();
    5453          problem = (OrienteeringProblem)value;
    55           //if (problem != null) RegisterProblemEventHandlers();
    56           //UpdateProblem();
    5754        }
    5855      }
     
    6461
    6562    #region Parameter Properties
    66 
    6763    public ILookupParameter<IntegerVector> IntegerVectorParameter {
    6864      get { return (ILookupParameter<IntegerVector>)Parameters["OrienteeringSolution"]; }
     
    104100      : base(original, cloner) {
    105101      this.problem = cloner.Clone(original.problem);
    106       //RegisterEventHandlers();
    107102    }
    108103    public OrienteeringLocalImprovementOperator()
     
    115110      Parameters.Add(new LookupParameter<IntValue>("TerminusPoint", "Index of the ending point."));
    116111      Parameters.Add(new LookupParameter<DoubleValue>("FixedPenalty", "The penalty for each visited vertex."));
     112
    117113      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(150)));
    118114      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated moves."));
    119115      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The name of the collection where the results are stored."));
    120 
    121       //RegisterEventHandlers();
    122116    }
    123117
     
    126120    }
    127121
    128     [StorableHook(HookType.AfterDeserialization)]
    129     private void AfterDeserialization() {
    130       //RegisterEventHandlers();
    131     }
    132 
    133122    public override IOperation Apply() {
    134       // TODO Use LocalImprovementorOperator Parameters
    135 
    136123      int numPoints = ScoresParameter.ActualValue.Length;
    137124      var distances = DistanceMatrixParameter.ActualValue;
    138125      var scores = ScoresParameter.ActualValue;
    139126      double fixedPenalty = FixedPenaltyParameter.ActualValue.Value;
     127      double maxLength = MaximumDistanceParameter.ActualValue.Value;
    140128
    141129      bool optimizationDone = true;
    142130
    143       // Find the maximum distance restriction
    144       double maxLength = MaximumDistanceParameter.ActualValue.Value;
    145 
    146131      var tour = IntegerVectorParameter.ActualValue.ToList();
    147132
    148133      double tourLength = distances.CalculateTourLength(tour, fixedPenalty);
    149       double score = tour.Sum(p => scores[p]);
     134      double tourScore = tour.Sum(point => scores[point]);
    150135
    151136      // Check if the tour can be improved by adding or replacing points
     
    167152        ReplacePoints(tour, visitablePoints,
    168153          distances, maxLength, scores,
    169           ref tourLength, ref score, ref optimizationDone);
    170       }
    171 
     154          ref tourLength, ref tourScore, ref optimizationDone);
     155      }
     156
     157      // Set new tour
    172158      IntegerVectorParameter.ActualValue = new IntegerVector(tour.ToArray());
    173159
     
    193179
    194180            double newLength = tourLength;
    195             for (int counterLength = (position - 1); counterLength < (position + blockLength); counterLength++) {
    196               newLength -= distances[tour[counterLength], tour[counterLength + 1]];
    197             }
    198             for (int counterLength = (position + blockLength - 1); counterLength > (position); counterLength--) {
    199               newLength += distances[tour[counterLength], tour[counterLength - 1]];
    200             }
     181            // Recalculate length of whole swapped part, in case distances are not symmetric
     182            for (int index = position - 1; index < position + blockLength; index++) newLength -= distances[tour[index], tour[index + 1]];
     183            for (int index = position + blockLength - 1; index > position; index--) newLength += distances[tour[index], tour[index - 1]];
    201184            newLength += distances[tour[position - 1], tour[position + blockLength - 1]];
    202185            newLength += distances[tour[position], tour[position + blockLength]];
    203186
    204 
    205             if (newLength <= (tourLength - 0.00001)) { // Avoid cycling caused by precision
    206               var partPath = tour.GetRange(position, blockLength);
     187            if (newLength < tourLength - 0.00001) { // Avoid cycling caused by precision
     188              var reversePart = tour.GetRange(position, blockLength).AsEnumerable().Reverse();
    207189
    208190              tour.RemoveRange(position, blockLength);
    209               tour.InsertRange(position, partPath.AsEnumerable().Reverse());
     191              tour.InsertRange(position, reversePart);
    210192
    211193              tourLength = newLength;
     
    263245          double newPointScore = scores[visitablePoints[i]];
    264246
    265           if (tourLength + detour <= maxLength && newPointScore > oldPointScore) {
     247          if ((tourLength + detour <= maxLength) && (newPointScore > oldPointScore)) {
    266248            // Replace the old point by the new one
    267249            tour[tourPosition] = visitablePoints[i];
Note: See TracChangeset for help on using the changeset viewer.