Free cookie consent management tool by TermsFeed Policy Generator

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

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

Improved VRP visualization (#1039)

File size: 9.2 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;
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 {
37    public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
38      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
39    }
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    }
46    public LookupParameter<DoubleMatrix> CoordinatesParameter {
47      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
48    }
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    }
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
83    [StorableConstructor]
84    private BestVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
85
86    public BestVRPSolutionAnalyzer()
87      : base() {
88        Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
89      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
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
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;
108      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
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),
124          new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value),
125          DistanceMatrixParameter.ActualValue, UseDistanceMatrixParameter.ActualValue,
126          ReadyTimeParameter.ActualValue, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue);
127        BestSolutionParameter.ActualValue = solution;
128        results.Add(new Result("Best VRP Solution", solution));
129
130        results.Add(new Result("Best TravelTime", new DoubleValue(travelTimes[i].Value)));
131        results.Add(new Result("Best Distance",  new DoubleValue(distances[i].Value)));
132        results.Add(new Result("Best VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
133        results.Add(new Result("Best Overload",  new DoubleValue(overloads[i].Value)));
134        results.Add(new Result("Best Tardiness",  new DoubleValue(tardinesses[i].Value)));
135      } else {
136        if (solution.Quality.Value > qualities[i].Value) {
137          solution.Coordinates = coordinates;
138          solution.Solution = best.Clone() as IVRPEncoding;
139          solution.Quality.Value = qualities[i].Value;
140          solution.Distance.Value = (results["Best Distance"].Value as DoubleValue).Value = distances[i].Value;
141          solution.Overload.Value = (results["Best Overload"].Value as DoubleValue).Value = overloads[i].Value;
142          solution.Tardiness.Value = (results["Best Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
143          solution.TravelTime.Value = (results["Best TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
144          solution.VehicleUtilization.Value = (results["Best VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
145          solution.DistanceMatrix = DistanceMatrixParameter.ActualValue;
146          solution.UseDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
147          solution.ReadyTime = ReadyTimeParameter.ActualValue;
148          solution.DueTime = DueTimeParameter.ActualValue;
149          solution.ServiceTime = ServiceTimeParameter.ActualValue;
150        }
151      }
152
153      return base.Apply();
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.