Changeset 13470 for branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Improvers/PTSPAnalyticalTwoPointFiveLocalImprovement.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/PTSPAnalyticalTwoPointFiveLocalImprovement.cs
r13451 r13470 38 38 /// The operator tries to improve the probabilistic traveling salesman solution by swapping two randomly chosen edges for a certain number of times. 39 39 /// </remarks> 40 [Item("PTSP ExhaustiveInversionLocalImprovement", "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 2.5 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 ExhaustiveInversionLocalImprovement : SingleSuccessorOperator, IEstimatedPTSPOperator, ILocalImprovementOperator {42 public sealed class PTSPAnalyticalTwoPointFiveLocalImprovement : 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 ExhaustiveInversionLocalImprovement(bool deserializing) : base(deserializing) { }82 private PTSP ExhaustiveInversionLocalImprovement(PTSPExhaustiveInversionLocalImprovement original, Cloner cloner) : base(original, cloner) { }83 public PTSP ExhaustiveInversionLocalImprovement()81 private PTSPAnalyticalTwoPointFiveLocalImprovement(bool deserializing) : base(deserializing) { } 82 private PTSPAnalyticalTwoPointFiveLocalImprovement(PTSPAnalyticalTwoPointFiveLocalImprovement original, Cloner cloner) : base(original, cloner) { } 83 public PTSPAnalyticalTwoPointFiveLocalImprovement() 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 ExhaustiveInversionLocalImprovement(this, cloner);97 return new PTSPAnalyticalTwoPointFiveLocalImprovement(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 InversionMove bestMove = null;104 double bestQuality = 0; // we have to make an improvement, so 0is the baseline105 doubleevaluations = 0.0;106 foreach (var move in Exhaustive InversionMoveGenerator.Generate(assignment)) {107 double moveQuality = PTSPEstimatedInversionMoveEvaluator.EvaluateByDistanceMatrix(assignment, move, distanceM, realizations);108 evaluations += 2 * (move.Index2 - move.Index1 + 1) / (double)assignment.Length;104 TwoPointFiveMove bestMove = null; 105 var bestQuality = quality.Value; // we have to make an improvement, so current quality is the baseline 106 var evaluations = 0.0; 107 foreach (var move in ExhaustiveTwoPointFiveMoveGenerator.Generate(assignment)) { 108 var moveQuality = PTSPAnalyticalTwoPointFiveMoveEvaluator.EvaluateMove(assignment, move, distance, probabilities); 109 evaluations++; 109 110 if (maximization && moveQuality > bestQuality 110 111 || !maximization && moveQuality < bestQuality) { … … 115 116 evaluatedSolutions.Value += (int)Math.Ceiling(evaluations); 116 117 if (bestMove == null) break; 117 InversionManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);118 quality.Value += bestQuality;118 TwoPointFiveMoveMaker.Apply(assignment, bestMove); 119 quality.Value = bestQuality; 119 120 localIterations.Value++; 120 121 cancellation.ThrowIfCancellationRequested(); … … 130 131 var localIterations = LocalIterationsParameter.ActualValue; 131 132 var evaluations = EvaluatedSolutionsParameter.ActualValue; 132 var realizations = RealizationsParameter.ActualValue;133 var probabilities = ProbabilitiesParameter.ActualValue; 133 134 if (localIterations == null) { 134 135 localIterations = new IntValue(0); … … 136 137 } 137 138 138 Improve(assignment, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken, realizations);139 Improve(assignment, distances, quality, localIterations, evaluations, maximization, maxIterations, probabilities, CancellationToken); 139 140 140 141 localIterations.Value = 0;
Note: See TracChangeset
for help on using the changeset viewer.