1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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("BestCapacitatedVRPSolutionAnalyzer", "An operator for analyzing the best solution of capacitated Vehicle Routing Problems.")]
|
---|
39 | [StorableType("5E50BE52-E801-4F84-8070-298368FEC208")]
|
---|
40 | public sealed class BestCapacitatedVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ICapacitatedOperator, 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> OverloadParameter {
|
---|
52 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Overload"]; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public LookupParameter<VRPSolution> BestSolutionParameter {
|
---|
56 | get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
|
---|
57 | }
|
---|
58 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
59 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public bool EnabledByDefault {
|
---|
63 | get { return true; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | [StorableConstructor]
|
---|
67 | private BestCapacitatedVRPSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
68 |
|
---|
69 | public BestCapacitatedVRPSolutionAnalyzer()
|
---|
70 | : base() {
|
---|
71 | Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
|
---|
72 | Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
|
---|
73 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
|
---|
74 |
|
---|
75 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
|
---|
76 |
|
---|
77 | Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
|
---|
78 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
82 | return new BestCapacitatedVRPSolutionAnalyzer(this, cloner);
|
---|
83 | }
|
---|
84 |
|
---|
85 | private BestCapacitatedVRPSolutionAnalyzer(BestCapacitatedVRPSolutionAnalyzer original, Cloner cloner)
|
---|
86 | : base(original, cloner) {
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override IOperation Apply() {
|
---|
90 | IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
|
---|
91 | ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
|
---|
92 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
93 |
|
---|
94 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
95 | ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
|
---|
96 |
|
---|
97 | int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
98 |
|
---|
99 | VRPSolution solution = BestSolutionParameter.ActualValue;
|
---|
100 | if (solution != null) {
|
---|
101 | if (!results.ContainsKey("Best VRP Solution Overload")) {
|
---|
102 | results.Add(new Result("Best VRP Solution Overload", new DoubleValue(overloads[i].Value)));
|
---|
103 | } else {
|
---|
104 | VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
|
---|
105 | if (qualities[i].Value <= eval.Quality) {
|
---|
106 | (results["Best VRP Solution Overload"].Value as DoubleValue).Value = overloads[i].Value;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return base.Apply();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|