Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestAverageWorstTours/BestAverageWorstVRPToursCalculator.cs @ 4374

Last change on this file since 4374 was 4374, checked in by svonolfe, 14 years ago

Added analyzers and views (#1177)

File size: 6.2 KB
Line 
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
22using HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Operators;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.VehicleRouting {
29  [Item("BestAverageWorstVRPToursCalculator", "An operator which calculates the current best, average and worst properties of VRP tours in the scope tree.")]
30  [StorableClass]
31  public sealed class BestAverageWorstVRPToursCalculator : SingleSuccessorOperator {
32    public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
33      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
34    }
35    public ValueLookupParameter<DoubleValue> BestDistanceParameter {
36      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestDistance"]; }
37    }
38    public ValueLookupParameter<DoubleValue> AverageDistanceParameter {
39      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageDistance"]; }
40    }
41    public ValueLookupParameter<DoubleValue> WorstDistanceParameter {
42      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstDistance"]; }
43    }
44
45    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
46      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
47    }
48    public ValueLookupParameter<DoubleValue> BestVehiclesUtilizedParameter {
49      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestVehiclesUtilized"]; }
50    }
51    public ValueLookupParameter<DoubleValue> AverageVehiclesUtilizedParameter {
52      get { return (ValueLookupParameter<DoubleValue>)Parameters["AverageVehiclesUtilized"]; }
53    }
54    public ValueLookupParameter<DoubleValue> WorstVehiclesUtilizedParameter {
55      get { return (ValueLookupParameter<DoubleValue>)Parameters["WorstVehiclesUtilized"]; }
56    }
57
58    public BestAverageWorstVRPToursCalculator()
59      : base() {
60      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
61      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestDistance", "The best distance value."));
62      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageDistance", "The average distance value of all solutions."));
63      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstDistance", "The worst distance value of all solutions."));
64
65      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
66      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestVehiclesUtilized", "The best utilized vehicles value."));
67      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageVehiclesUtilized", "The average utilized vehicles value of all solutions."));
68      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstVehiclesUtilized", "The worst utilized vehicles value of all solutions."));
69    }
70
71    private void UpdateDistances() {
72      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
73      if (distances.Length > 0) {
74        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
75        for (int i = 0; i < distances.Length; i++) {
76          if (distances[i].Value < min) min = distances[i].Value;
77          if (distances[i].Value > max) max = distances[i].Value;
78          sum += distances[i].Value;
79        }
80
81        DoubleValue best = BestDistanceParameter.ActualValue;
82        if (best == null) BestDistanceParameter.ActualValue = new DoubleValue(min);
83        else best.Value = min;
84        DoubleValue average = AverageDistanceParameter.ActualValue;
85        if (average == null) AverageDistanceParameter.ActualValue = new DoubleValue(sum / distances.Length);
86        else average.Value = sum / distances.Length;
87        DoubleValue worst = WorstDistanceParameter.ActualValue;
88        if (worst == null) WorstDistanceParameter.ActualValue = new DoubleValue(max);
89        else worst.Value = max;
90      }
91    }
92
93   
94    private void UpdateVehiclesUtilized() {
95      ItemArray<DoubleValue> vehiclesUtilized = VehiclesUtilizedParameter.ActualValue;
96      if (vehiclesUtilized.Length > 0) {
97        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
98        for (int i = 0; i < vehiclesUtilized.Length; i++) {
99          if (vehiclesUtilized[i].Value < min) min = vehiclesUtilized[i].Value;
100          if (vehiclesUtilized[i].Value > max) max = vehiclesUtilized[i].Value;
101          sum += vehiclesUtilized[i].Value;
102        }
103
104        DoubleValue best = BestVehiclesUtilizedParameter.ActualValue;
105        if (best == null) BestVehiclesUtilizedParameter.ActualValue = new DoubleValue(min);
106        else best.Value = min;
107        DoubleValue average = AverageVehiclesUtilizedParameter.ActualValue;
108        if (average == null) AverageVehiclesUtilizedParameter.ActualValue = new DoubleValue(sum / vehiclesUtilized.Length);
109        else average.Value = sum / vehiclesUtilized.Length;
110        DoubleValue worst = WorstVehiclesUtilizedParameter.ActualValue;
111        if (worst == null) WorstVehiclesUtilizedParameter.ActualValue = new DoubleValue(max);
112        else worst.Value = max;
113      }
114    }
115
116    public override IOperation Apply() {
117      UpdateDistances();
118      UpdateVehiclesUtilized();
119
120      return base.Apply();
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.