Changeset 11228 for branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/Improvers/OrienteeringLocalImprovementOperator.cs
- Timestamp:
- 07/29/14 15:43:47 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/Improvers/OrienteeringLocalImprovementOperator.cs
r11226 r11228 51 51 if (value != null && !(value is OrienteeringProblem)) 52 52 throw new ArgumentException("Only problems of type " + ProblemType.ToString() + " can be assigned."); 53 //if (problem != null) DeregisterProblemEventHandlers();54 53 problem = (OrienteeringProblem)value; 55 //if (problem != null) RegisterProblemEventHandlers();56 //UpdateProblem();57 54 } 58 55 } … … 64 61 65 62 #region Parameter Properties 66 67 63 public ILookupParameter<IntegerVector> IntegerVectorParameter { 68 64 get { return (ILookupParameter<IntegerVector>)Parameters["OrienteeringSolution"]; } … … 104 100 : base(original, cloner) { 105 101 this.problem = cloner.Clone(original.problem); 106 //RegisterEventHandlers();107 102 } 108 103 public OrienteeringLocalImprovementOperator() … … 115 110 Parameters.Add(new LookupParameter<IntValue>("TerminusPoint", "Index of the ending point.")); 116 111 Parameters.Add(new LookupParameter<DoubleValue>("FixedPenalty", "The penalty for each visited vertex.")); 112 117 113 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(150))); 118 114 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated moves.")); 119 115 Parameters.Add(new LookupParameter<ResultCollection>("Results", "The name of the collection where the results are stored.")); 120 121 //RegisterEventHandlers();122 116 } 123 117 … … 126 120 } 127 121 128 [StorableHook(HookType.AfterDeserialization)]129 private void AfterDeserialization() {130 //RegisterEventHandlers();131 }132 133 122 public override IOperation Apply() { 134 // TODO Use LocalImprovementorOperator Parameters135 136 123 int numPoints = ScoresParameter.ActualValue.Length; 137 124 var distances = DistanceMatrixParameter.ActualValue; 138 125 var scores = ScoresParameter.ActualValue; 139 126 double fixedPenalty = FixedPenaltyParameter.ActualValue.Value; 127 double maxLength = MaximumDistanceParameter.ActualValue.Value; 140 128 141 129 bool optimizationDone = true; 142 130 143 // Find the maximum distance restriction144 double maxLength = MaximumDistanceParameter.ActualValue.Value;145 146 131 var tour = IntegerVectorParameter.ActualValue.ToList(); 147 132 148 133 double tourLength = distances.CalculateTourLength(tour, fixedPenalty); 149 double score = tour.Sum(p => scores[p]);134 double tourScore = tour.Sum(point => scores[point]); 150 135 151 136 // Check if the tour can be improved by adding or replacing points … … 167 152 ReplacePoints(tour, visitablePoints, 168 153 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 172 158 IntegerVectorParameter.ActualValue = new IntegerVector(tour.ToArray()); 173 159 … … 193 179 194 180 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]]; 201 184 newLength += distances[tour[position - 1], tour[position + blockLength - 1]]; 202 185 newLength += distances[tour[position], tour[position + blockLength]]; 203 186 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(); 207 189 208 190 tour.RemoveRange(position, blockLength); 209 tour.InsertRange(position, partPath.AsEnumerable().Reverse());191 tour.InsertRange(position, reversePart); 210 192 211 193 tourLength = newLength; … … 263 245 double newPointScore = scores[visitablePoints[i]]; 264 246 265 if ( tourLength + detour <= maxLength && newPointScore > oldPointScore) {247 if ((tourLength + detour <= maxLength) && (newPointScore > oldPointScore)) { 266 248 // Replace the old point by the new one 267 249 tour[tourPosition] = visitablePoints[i];
Note: See TracChangeset
for help on using the changeset viewer.