Changeset 17709
- Timestamp:
- 08/03/20 11:35:39 (4 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 12 added
- 3 deleted
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Core/3.3/Interfaces/IParameterizedItem.cs
r17226 r17709 30 30 public interface IParameterizedItem : IItem { 31 31 IKeyedItemCollection<string, IParameter> Parameters { get; } 32 32 // TODO: Move the following to separate interface, not only paramterized items 33 // could be collected for run. Mabe ICollectible 33 34 void CollectParameterValues(IDictionary<string, IItem> values); 34 35 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/EvaluationResult.cs
r17381 r17709 35 35 [Storable] 36 36 private readonly Dictionary<string, object> data = new Dictionary<string, object>(); 37 public IEnumerable<KeyValuePair<string, object>> AdditionalData => data.AsEnumerable(); 37 38 38 39 protected EvaluationResult() : base() { } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEvaluationResult.cs
r17459 r17709 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HEAL.Attic; 23 24 using HeuristicLab.Core; … … 28 29 29 30 //TODO: make methods generic for get/set additional data 31 IEnumerable<KeyValuePair<string, object>> AdditionalData { get; } 30 32 void SetAdditionalData(string identifier, object o); 31 33 object GetAdditionalData(string identifier); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs
r17699 r17709 86 86 scope.Variables.Add(new Variable(EvaluationResultName, solutionContext.EvaluationResult)); 87 87 } else variable2.Value = solutionContext.EvaluationResult; 88 if (solutionContext.EvaluationResult != null) { 89 foreach (var item in solutionContext.EvaluationResult.AdditionalData) { 90 if (item.Value is IItem i) { 91 if (!scope.Variables.TryGetValue(item.Key, out var variable)) 92 scope.Variables.Add(new Variable(item.Key, i)); 93 else variable.Value = i; 94 } 95 } 96 } 88 97 } 89 98 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs
r17699 r17709 69 69 ScopeUtil.CopyToScope(ExecutionContext.Scope, solutionContext); 70 70 QualityParameter.ActualValue = new DoubleValue(solutionContext.EvaluationResult.Quality); 71 72 71 return base.InstrumentedApply(); 73 72 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting.Views/3.4/HeuristicLab.Problems.VehicleRouting.Views-3.4.csproj
r16723 r17709 119 119 </ItemGroup> 120 120 <ItemGroup> 121 <Compile Include="CVRPPDTWEvaluationView.cs"> 122 <SubType>UserControl</SubType> 123 </Compile> 124 <Compile Include="CVRPPDTWEvaluationView.Designer.cs"> 125 <DependentUpon>CVRPPDTWEvaluationView.cs</DependentUpon> 126 </Compile> 127 <Compile Include="CVRPTWEvaluationView.cs"> 128 <SubType>UserControl</SubType> 129 </Compile> 130 <Compile Include="CVRPTWEvaluationView.Designer.cs"> 131 <DependentUpon>CVRPTWEvaluationView.cs</DependentUpon> 132 </Compile> 133 <Compile Include="CVRPEvaluationView.cs"> 134 <SubType>UserControl</SubType> 135 </Compile> 136 <Compile Include="CVRPEvaluationView.Designer.cs"> 137 <DependentUpon>CVRPEvaluationView.cs</DependentUpon> 138 </Compile> 121 139 <Compile Include="Properties\AssemblyInfo.cs" /> 122 140 <Compile Include="MDCVRPPDTWView.cs"> … … 156 174 <Compile Include="SingleDepotVRPView.Designer.cs"> 157 175 <DependentUpon>SingleDepotVRPView.cs</DependentUpon> 176 </Compile> 177 <Compile Include="VRPEvaluationView.cs"> 178 <SubType>UserControl</SubType> 179 </Compile> 180 <Compile Include="VRPEvaluationView.Designer.cs"> 181 <DependentUpon>VRPEvaluationView.cs</DependentUpon> 158 182 </Compile> 159 183 <Compile Include="VRPImportDialog.cs"> … … 297 321 <Private>False</Private> 298 322 </Reference> 323 </ItemGroup> 324 <ItemGroup> 325 <EmbeddedResource Include="CVRPEvaluationView.resx"> 326 <DependentUpon>CVRPEvaluationView.cs</DependentUpon> 327 </EmbeddedResource> 328 <EmbeddedResource Include="CVRPPDTWEvaluationView.resx"> 329 <DependentUpon>CVRPPDTWEvaluationView.cs</DependentUpon> 330 </EmbeddedResource> 331 <EmbeddedResource Include="CVRPTWEvaluationView.resx"> 332 <DependentUpon>CVRPTWEvaluationView.cs</DependentUpon> 333 </EmbeddedResource> 334 <EmbeddedResource Include="VRPEvaluationView.resx"> 335 <DependentUpon>VRPEvaluationView.cs</DependentUpon> 336 </EmbeddedResource> 299 337 </ItemGroup> 300 338 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
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 -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/HeuristicLab.Problems.VehicleRouting-3.4.csproj
r17708 r17709 128 128 <Compile Include="Analyzer\BestAverageWorstTours\TimeWindowed\BestTimeWindowedVRPToursMemorizer.cs" /> 129 129 <Compile Include="Analyzer\BestSolution\BestVRPSolutionAnalyzer.cs" /> 130 <Compile Include="Analyzer\BestSolution\Capacitated\BestCapacitatedVRPSolutionAnalyzer.cs" />131 <Compile Include="Analyzer\BestSolution\PickupAndDelivery\BestPickupAndDeliveryVRPSolutionAnalyzer.cs" />132 <Compile Include="Analyzer\BestSolution\TimeWindowed\BestTimeWindowedVRPSolutionAnalyzer.cs" />133 130 <Compile Include="Analyzer\ConstraintRelaxation\Capacitated\CapacityRelaxationVRPAnalyzer.cs" /> 134 131 <Compile Include="Analyzer\ConstraintRelaxation\PickupAndDelivery\PickupViolationsRelaxationVRPAnalyzer.cs" /> … … 490 487 </Reference> 491 488 </ItemGroup> 489 <ItemGroup /> 492 490 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 493 491 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPEvaluation.cs
r17708 r17709 124 124 [Item("VRPEvaluation", "")] 125 125 [StorableType("0c4dfa78-8e41-4558-b2dd-4a22954c35ba")] 126 public class VRPEvaluation : EvaluationResult { 126 public class VRPEvaluation : EvaluationResult, ISingleObjectiveEvaluationResult { 127 // TODO: Would be nice to collect these, into individual results in a run 127 128 [Storable] public double Quality { get; set; } 128 129 [Storable] public double Distance { get; set; } … … 130 131 [Storable] public InsertionInfo InsertionInfo { get; set; } 131 132 [Storable] public double Penalty { get; set; } 132 133 [Storable] public bool IsFeasible { get; set; } 133 134 134 135 [StorableConstructor] … … 141 142 InsertionInfo = cloner.Clone(original.InsertionInfo); 142 143 Penalty = original.Penalty; 144 IsFeasible = original.IsFeasible; 143 145 } 144 146 public VRPEvaluation() : base() { -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPEvaluator.cs
r17698 r17709 20 20 #endregion 21 21 22 using HEAL.Attic; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Data; 25 26 using HeuristicLab.Parameters; 26 using HEAL.Attic;27 27 using HeuristicLab.Problems.VehicleRouting.Encodings; 28 28 using HeuristicLab.Problems.VehicleRouting.Interfaces; … … 124 124 EvaluateTour(evaluation, instance, tour, solution); 125 125 } 126 126 evaluation.IsFeasible = Feasible(evaluation); 127 127 return evaluation; 128 128 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs
r17706 r17709 97 97 98 98 public override ISingleObjectiveEvaluationResult Evaluate(IVRPEncodedSolution solution, IRandom random, CancellationToken cancellationToken) { 99 return new SingleObjectiveEvaluationResult(ProblemInstance.Evaluate(solution).Quality);99 return ProblemInstance.Evaluate(solution); 100 100 } 101 101 … … 150 150 151 151 void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) { 152 //InitializeOperators();153 AttachProblemInstanceEventHandlers(); 154 155 //OnOperatorsChanged();152 InitializeOperators(); 153 AttachProblemInstanceEventHandlers(); 154 155 OnOperatorsChanged(); 156 156 } 157 157 … … 169 169 Operators.Add(new QualitySimilarityCalculator()); 170 170 Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>())); 171 Operators.AddRange(ProblemInstance.Operators.OfType<IAnalyzer>()); 171 172 } 172 173
Note: See TracChangeset
for help on using the changeset viewer.