[3938] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3938] | 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 |
|
---|
| 22 | using System.Linq;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[3938] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace 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 {
|
---|
[4179] | 38 | public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
|
---|
| 39 | get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
|
---|
[3938] | 40 | }
|
---|
[4015] | 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 | }
|
---|
[3938] | 47 | public LookupParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 48 | get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
| 49 | }
|
---|
[4015] | 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 | }
|
---|
[3938] | 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 | }
|
---|
[4851] | 83 | public LookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 84 | get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 85 | }
|
---|
[4852] | 86 | public LookupParameter<IVRPEncoding> BestKnownSolutionParameter {
|
---|
| 87 | get { return (LookupParameter<IVRPEncoding>)Parameters["BestKnownSolution"]; }
|
---|
[4851] | 88 | }
|
---|
[3938] | 89 |
|
---|
[4179] | 90 | [StorableConstructor]
|
---|
| 91 | private BestVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 92 | private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
|
---|
| 93 | : base(original, cloner) {
|
---|
| 94 | }
|
---|
[3938] | 95 | public BestVRPSolutionAnalyzer()
|
---|
| 96 | : base() {
|
---|
[4179] | 97 | Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
|
---|
[3938] | 98 | Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
[4015] | 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 |
|
---|
[4851] | 105 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
|
---|
[4852] | 106 | Parameters.Add(new LookupParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
|
---|
[4851] | 107 |
|
---|
[3938] | 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."));
|
---|
[4513] | 114 | Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
|
---|
[3938] | 115 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[4722] | 118 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 119 | return new BestVRPSolutionAnalyzer(this, cloner);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[4851] | 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")) {
|
---|
[4852] | 129 | Parameters.Add(new LookupParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance."));
|
---|
[4851] | 130 | }
|
---|
| 131 | #endregion
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[3938] | 134 | public override IOperation Apply() {
|
---|
| 135 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
[4179] | 136 | ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
|
---|
[3938] | 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 |
|
---|
[4851] | 145 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
| 146 |
|
---|
[3938] | 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),
|
---|
[4068] | 154 | new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value),
|
---|
| 155 | DistanceMatrixParameter.ActualValue, UseDistanceMatrixParameter.ActualValue,
|
---|
[4015] | 156 | ReadyTimeParameter.ActualValue, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue);
|
---|
[3938] | 157 | BestSolutionParameter.ActualValue = solution;
|
---|
| 158 | results.Add(new Result("Best VRP Solution", solution));
|
---|
[4185] | 159 |
|
---|
[4352] | 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 |
|
---|
[3938] | 166 | } else {
|
---|
[4352] | 167 | if (qualities[i].Value <= solution.Quality.Value) {
|
---|
[3938] | 168 | solution.Coordinates = coordinates;
|
---|
| 169 | solution.Solution = best.Clone() as IVRPEncoding;
|
---|
| 170 | solution.Quality.Value = qualities[i].Value;
|
---|
[4352] | 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;
|
---|
[4015] | 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;
|
---|
[3938] | 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[4851] | 184 | if (bestKnownQuality == null ||
|
---|
| 185 | qualities[i].Value < bestKnownQuality.Value) {
|
---|
| 186 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
|
---|
[4852] | 187 | BestKnownSolutionParameter.ActualValue = (IVRPEncoding)best.Clone();
|
---|
[4851] | 188 | }
|
---|
| 189 |
|
---|
[3938] | 190 | return base.Apply();
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|