Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Problems.VehicleRouting/3.3/Analyzers/BestVRPSolutionAnalyzer.cs @ 7317

Last change on this file since 7317 was 7270, checked in by spimming, 13 years ago

#1680:

  • merged changes from trunk into branch
File size: 11.1 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 AfterDeserializationHook() {
128      #region Backwards Compatibility
129      if (!Parameters.ContainsKey("BestKnownQuality")) {
130        Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
131      }
132      if (!Parameters.ContainsKey("BestKnownSolution")) {
133        Parameters.Add(new LookupParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
134      }
135      #endregion
136    }
137
138    public override IOperation Apply() {
139      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
140      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
141      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
142      ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
143      ItemArray<DoubleValue> tardinesses = TardinessParameter.ActualValue;
144      ResultCollection results = ResultsParameter.ActualValue;
145      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
146      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
147      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
148
149      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
150
151      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
152
153      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
154      VRPSolution solution = BestSolutionParameter.ActualValue;
155      if (solution == null) {
156        solution = new VRPSolution(coordinates, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value),
157          new DoubleValue(distances[i].Value), new DoubleValue(overloads[i].Value), new DoubleValue(tardinesses[i].Value),
158          new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value),
159          DistanceMatrixParameter.ActualValue, UseDistanceMatrixParameter.ActualValue,
160          ReadyTimeParameter.ActualValue, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue);
161        BestSolutionParameter.ActualValue = solution;
162        results.Add(new Result("Best VRP Solution", solution));
163
164        results.Add(new Result("Best VRP Solution TravelTime", new DoubleValue(travelTimes[i].Value)));
165        results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
166        results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
167        results.Add(new Result("Best VRP Solution Overload", new DoubleValue(overloads[i].Value)));
168        results.Add(new Result("Best VRP Solution Tardiness", new DoubleValue(tardinesses[i].Value)));
169
170      } else {
171        if (qualities[i].Value <= solution.Quality.Value) {
172          solution.Coordinates = coordinates;
173          solution.Solution = best.Clone() as IVRPEncoding;
174          solution.Quality.Value = qualities[i].Value;
175          solution.Distance.Value = (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
176          solution.Overload.Value = (results["Best VRP Solution Overload"].Value as DoubleValue).Value = overloads[i].Value;
177          solution.Tardiness.Value = (results["Best VRP Solution Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
178          solution.TravelTime.Value = (results["Best VRP Solution TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
179          solution.VehicleUtilization.Value = (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
180          solution.DistanceMatrix = DistanceMatrixParameter.ActualValue;
181          solution.UseDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
182          solution.ReadyTime = ReadyTimeParameter.ActualValue;
183          solution.DueTime = DueTimeParameter.ActualValue;
184          solution.ServiceTime = ServiceTimeParameter.ActualValue;
185        }
186      }
187
188      if (bestKnownQuality == null ||
189          qualities[i].Value < bestKnownQuality.Value) {
190        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
191        BestKnownSolutionParameter.ActualValue = (IVRPEncoding)best.Clone();
192      }
193
194      return base.Apply();
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.