Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Analyzers/BestVRPSolutionAnalyzer.cs @ 5405

Last change on this file since 5405 was 4852, checked in by svonolfe, 14 years ago

partly reverted r4851 (#1236)

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