Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestAverageWorstTours/BestAverageWorstVRPToursCalculator.cs @ 11171

Last change on this file since 11171 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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("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> DistanceParameter {
34      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
35    }
36    public ValueLookupParameter<DoubleValue> BestDistanceParameter {
37      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestDistance"]; }
38    }
39    public ValueLookupParameter<DoubleValue> AverageDistanceParameter {
40      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageDistance"]; }
41    }
42    public ValueLookupParameter<DoubleValue> WorstDistanceParameter {
43      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstDistance"]; }
44    }
45
46    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
48    }
49    public ValueLookupParameter<DoubleValue> BestVehiclesUtilizedParameter {
50      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestVehiclesUtilized"]; }
51    }
52    public ValueLookupParameter<DoubleValue> AverageVehiclesUtilizedParameter {
53      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageVehiclesUtilized"]; }
54    }
55    public ValueLookupParameter<DoubleValue> WorstVehiclesUtilizedParameter {
56      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstVehiclesUtilized"]; }
57    }
58
59    public BestAverageWorstVRPToursCalculator()
60      : base() {
61      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
62      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestDistance", "The best distance value."));
63      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageDistance", "The average distance value of all solutions."));
64      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstDistance", "The worst distance value of all solutions."));
65
66      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
67      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestVehiclesUtilized", "The best utilized vehicles value."));
68      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageVehiclesUtilized", "The average utilized vehicles value of all solutions."));
69      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstVehiclesUtilized", "The worst utilized vehicles value of all solutions."));
70    }
71
72    [StorableConstructor]
73    private BestAverageWorstVRPToursCalculator(bool deserializing) : base(deserializing) { }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new BestAverageWorstVRPToursCalculator(this, cloner);
77    }
78
79    private BestAverageWorstVRPToursCalculator(BestAverageWorstVRPToursCalculator original, Cloner cloner)
80      : base(original, cloner) {
81    }
82
83    private void UpdateDistances() {
84      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
85      if (distances.Length > 0) {
86        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
87        for (int i = 0; i < distances.Length; i++) {
88          if (distances[i].Value < min) min = distances[i].Value;
89          if (distances[i].Value > max) max = distances[i].Value;
90          sum += distances[i].Value;
91        }
92
93        DoubleValue best = BestDistanceParameter.ActualValue;
94        if (best == null) BestDistanceParameter.ActualValue = new DoubleValue(min);
95        else best.Value = min;
96        DoubleValue average = AverageDistanceParameter.ActualValue;
97        if (average == null) AverageDistanceParameter.ActualValue = new DoubleValue(sum / distances.Length);
98        else average.Value = sum / distances.Length;
99        DoubleValue worst = WorstDistanceParameter.ActualValue;
100        if (worst == null) WorstDistanceParameter.ActualValue = new DoubleValue(max);
101        else worst.Value = max;
102      }
103    }
104
105
106    private void UpdateVehiclesUtilized() {
107      ItemArray<DoubleValue> vehiclesUtilized = VehiclesUtilizedParameter.ActualValue;
108      if (vehiclesUtilized.Length > 0) {
109        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
110        for (int i = 0; i < vehiclesUtilized.Length; i++) {
111          if (vehiclesUtilized[i].Value < min) min = vehiclesUtilized[i].Value;
112          if (vehiclesUtilized[i].Value > max) max = vehiclesUtilized[i].Value;
113          sum += vehiclesUtilized[i].Value;
114        }
115
116        DoubleValue best = BestVehiclesUtilizedParameter.ActualValue;
117        if (best == null) BestVehiclesUtilizedParameter.ActualValue = new DoubleValue(min);
118        else best.Value = min;
119        DoubleValue average = AverageVehiclesUtilizedParameter.ActualValue;
120        if (average == null) AverageVehiclesUtilizedParameter.ActualValue = new DoubleValue(sum / vehiclesUtilized.Length);
121        else average.Value = sum / vehiclesUtilized.Length;
122        DoubleValue worst = WorstVehiclesUtilizedParameter.ActualValue;
123        if (worst == null) WorstVehiclesUtilizedParameter.ActualValue = new DoubleValue(max);
124        else worst.Value = max;
125      }
126    }
127
128    public override IOperation Apply() {
129      UpdateDistances();
130      UpdateVehiclesUtilized();
131
132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.