Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestAverageWorstTours/TimeWindowed/BestAverageWorstTimeWindowedVRPToursCalculator.cs

Last change on this file was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
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("BestAverageWorstTimeWindowedVRPToursCalculator", "An operator which calculates the current best, average and worst properties of VRP tours in the scope tree.")]
31  [StorableClass]
32  public sealed class BestAverageWorstTimeWindowedVRPToursCalculator : SingleSuccessorOperator {
33    public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
34      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
35    }
36    public ValueLookupParameter<DoubleValue> BestTardinessParameter {
37      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestTardiness"]; }
38    }
39    public ValueLookupParameter<DoubleValue> AverageTardinessParameter {
40      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageTardiness"]; }
41    }
42    public ValueLookupParameter<DoubleValue> WorstTardinessParameter {
43      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstTardiness"]; }
44    }
45
46    public ScopeTreeLookupParameter<DoubleValue> TravelTimeParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["TravelTime"]; }
48    }
49    public ValueLookupParameter<DoubleValue> BestTravelTimeParameter {
50      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestTravelTime"]; }
51    }
52    public ValueLookupParameter<DoubleValue> AverageTravelTimeParameter {
53      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageTravelTime"]; }
54    }
55    public ValueLookupParameter<DoubleValue> WorstTravelTimeParameter {
56      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstTravelTime"]; }
57    }
58
59    public BestAverageWorstTimeWindowedVRPToursCalculator()
60      : base() {
61      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
62      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestTardiness", "The best tardiness value."));
63      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageTardiness", "The average tardiness value of all solutions."));
64      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstTardiness", "The worst tardiness value of all solutions."));
65
66      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
67      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestTravelTime", "The best travel time value."));
68      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageTravelTime", "The average travel time value of all solutions."));
69      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstTravelTime", "The worst travel time value of all solutions."));
70    }
71
72    [StorableConstructor]
73    private BestAverageWorstTimeWindowedVRPToursCalculator(bool deserializing) : base(deserializing) { }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new BestAverageWorstTimeWindowedVRPToursCalculator(this, cloner);
77    }
78
79    private BestAverageWorstTimeWindowedVRPToursCalculator(BestAverageWorstTimeWindowedVRPToursCalculator original, Cloner cloner)
80      : base(original, cloner) {
81    }
82
83    private void UpdateTardiness() {
84      ItemArray<DoubleValue> tardiness = TardinessParameter.ActualValue;
85      if (tardiness.Length > 0) {
86        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
87        for (int i = 0; i < tardiness.Length; i++) {
88          if (tardiness[i].Value < min) min = tardiness[i].Value;
89          if (tardiness[i].Value > max) max = tardiness[i].Value;
90          sum += tardiness[i].Value;
91        }
92
93        DoubleValue best = BestTardinessParameter.ActualValue;
94        if (best == null) BestTardinessParameter.ActualValue = new DoubleValue(min);
95        else best.Value = min;
96        DoubleValue average = AverageTardinessParameter.ActualValue;
97        if (average == null) AverageTardinessParameter.ActualValue = new DoubleValue(sum / tardiness.Length);
98        else average.Value = sum / tardiness.Length;
99        DoubleValue worst = WorstTardinessParameter.ActualValue;
100        if (worst == null) WorstTardinessParameter.ActualValue = new DoubleValue(max);
101        else worst.Value = max;
102      }
103    }
104
105    private void UpdateTravelTimes() {
106      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
107      if (travelTimes.Length > 0) {
108        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
109        for (int i = 0; i < travelTimes.Length; i++) {
110          if (travelTimes[i].Value < min) min = travelTimes[i].Value;
111          if (travelTimes[i].Value > max) max = travelTimes[i].Value;
112          sum += travelTimes[i].Value;
113        }
114
115        DoubleValue best = BestTravelTimeParameter.ActualValue;
116        if (best == null) BestTravelTimeParameter.ActualValue = new DoubleValue(min);
117        else best.Value = min;
118        DoubleValue average = AverageTravelTimeParameter.ActualValue;
119        if (average == null) AverageTravelTimeParameter.ActualValue = new DoubleValue(sum / travelTimes.Length);
120        else average.Value = sum / travelTimes.Length;
121        DoubleValue worst = WorstTravelTimeParameter.ActualValue;
122        if (worst == null) WorstTravelTimeParameter.ActualValue = new DoubleValue(max);
123        else worst.Value = max;
124      }
125    }
126
127    public override IOperation Apply() {
128      UpdateTardiness();
129      UpdateTravelTimes();
130
131      return base.Apply();
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.