Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestAverageWorstTours/Capacitated/BestAverageWorstCapacitatedVRPToursCalculator.cs @ 11170

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

#2115 updated copyright year in stable branch

File size: 4.2 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("BestAverageWorstCapacitatedVRPToursCalculator", "An operator which calculates the current best, average and worst properties of VRP tours in the scope tree.")]
31  [StorableClass]
32  public sealed class BestAverageWorstCapacitatedVRPToursCalculator : 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 BestAverageWorstCapacitatedVRPToursCalculator()
47      : base() {
48      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
49      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestOverload", "The best overload value."));
50      Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageOverload", "The average overload value of all solutions."));
51      Parameters.Add(new ValueLookupParameter<DoubleValue>("WorstOverload", "The worst overload value of all solutions."));
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new BestAverageWorstCapacitatedVRPToursCalculator(this, cloner);
56    }
57
58    private BestAverageWorstCapacitatedVRPToursCalculator(BestAverageWorstCapacitatedVRPToursCalculator original, Cloner cloner)
59      : base(original, cloner) {
60    }
61
62    [StorableConstructor]
63    private BestAverageWorstCapacitatedVRPToursCalculator(bool deserializing) : base(deserializing) { }
64
65    private void UpdateOverloads() {
66      ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
67      if (overloads.Length > 0) {
68        double min = double.MaxValue, max = double.MinValue, sum = 0.0;
69        for (int i = 0; i < overloads.Length; i++) {
70          if (overloads[i].Value < min) min = overloads[i].Value;
71          if (overloads[i].Value > max) max = overloads[i].Value;
72          sum += overloads[i].Value;
73        }
74
75        DoubleValue best = BestOverloadParameter.ActualValue;
76        if (best == null) BestOverloadParameter.ActualValue = new DoubleValue(min);
77        else best.Value = min;
78        DoubleValue average = AverageOverloadParameter.ActualValue;
79        if (average == null) AverageOverloadParameter.ActualValue = new DoubleValue(sum / overloads.Length);
80        else average.Value = sum / overloads.Length;
81        DoubleValue worst = WorstOverloadParameter.ActualValue;
82        if (worst == null) WorstOverloadParameter.ActualValue = new DoubleValue(max);
83        else worst.Value = max;
84      }
85    }
86
87    public override IOperation Apply() {
88      UpdateOverloads();
89
90      return base.Apply();
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.