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