source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/BestVRPSolutionAnalyzer.cs @ 7175

Last change on this file since 7175 was 7175, checked in by svonolfe, 8 years ago

Adapted VRP analyzers to the new IAnalyzer interface (#1584)

File size: 9.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System.Linq;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30using HeuristicLab.Problems.VehicleRouting.Variants;
31using HeuristicLab.Common;
32
33namespace HeuristicLab.Problems.VehicleRouting {
34  /// <summary>
35  /// An operator for analyzing the best solution of Vehicle Routing Problems.
36  /// </summary>
37  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
38  [StorableClass]
39  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, IGeneralVRPOperator {
40    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
41      get { return (ILookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
42    }
43    public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
44      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
45    }
46
47    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
49    }
50    public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
51      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
52    }
53    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
54      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
55    }
56
57    public LookupParameter<VRPSolution> BestSolutionParameter {
58      get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
59    }
60    public LookupParameter<VRPSolution> BestValidSolutionParameter {
61      get { return (LookupParameter<VRPSolution>)Parameters["BestValidSolution"]; }
62    }
63    public ValueLookupParameter<ResultCollection> ResultsParameter {
64      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
65    }
66
67    public LookupParameter<DoubleValue> BestKnownQualityParameter {
68      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
69    }
70    public LookupParameter<VRPSolution> BestKnownSolutionParameter {
71      get { return (LookupParameter<VRPSolution>)Parameters["BestKnownSolution"]; }
72    }
73
74    public bool EnabledByDefault {
75      get { return true; }
76    }
77
78    [StorableConstructor]
79    private BestVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
80
81    public BestVRPSolutionAnalyzer()
82      : base() {
83        Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
84        Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
85
86        Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
87        Parameters.Add(new LookupParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
88
89        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
90        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
91        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
92
93        Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
94        Parameters.Add(new LookupParameter<VRPSolution>("BestValidSolution", "The best valid VRP solution."));
95        Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new BestVRPSolutionAnalyzer(this, cloner);
100    }
101
102    private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
103      : base(original, cloner) {
104    }
105
106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserializationHook() {
108      #region Backwards Compatibility
109      if (!Parameters.ContainsKey("BestKnownQuality")) {
110        Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
111      }
112      if (!Parameters.ContainsKey("BestKnownSolution")) {
113        Parameters.Add(new LookupParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
114      }
115      #endregion
116    }
117
118    public override IOperation Apply() {
119      IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
120      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
121      ResultCollection results = ResultsParameter.ActualValue;
122
123      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
124      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
125      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
126
127      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
128
129      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
130
131      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
132      VRPSolution solution = BestSolutionParameter.ActualValue;
133      if (solution == null) {
134        solution = new VRPSolution(problemInstance, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value));
135        BestSolutionParameter.ActualValue = solution;
136        results.Add(new Result("Best VRP Solution", solution));
137
138        results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
139        results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
140      } else {
141        if (qualities[i].Value <= solution.Quality.Value) {
142          solution.ProblemInstance = problemInstance;
143          solution.Solution = best.Clone() as IVRPEncoding;
144          solution.Quality.Value = qualities[i].Value;
145          (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
146          (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
147        }
148      }
149
150      VRPSolution validSolution = BestValidSolutionParameter.ActualValue;
151      if (validSolution == null) {
152        if (ProblemInstanceParameter.ActualValue.Feasible(best)) {
153          validSolution = new VRPSolution(problemInstance, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value));
154          BestValidSolutionParameter.ActualValue = validSolution;
155          if (results.ContainsKey("Best valid VRP Solution"))
156            results["Best valid VRP Solution"].Value = validSolution;
157          else
158            results.Add(new Result("Best valid VRP Solution", validSolution));
159
160          results.Add(new Result("Best valid VRP Solution Distance", new DoubleValue(distances[i].Value)));
161          results.Add(new Result("Best valid VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
162        }
163      } else {
164        if (qualities[i].Value <= validSolution.Quality.Value) {
165          if (ProblemInstanceParameter.ActualValue.Feasible(best)) {
166            validSolution.ProblemInstance = problemInstance;
167            validSolution.Solution = best.Clone() as IVRPEncoding;
168            validSolution.Quality.Value = qualities[i].Value;
169            (results["Best valid VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
170            (results["Best valid VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
171          }
172        }
173      }
174
175      if (bestKnownQuality == null ||
176          qualities[i].Value < bestKnownQuality.Value) {
177        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
178        BestKnownSolutionParameter.ActualValue = (VRPSolution)solution.Clone();
179      }
180
181      return base.Apply();
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.