Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Analyzers/BestAverageWorstVRPToursCalculator.cs @ 5024

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

Merged cloning refactoring branch back into trunk (#922)

File size: 13.3 KB
RevLine 
[4301]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
[4722]22using HeuristicLab.Common;
[4301]23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.VehicleRouting {
30  [Item("BestAverageWorstVRPToursCalculator", "An operator which calculates the current best, average and worst properties of VRP tours in the scope tree.")]
31  [StorableClass]
32  public sealed class BestAverageWorstVRPToursCalculator : SingleSuccessorOperator {
33    public ScopeTreeLookupParameter<DoubleValue> OverloadParameter {
34      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Overload"]; }
35    }
36    public ValueLookupParameter<DoubleValue> BestOverloadParameter {
37      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestOverload"]; }
38    }
39    public ValueLookupParameter<DoubleValue> AverageOverloadParameter {
40      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageOverload"]; }
41    }
42    public ValueLookupParameter<DoubleValue> WorstOverloadParameter {
43      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstOverload"]; }
44    }
45
46    public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
48    }
49    public ValueLookupParameter<DoubleValue> BestTardinessParameter {
50      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestTardiness"]; }
51    }
52    public ValueLookupParameter<DoubleValue> AverageTardinessParameter {
53      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageTardiness"]; }
54    }
55    public ValueLookupParameter<DoubleValue> WorstTardinessParameter {
56      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstTardiness"]; }
57    }
58
59    public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
60      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
61    }
62    public ValueLookupParameter<DoubleValue> BestDistanceParameter {
63      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestDistance"]; }
64    }
65    public ValueLookupParameter<DoubleValue> AverageDistanceParameter {
66      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageDistance"]; }
67    }
68    public ValueLookupParameter<DoubleValue> WorstDistanceParameter {
69      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstDistance"]; }
70    }
71
72    public ScopeTreeLookupParameter<DoubleValue> TravelTimeParameter {
73      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["TravelTime"]; }
74    }
75    public ValueLookupParameter<DoubleValue> BestTravelTimeParameter {
76      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestTravelTime"]; }
77    }
78    public ValueLookupParameter<DoubleValue> AverageTravelTimeParameter {
79      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageTravelTime"]; }
80    }
81    public ValueLookupParameter<DoubleValue> WorstTravelTimeParameter {
82      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstTravelTime"]; }
83    }
84
85    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
86      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
87    }
88    public ValueLookupParameter<DoubleValue> BestVehiclesUtilizedParameter {
89      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestVehiclesUtilized"]; }
90    }
91    public ValueLookupParameter<DoubleValue> AverageVehiclesUtilizedParameter {
92      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageVehiclesUtilized"]; }
93    }
94    public ValueLookupParameter<DoubleValue> WorstVehiclesUtilizedParameter {
95      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstVehiclesUtilized"]; }
96    }
97
[4722]98    [StorableConstructor]
99    private BestAverageWorstVRPToursCalculator(bool deserializing) : base(deserializing) { }
100    private BestAverageWorstVRPToursCalculator(BestAverageWorstVRPToursCalculator original, Cloner cloner)
101      : base(original, cloner) {
102    }
[4301]103    public BestAverageWorstVRPToursCalculator()
104      : base() {
105      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
106      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestDistance", "The best distance value."));
107      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageDistance", "The average distance value of all solutions."));
108      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstDistance", "The worst distance value of all solutions."));
109
110      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
111      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestOverload", "The best overload value."));
112      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageOverload", "The average overload value of all solutions."));
113      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstOverload", "The worst overload value of all solutions."));
114
115      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
116      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestTardiness", "The best tardiness value."));
117      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageTardiness", "The average tardiness value of all solutions."));
118      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstTardiness", "The worst tardiness value of all solutions."));
119
120      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
121      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestTravelTime", "The best travel time value."));
122      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageTravelTime", "The average travel time value of all solutions."));
123      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstTravelTime", "The worst travel time value of all solutions."));
124
125      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
126      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestVehiclesUtilized", "The best utilized vehicles value."));
127      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageVehiclesUtilized", "The average utilized vehicles value of all solutions."));
128      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstVehiclesUtilized", "The worst utilized vehicles value of all solutions."));
129    }
130
[4722]131    public override IDeepCloneable Clone(Cloner cloner) {
132      return new BestAverageWorstVRPToursCalculator(this, cloner);
133    }
134
[4301]135    private void UpdateOverloads() {
136      ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
137      if (overloads.Length > 0) {
138        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
139        for (int i = 0; i < overloads.Length; i++) {
140          if (overloads[i].Value < min) min = overloads[i].Value;
141          if (overloads[i].Value > max) max = overloads[i].Value;
142          sum += overloads[i].Value;
143        }
144
145        DoubleValue best = BestOverloadParameter.ActualValue;
146        if (best == null) BestOverloadParameter.ActualValue = new DoubleValue(min);
147        else best.Value = min;
148        DoubleValue average = AverageOverloadParameter.ActualValue;
149        if (average == null) AverageOverloadParameter.ActualValue = new DoubleValue(sum / overloads.Length);
150        else average.Value = sum / overloads.Length;
151        DoubleValue worst = WorstOverloadParameter.ActualValue;
152        if (worst == null) WorstOverloadParameter.ActualValue = new DoubleValue(max);
153        else worst.Value = max;
154      }
155    }
156
157    private void UpdateTardiness() {
158      ItemArray<DoubleValue> tardiness = TardinessParameter.ActualValue;
159      if (tardiness.Length > 0) {
160        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
161        for (int i = 0; i < tardiness.Length; i++) {
162          if (tardiness[i].Value < min) min = tardiness[i].Value;
163          if (tardiness[i].Value > max) max = tardiness[i].Value;
164          sum += tardiness[i].Value;
165        }
166
167        DoubleValue best = BestTardinessParameter.ActualValue;
168        if (best == null) BestTardinessParameter.ActualValue = new DoubleValue(min);
169        else best.Value = min;
170        DoubleValue average = AverageTardinessParameter.ActualValue;
171        if (average == null) AverageTardinessParameter.ActualValue = new DoubleValue(sum / tardiness.Length);
172        else average.Value = sum / tardiness.Length;
173        DoubleValue worst = WorstTardinessParameter.ActualValue;
174        if (worst == null) WorstTardinessParameter.ActualValue = new DoubleValue(max);
175        else worst.Value = max;
176      }
177    }
178
179    private void UpdateDistances() {
180      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
181      if (distances.Length > 0) {
182        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
183        for (int i = 0; i < distances.Length; i++) {
184          if (distances[i].Value < min) min = distances[i].Value;
185          if (distances[i].Value > max) max = distances[i].Value;
186          sum += distances[i].Value;
187        }
188
189        DoubleValue best = BestDistanceParameter.ActualValue;
190        if (best == null) BestDistanceParameter.ActualValue = new DoubleValue(min);
191        else best.Value = min;
192        DoubleValue average = AverageDistanceParameter.ActualValue;
193        if (average == null) AverageDistanceParameter.ActualValue = new DoubleValue(sum / distances.Length);
194        else average.Value = sum / distances.Length;
195        DoubleValue worst = WorstDistanceParameter.ActualValue;
196        if (worst == null) WorstDistanceParameter.ActualValue = new DoubleValue(max);
197        else worst.Value = max;
198      }
199    }
200
201    private void UpdateTravelTimes() {
202      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
203      if (travelTimes.Length > 0) {
204        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
205        for (int i = 0; i < travelTimes.Length; i++) {
206          if (travelTimes[i].Value < min) min = travelTimes[i].Value;
207          if (travelTimes[i].Value > max) max = travelTimes[i].Value;
208          sum += travelTimes[i].Value;
209        }
210
211        DoubleValue best = BestTravelTimeParameter.ActualValue;
212        if (best == null) BestTravelTimeParameter.ActualValue = new DoubleValue(min);
213        else best.Value = min;
214        DoubleValue average = AverageTravelTimeParameter.ActualValue;
215        if (average == null) AverageTravelTimeParameter.ActualValue = new DoubleValue(sum / travelTimes.Length);
216        else average.Value = sum / travelTimes.Length;
217        DoubleValue worst = WorstTravelTimeParameter.ActualValue;
218        if (worst == null) WorstTravelTimeParameter.ActualValue = new DoubleValue(max);
219        else worst.Value = max;
220      }
221    }
222
223    private void UpdateVehiclesUtilized() {
224      ItemArray<DoubleValue> vehiclesUtilized = VehiclesUtilizedParameter.ActualValue;
225      if (vehiclesUtilized.Length > 0) {
226        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
227        for (int i = 0; i < vehiclesUtilized.Length; i++) {
228          if (vehiclesUtilized[i].Value < min) min = vehiclesUtilized[i].Value;
229          if (vehiclesUtilized[i].Value > max) max = vehiclesUtilized[i].Value;
230          sum += vehiclesUtilized[i].Value;
231        }
232
233        DoubleValue best = BestVehiclesUtilizedParameter.ActualValue;
234        if (best == null) BestVehiclesUtilizedParameter.ActualValue = new DoubleValue(min);
235        else best.Value = min;
236        DoubleValue average = AverageVehiclesUtilizedParameter.ActualValue;
237        if (average == null) AverageVehiclesUtilizedParameter.ActualValue = new DoubleValue(sum / vehiclesUtilized.Length);
238        else average.Value = sum / vehiclesUtilized.Length;
239        DoubleValue worst = WorstVehiclesUtilizedParameter.ActualValue;
240        if (worst == null) WorstVehiclesUtilizedParameter.ActualValue = new DoubleValue(max);
241        else worst.Value = max;
242      }
243    }
244
245    public override IOperation Apply() {
246      UpdateOverloads();
247      UpdateTardiness();
248      UpdateDistances();
249      UpdateTravelTimes();
250      UpdateVehiclesUtilized();
251
252      return base.Apply();
253    }
254  }
255}
Note: See TracBrowser for help on using the repository browser.