Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged r4351 of the VRP feature branch into trunk (#1039)

File size: 9.3 KB
RevLine 
[3938]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;
29
30namespace HeuristicLab.Problems.VehicleRouting {
31  /// <summary>
32  /// An operator for analyzing the best solution of Vehicle Routing Problems.
33  /// </summary>
34  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
35  [StorableClass]
36  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
[4179]37    public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
38      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
[3938]39    }
[4015]40    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
41      get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
42    }
43    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
44      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
45    }
[3938]46    public LookupParameter<DoubleMatrix> CoordinatesParameter {
47      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
48    }
[4015]49    public ILookupParameter<DoubleArray> ReadyTimeParameter {
50      get { return (ILookupParameter<DoubleArray>)Parameters["ReadyTime"]; }
51    }
52    public ILookupParameter<DoubleArray> DueTimeParameter {
53      get { return (ILookupParameter<DoubleArray>)Parameters["DueTime"]; }
54    }
55    public ILookupParameter<DoubleArray> ServiceTimeParameter {
56      get { return (ILookupParameter<DoubleArray>)Parameters["ServiceTime"]; }
57    }
[3938]58    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
59      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
60    }
61    public ScopeTreeLookupParameter<DoubleValue> OverloadParameter {
62      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Overload"]; }
63    }
64    public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
65      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
66    }
67    public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
68      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
69    }
70    public ScopeTreeLookupParameter<DoubleValue> TravelTimeParameter {
71      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["TravelTime"]; }
72    }
73    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
74      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
75    }
76    public LookupParameter<VRPSolution> BestSolutionParameter {
77      get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
78    }
79    public ValueLookupParameter<ResultCollection> ResultsParameter {
80      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
81    }
82
[4179]83    [StorableConstructor]
84    private BestVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
85
[3938]86    public BestVRPSolutionAnalyzer()
87      : base() {
[4179]88        Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
[3938]89      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
[4015]90      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
91      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
92      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
93      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
94      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
95
[3938]96      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
97      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
98      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
99      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
100      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
101      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
102      Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best TSP solution."));
103      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
104    }
105
106    public override IOperation Apply() {
107      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
[4179]108      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
[3938]109      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
110      ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
111      ItemArray<DoubleValue> tardinesses = TardinessParameter.ActualValue;
112      ResultCollection results = ResultsParameter.ActualValue;
113      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
114      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
115      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
116
117      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
118
119      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
120      VRPSolution solution = BestSolutionParameter.ActualValue;
121      if (solution == null) {
122        solution = new VRPSolution(coordinates, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value),
123          new DoubleValue(distances[i].Value), new DoubleValue(overloads[i].Value), new DoubleValue(tardinesses[i].Value),
[4068]124          new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value),
125          DistanceMatrixParameter.ActualValue, UseDistanceMatrixParameter.ActualValue,
[4015]126          ReadyTimeParameter.ActualValue, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue);
[3938]127        BestSolutionParameter.ActualValue = solution;
128        results.Add(new Result("Best VRP Solution", solution));
[4185]129
[4352]130        results.Add(new Result("Best VRP Solution TravelTime", new DoubleValue(travelTimes[i].Value)));
131        results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
132        results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
133        results.Add(new Result("Best VRP Solution Overload", new DoubleValue(overloads[i].Value)));
134        results.Add(new Result("Best VRP Solution Tardiness", new DoubleValue(tardinesses[i].Value)));
135
[3938]136      } else {
[4352]137        if (qualities[i].Value <= solution.Quality.Value) {
[3938]138          solution.Coordinates = coordinates;
139          solution.Solution = best.Clone() as IVRPEncoding;
140          solution.Quality.Value = qualities[i].Value;
[4352]141          solution.Distance.Value = (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
142          solution.Overload.Value = (results["Best VRP Solution Overload"].Value as DoubleValue).Value = overloads[i].Value;
143          solution.Tardiness.Value = (results["Best VRP Solution Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
144          solution.TravelTime.Value = (results["Best VRP Solution TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
145          solution.VehicleUtilization.Value = (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
[4015]146          solution.DistanceMatrix = DistanceMatrixParameter.ActualValue;
147          solution.UseDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
148          solution.ReadyTime = ReadyTimeParameter.ActualValue;
149          solution.DueTime = DueTimeParameter.ActualValue;
150          solution.ServiceTime = ServiceTimeParameter.ActualValue;
[3938]151        }
152      }
153
154      return base.Apply();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.