Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.VehicleRouting/3.3/Analyzers/BestVRPSolutionAnalyzer.cs @ 12858

Last change on this file since 12858 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 9.6 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
84    [StorableConstructor]
85    private BestVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
86    private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
87      : base(original, cloner) {
88    }
89    public BestVRPSolutionAnalyzer()
90      : base() {
91        Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
92      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
93      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
94      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
95      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
96      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
97      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
98
99      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
100      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
101      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
102      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
103      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
104      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
105      Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
106      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
107    }
108
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new BestVRPSolutionAnalyzer(this, cloner);
111    }
112
113    public override IOperation Apply() {
114      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
115      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
116      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
117      ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
118      ItemArray<DoubleValue> tardinesses = TardinessParameter.ActualValue;
119      ResultCollection results = ResultsParameter.ActualValue;
120      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
121      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
122      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
123
124      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
125
126      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
127      VRPSolution solution = BestSolutionParameter.ActualValue;
128      if (solution == null) {
129        solution = new VRPSolution(coordinates, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value),
130          new DoubleValue(distances[i].Value), new DoubleValue(overloads[i].Value), new DoubleValue(tardinesses[i].Value),
131          new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value),
132          DistanceMatrixParameter.ActualValue, UseDistanceMatrixParameter.ActualValue,
133          ReadyTimeParameter.ActualValue, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue);
134        BestSolutionParameter.ActualValue = solution;
135        results.Add(new Result("Best VRP Solution", solution));
136
137        results.Add(new Result("Best VRP Solution TravelTime", new DoubleValue(travelTimes[i].Value)));
138        results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
139        results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
140        results.Add(new Result("Best VRP Solution Overload", new DoubleValue(overloads[i].Value)));
141        results.Add(new Result("Best VRP Solution Tardiness", new DoubleValue(tardinesses[i].Value)));
142
143      } else {
144        if (qualities[i].Value <= solution.Quality.Value) {
145          solution.Coordinates = coordinates;
146          solution.Solution = best.Clone() as IVRPEncoding;
147          solution.Quality.Value = qualities[i].Value;
148          solution.Distance.Value = (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
149          solution.Overload.Value = (results["Best VRP Solution Overload"].Value as DoubleValue).Value = overloads[i].Value;
150          solution.Tardiness.Value = (results["Best VRP Solution Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
151          solution.TravelTime.Value = (results["Best VRP Solution TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
152          solution.VehicleUtilization.Value = (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
153          solution.DistanceMatrix = DistanceMatrixParameter.ActualValue;
154          solution.UseDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
155          solution.ReadyTime = ReadyTimeParameter.ActualValue;
156          solution.DueTime = DueTimeParameter.ActualValue;
157          solution.ServiceTime = ServiceTimeParameter.ActualValue;
158        }
159      }
160
161      return base.Apply();
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.