1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HEAL.Attic;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
31 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
32 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.VehicleRouting {
|
---|
35 | /// <summary>
|
---|
36 | /// An operator for analyzing the best solution of Vehicle Routing Problems.
|
---|
37 | /// </summary>
|
---|
38 | [Item("BestTimeWindowedVRPSolutionAnalyzer", "An operator for analyzing the best solution of time windowed Vehicle Routing Problems.")]
|
---|
39 | [StorableType("2804825D-064C-4AF5-970B-3F17796579D9")]
|
---|
40 | public sealed class BestTimeWindowedVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ITimeWindowedOperator, ISingleObjectiveOperator {
|
---|
41 | public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
|
---|
42 | get { return (ILookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
|
---|
43 | }
|
---|
44 | public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
|
---|
45 | get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
|
---|
46 | }
|
---|
47 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
48 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
|
---|
52 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
|
---|
53 | }
|
---|
54 | public ScopeTreeLookupParameter<DoubleValue> TravelTimeParameter {
|
---|
55 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["TravelTime"]; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public LookupParameter<VRPSolution> BestSolutionParameter {
|
---|
59 | get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
|
---|
60 | }
|
---|
61 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
62 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public bool EnabledByDefault {
|
---|
66 | get { return true; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [StorableConstructor]
|
---|
70 | private BestTimeWindowedVRPSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
71 |
|
---|
72 | public BestTimeWindowedVRPSolutionAnalyzer()
|
---|
73 | : base() {
|
---|
74 | Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
|
---|
75 | Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
|
---|
76 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
|
---|
77 |
|
---|
78 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
|
---|
79 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
|
---|
80 |
|
---|
81 | Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
|
---|
82 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
86 | return new BestTimeWindowedVRPSolutionAnalyzer(this, cloner);
|
---|
87 | }
|
---|
88 |
|
---|
89 | private BestTimeWindowedVRPSolutionAnalyzer(BestTimeWindowedVRPSolutionAnalyzer original, Cloner cloner)
|
---|
90 | : base(original, cloner) {
|
---|
91 | }
|
---|
92 |
|
---|
93 | public override IOperation Apply() {
|
---|
94 | IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
|
---|
95 | ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
|
---|
96 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
97 |
|
---|
98 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
99 | ItemArray<DoubleValue> tardinesses = TardinessParameter.ActualValue;
|
---|
100 | ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
|
---|
101 |
|
---|
102 | int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
103 |
|
---|
104 | VRPSolution solution = BestSolutionParameter.ActualValue;
|
---|
105 | if (solution != null) {
|
---|
106 | if (!results.ContainsKey("Best VRP Solution Tardiness")) {
|
---|
107 | results.Add(new Result("Best VRP Solution Tardiness", new DoubleValue(tardinesses[i].Value)));
|
---|
108 | results.Add(new Result("Best VRP Solution TravelTime", new DoubleValue(travelTimes[i].Value)));
|
---|
109 | } else {
|
---|
110 | VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
|
---|
111 | if (qualities[i].Value <= eval.Quality) {
|
---|
112 | (results["Best VRP Solution Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
|
---|
113 | (results["Best VRP Solution TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | return base.Apply();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|