Changeset 17709 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution
- Timestamp:
- 08/03/20 11:35:39 (4 years ago)
- Location:
- branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution
- Files:
-
- 3 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/BestVRPSolutionAnalyzer.cs
r17698 r17709 21 21 22 22 using System.Linq; 23 using HEAL.Attic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab.Data;26 26 using HeuristicLab.Operators; 27 27 using HeuristicLab.Optimization; 28 28 using HeuristicLab.Parameters; 29 using HEAL.Attic;30 using HeuristicLab.Problems.VehicleRouting.Interfaces;31 29 using HeuristicLab.Problems.VehicleRouting.ProblemInstances; 32 using HeuristicLab.Problems.VehicleRouting.Variants;33 30 34 31 namespace HeuristicLab.Problems.VehicleRouting { … … 37 34 /// </summary> 38 35 [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")] 39 [StorableType("42DFB4D7-0DDA-424A-9DB3-1EDAB917A26B")] 40 public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, IGeneralVRPOperator, ISingleObjectiveOperator { 41 public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter { 42 get { return (ILookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; } 43 } 44 public ScopeTreeLookupParameter<IVRPEncodedSolution> VRPToursParameter { 45 get { return (ScopeTreeLookupParameter<IVRPEncodedSolution>)Parameters["VRPTours"]; } 46 } 36 [StorableType("3e1bb409-0b8f-4324-826c-2190aa5fb2b6")] 37 public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator { 47 38 48 public ScopeTreeLookupParameter<DoubleValue> QualityParameter { 49 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 50 } 51 public ScopeTreeLookupParameter<DoubleValue> DistanceParameter { 52 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; } 53 } 54 public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter { 55 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; } 56 } 39 [Storable] private IScopeTreeLookupParameter<VRPEvaluation> evaluationParameter; 40 public IScopeTreeLookupParameter<VRPEvaluation> EvaluationParameter => evaluationParameter; 57 41 58 public LookupParameter<VRPSolution> BestSolutionParameter { 59 get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; } 60 } 61 public LookupParameter<VRPSolution> BestValidSolutionParameter { 62 get { return (LookupParameter<VRPSolution>)Parameters["BestValidSolution"]; } 63 } 64 public ValueLookupParameter<ResultCollection> ResultsParameter { 65 get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; } 66 } 42 [Storable] private ILookupParameter<VRPSolution> bestSolutionParameter; 43 public ILookupParameter<VRPSolution> BestSolutionParameter => bestSolutionParameter; 67 44 68 public LookupParameter<DoubleValue> BestKnownQualityParameter { 69 get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 70 } 71 public LookupParameter<VRPSolution> BestKnownSolutionParameter { 72 get { return (LookupParameter<VRPSolution>)Parameters["BestKnownSolution"]; } 73 } 45 [Storable] private IResultParameter<VRPEvaluation> bestEvaluationParameter; 46 public IResultParameter<VRPEvaluation> BestSolutionEvaluationParameter => bestEvaluationParameter; 74 47 75 48 public bool EnabledByDefault { … … 79 52 [StorableConstructor] 80 53 private BestVRPSolutionAnalyzer(StorableConstructorFlag _) : base(_) { } 81 54 private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner) 55 : base(original, cloner) { 56 evaluationParameter = cloner.Clone(original.evaluationParameter); 57 bestSolutionParameter = cloner.Clone(original.bestSolutionParameter); 58 bestEvaluationParameter = cloner.Clone(original.bestEvaluationParameter); 59 } 82 60 public BestVRPSolutionAnalyzer() 83 61 : base() { 84 Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance.")); 85 Parameters.Add(new ScopeTreeLookupParameter<IVRPEncodedSolution>("VRPTours", "The VRP tours which should be evaluated.")); 86 87 Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance.")); 88 Parameters.Add(new LookupParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance.")); 89 90 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed.")); 91 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed.")); 92 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed.")); 93 94 Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution.")); 95 Parameters.Add(new LookupParameter<VRPSolution>("BestValidSolution", "The best valid VRP solution.")); 96 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored.")); 62 Parameters.Add(evaluationParameter = new ScopeTreeLookupParameter<VRPEvaluation>("EvaluationResult", "The qualities of the VRP solutions which should be analyzed.")); 63 Parameters.Add(bestSolutionParameter = new LookupParameter<VRPSolution>("BestSolution", "The best-so-far solution.")); 64 Parameters.Add(bestEvaluationParameter = new ResultParameter<VRPEvaluation>("Best VRP Evaluation", "The best VRP evaluation.", "Results")); 97 65 } 98 66 … … 101 69 } 102 70 103 private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner) 104 : base(original, cloner) { 105 } 71 public override IOperation Apply() { 72 var evaluations = EvaluationParameter.ActualValue; 106 73 107 [StorableHook(HookType.AfterDeserialization)] 108 private void AfterDeserialization() { 109 #region Backwards Compatibility 110 if (!Parameters.ContainsKey("BestKnownQuality")) { 111 Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance.")); 112 } 113 if (!Parameters.ContainsKey("BestKnownSolution")) { 114 Parameters.Add(new LookupParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance.")); 115 } 116 #endregion 117 } 74 int i = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First().index; 118 75 119 public override IOperation Apply() { 120 IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue; 121 ItemArray<IVRPEncodedSolution> solutions = VRPToursParameter.ActualValue; 122 ResultCollection results = ResultsParameter.ActualValue; 123 124 ItemArray<DoubleValue> qualities = QualityParameter.ActualValue; 125 ItemArray<DoubleValue> distances = DistanceParameter.ActualValue; 126 ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue; 127 128 int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; 129 IVRPEncodedSolution best = solutions[i].Clone() as IVRPEncodedSolution; 130 VRPSolution solution = BestSolutionParameter.ActualValue; 131 if (solution == null) { 132 solution = new VRPSolution(problemInstance, best.Clone() as IVRPEncodedSolution, new DoubleValue(qualities[i].Value)); 133 BestSolutionParameter.ActualValue = solution; 134 results.Add(new Result("Best VRP Solution", solution)); 135 136 results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value))); 137 results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value))); 138 } else { 139 VRPEvaluation eval = problemInstance.Evaluate(solution.Solution); 140 if (qualities[i].Value <= eval.Quality) { 141 solution.ProblemInstance = problemInstance; 142 solution.Solution = best.Clone() as IVRPEncodedSolution; 143 solution.Quality.Value = qualities[i].Value; 144 (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value; 145 (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value; 146 } 147 } 148 149 var idx = qualities.Select((x, index) => new { index, x.Value }).Where(index => problemInstance.Feasible(solutions[index.index])).OrderBy(x => x.Value).FirstOrDefault(); 150 if (idx != null) { 151 int j = idx.index; 152 IVRPEncodedSolution bestFeasible = solutions[j].Clone() as IVRPEncodedSolution; 153 VRPSolution validSolution = BestValidSolutionParameter.ActualValue; 154 if (validSolution == null) { 155 validSolution = new VRPSolution(problemInstance, best.Clone() as IVRPEncodedSolution, new DoubleValue(qualities[j].Value)); 156 BestValidSolutionParameter.ActualValue = validSolution; 157 if (results.ContainsKey("Best valid VRP Solution")) 158 results["Best valid VRP Solution"].Value = validSolution; 159 else 160 results.Add(new Result("Best valid VRP Solution", validSolution)); 161 162 results.Add(new Result("Best valid VRP Solution Distance", new DoubleValue(distances[j].Value))); 163 results.Add(new Result("Best valid VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[j].Value))); 164 } else { 165 if (qualities[j].Value <= validSolution.Quality.Value) { 166 if (ProblemInstanceParameter.ActualValue.Feasible(best)) { 167 validSolution.ProblemInstance = problemInstance; 168 validSolution.Solution = best.Clone() as IVRPEncodedSolution; 169 validSolution.Quality.Value = qualities[j].Value; 170 (results["Best valid VRP Solution Distance"].Value as DoubleValue).Value = distances[j].Value; 171 (results["Best valid VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[j].Value; 172 } 173 } 174 } 175 176 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 177 if (bestKnownQuality == null || qualities[j].Value < bestKnownQuality.Value) { 178 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[j].Value); 179 BestKnownSolutionParameter.ActualValue = (VRPSolution)validSolution.Clone(); 180 } 76 var bestEvaluation = BestSolutionEvaluationParameter.ActualValue; 77 78 var bestSolution = BestSolutionParameter.ActualValue; 79 if (bestSolution == null || evaluations[i].Quality <= bestSolution.Quality.Value) { 80 BestSolutionEvaluationParameter.ActualValue = (VRPEvaluation)evaluations[i].Clone(); 181 81 } 182 82
Note: See TracChangeset
for help on using the changeset viewer.