Changeset 13470 for branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Improvers/PTSPAnalyticalInsertionLocalImprovement.cs
- Timestamp:
- 12/15/15 16:38:08 (9 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Improvers/PTSPAnalyticalInsertionLocalImprovement.cs
r13451 r13470 38 38 /// The operator tries to improve the probabilistic traveling salesman solution by inserting a city in the tour between two other cities for a certain number of times. 39 39 /// </remarks> 40 [Item("PTSP ExhaustiveInsertionLocalImprovement", "An operator that improves probabilistic traveling salesman solutions. The operator tries to improve the probabilistic traveling salesman solution by swapping two randomly chosen edges for a certain number of times.")]40 [Item("PTSP Analytical Insertion Local Improvement", "An operator that improves probabilistic traveling salesman solutions. The operator tries to improve the probabilistic traveling salesman solution by swapping two randomly chosen edges for a certain number of times.")] 41 41 [StorableClass] 42 public sealed class PTSP ExhaustiveInsertionLocalImprovement : SingleSuccessorOperator, IEstimatedPTSPOperator, ILocalImprovementOperator {42 public sealed class PTSPAnalyticalInsertionLocalImprovement : SingleSuccessorOperator, IAnalyticalPTSPOperator, ILocalImprovementOperator { 43 43 44 44 public ILookupParameter<IntValue> LocalIterationsParameter { … … 74 74 } 75 75 76 public ILookupParameter< ItemList<BoolArray>> RealizationsParameter {77 get { return (ILookupParameter< ItemList<BoolArray>>)Parameters["Realizations"]; }76 public ILookupParameter<DoubleArray> ProbabilitiesParameter { 77 get { return (ILookupParameter<DoubleArray>)Parameters["Probabilities"]; } 78 78 } 79 79 80 80 [StorableConstructor] 81 private PTSP ExhaustiveInsertionLocalImprovement(bool deserializing) : base(deserializing) { }82 private PTSP ExhaustiveInsertionLocalImprovement(PTSPExhaustiveInsertionLocalImprovement original, Cloner cloner) : base(original, cloner) { }83 public PTSP ExhaustiveInsertionLocalImprovement()81 private PTSPAnalyticalInsertionLocalImprovement(bool deserializing) : base(deserializing) { } 82 private PTSPAnalyticalInsertionLocalImprovement(PTSPAnalyticalInsertionLocalImprovement original, Cloner cloner) : base(original, cloner) { } 83 public PTSPAnalyticalInsertionLocalImprovement() 84 84 : base() { 85 85 Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation.")); … … 91 91 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem should be maximized or minimized.")); 92 92 Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 93 Parameters.Add(new LookupParameter< ItemList<BoolArray>>("Realizations", "The list of samples drawn from all possible stochastic instances."));93 Parameters.Add(new LookupParameter<DoubleArray>("Probabilities", "The list of probabilities of the cities to appear.")); 94 94 } 95 95 96 96 public override IDeepCloneable Clone(Cloner cloner) { 97 return new PTSP ExhaustiveInsertionLocalImprovement(this, cloner);97 return new PTSPAnalyticalInsertionLocalImprovement(this, cloner); 98 98 } 99 99 100 public static void Improve(Permutation assignment, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation, ItemList<BoolArray> realizations) {100 public static void Improve(Permutation assignment, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, DoubleArray probabilities, CancellationToken cancellation) { 101 101 var distanceM = (DistanceMatrix)distances; 102 Func<int, int, double> distance = (a, b) => distanceM[a, b]; 102 103 for (var i = localIterations.Value; i < maxIterations; i++) { 103 104 TranslocationMove bestMove = null; 104 double bestQuality = 0; // we have to make an improvement, so 0is the baseline105 doubleevaluations = 0.0;105 var bestQuality = quality.Value; // we have to make an improvement, so current quality is the baseline 106 var evaluations = 0.0; 106 107 foreach (var move in ExhaustiveInsertionMoveGenerator.Generate(assignment)) { 107 double moveQuality = PTSPEstimatedInsertionMoveEvaluator.EvaluateByDistanceMatrix(assignment, move, distanceM, realizations); 108 int min = Math.Min(move.Index1, move.Index3); 109 int max = Math.Max(move.Index2, move.Index3 + (move.Index2 - move.Index1)); 110 evaluations += 2.0 * (max - min + 1) / assignment.Length 111 + 4.0 * (assignment.Length - (max - min + 1)) / assignment.Length; 108 var moveQuality = PTSPAnalyticalInsertionMoveEvaluator.EvaluateMove(assignment, move, distance, probabilities); 109 evaluations++; 112 110 if (maximization && moveQuality > bestQuality 113 111 || !maximization && moveQuality < bestQuality) { … … 119 117 if (bestMove == null) break; 120 118 TranslocationManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2, bestMove.Index3); 121 quality.Value += bestQuality;119 quality.Value = bestQuality; 122 120 localIterations.Value++; 123 121 cancellation.ThrowIfCancellationRequested(); … … 133 131 var localIterations = LocalIterationsParameter.ActualValue; 134 132 var evaluations = EvaluatedSolutionsParameter.ActualValue; 135 var realizations = RealizationsParameter.ActualValue;133 var probabilities = ProbabilitiesParameter.ActualValue; 136 134 if (localIterations == null) { 137 135 localIterations = new IntValue(0); … … 139 137 } 140 138 141 Improve(assignment, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken, realizations);139 Improve(assignment, distances, quality, localIterations, evaluations, maximization, maxIterations, probabilities, CancellationToken); 142 140 143 141 localIterations.Value = 0;
Note: See TracChangeset
for help on using the changeset viewer.