Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Problems.VehicleRouting/3.3/Analyzers/BestVRPSolutionAnalyzer.cs @ 12448

Last change on this file since 12448 was 7383, checked in by ascheibe, 13 years ago

#1745 merged trunk changes into branch

File size: 11.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.VehicleRouting {
32  /// <summary>
33  /// An operator for analyzing the best solution of Vehicle Routing Problems.
34  /// </summary>
35  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
36  [StorableClass]
37  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
38    public bool EnabledByDefault {
39      get { return true; }
40    }
41
42    public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
43      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
44    }
45    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
46      get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
47    }
48    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
49      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
50    }
51    public LookupParameter<DoubleMatrix> CoordinatesParameter {
52      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
53    }
54    public ILookupParameter<DoubleArray> ReadyTimeParameter {
55      get { return (ILookupParameter<DoubleArray>)Parameters["ReadyTime"]; }
56    }
57    public ILookupParameter<DoubleArray> DueTimeParameter {
58      get { return (ILookupParameter<DoubleArray>)Parameters["DueTime"]; }
59    }
60    public ILookupParameter<DoubleArray> ServiceTimeParameter {
61      get { return (ILookupParameter<DoubleArray>)Parameters["ServiceTime"]; }
62    }
63    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
64      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
65    }
66    public ScopeTreeLookupParameter<DoubleValue> OverloadParameter {
67      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Overload"]; }
68    }
69    public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
70      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
71    }
72    public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
73      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
74    }
75    public ScopeTreeLookupParameter<DoubleValue> TravelTimeParameter {
76      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["TravelTime"]; }
77    }
78    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
79      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
80    }
81    public LookupParameter<VRPSolution> BestSolutionParameter {
82      get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
83    }
84    public ValueLookupParameter<ResultCollection> ResultsParameter {
85      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
86    }
87    public LookupParameter<DoubleValue> BestKnownQualityParameter {
88      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
89    }
90    public LookupParameter<IVRPEncoding> BestKnownSolutionParameter {
91      get { return (LookupParameter<IVRPEncoding>)Parameters["BestKnownSolution"]; }
92    }
93
94    [StorableConstructor]
95    private BestVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
96    private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
97      : base(original, cloner) {
98    }
99    public BestVRPSolutionAnalyzer()
100      : base() {
101      Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
102      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
103      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
104      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
105      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
106      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
107      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
108
109      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
110      Parameters.Add(new LookupParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
111
112      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
113      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
114      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
115      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
116      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
117      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
118      Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
119      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      return new BestVRPSolutionAnalyzer(this, cloner);
124    }
125
126    [StorableHook(HookType.AfterDeserialization)]
127    private void AfterDeserialization() {
128      // BackwardsCompatibility3.3
129      #region Backwards compatible code, remove with 3.4
130      if (!Parameters.ContainsKey("BestKnownQuality")) {
131        Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
132      }
133      if (!Parameters.ContainsKey("BestKnownSolution")) {
134        Parameters.Add(new LookupParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
135      }
136      #endregion
137    }
138
139    public override IOperation Apply() {
140      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
141      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
142      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
143      ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
144      ItemArray<DoubleValue> tardinesses = TardinessParameter.ActualValue;
145      ResultCollection results = ResultsParameter.ActualValue;
146      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
147      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
148      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
149
150      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
151
152      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
153
154      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
155      VRPSolution solution = BestSolutionParameter.ActualValue;
156      if (solution == null) {
157        solution = new VRPSolution(coordinates, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value),
158          new DoubleValue(distances[i].Value), new DoubleValue(overloads[i].Value), new DoubleValue(tardinesses[i].Value),
159          new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value),
160          DistanceMatrixParameter.ActualValue, UseDistanceMatrixParameter.ActualValue,
161          ReadyTimeParameter.ActualValue, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue);
162        BestSolutionParameter.ActualValue = solution;
163        results.Add(new Result("Best VRP Solution", solution));
164
165        results.Add(new Result("Best VRP Solution TravelTime", new DoubleValue(travelTimes[i].Value)));
166        results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
167        results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
168        results.Add(new Result("Best VRP Solution Overload", new DoubleValue(overloads[i].Value)));
169        results.Add(new Result("Best VRP Solution Tardiness", new DoubleValue(tardinesses[i].Value)));
170
171      } else {
172        if (qualities[i].Value <= solution.Quality.Value) {
173          solution.Coordinates = coordinates;
174          solution.Solution = best.Clone() as IVRPEncoding;
175          solution.Quality.Value = qualities[i].Value;
176          solution.Distance.Value = (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
177          solution.Overload.Value = (results["Best VRP Solution Overload"].Value as DoubleValue).Value = overloads[i].Value;
178          solution.Tardiness.Value = (results["Best VRP Solution Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
179          solution.TravelTime.Value = (results["Best VRP Solution TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
180          solution.VehicleUtilization.Value = (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
181          solution.DistanceMatrix = DistanceMatrixParameter.ActualValue;
182          solution.UseDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
183          solution.ReadyTime = ReadyTimeParameter.ActualValue;
184          solution.DueTime = DueTimeParameter.ActualValue;
185          solution.ServiceTime = ServiceTimeParameter.ActualValue;
186        }
187      }
188
189      if (bestKnownQuality == null ||
190          qualities[i].Value < bestKnownQuality.Value) {
191        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
192        BestKnownSolutionParameter.ActualValue = (IVRPEncoding)best.Clone();
193      }
194
195      return base.Apply();
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.