Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestAverageWorstTours/PickupAndDelivery/BestAverageWorstPickupAndDeliveryVRPToursCalculator.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
28
29namespace HeuristicLab.Problems.VehicleRouting {
30  [Item("BestAverageWorstPickupAndDeliveryVRPToursCalculator", "An operator which calculates the current best, average and worst properties of VRP tours in the scope tree.")]
31  [StorableType("C6F8ED53-EF3E-49E5-9D29-227E870AF3D8")]
32  public sealed class BestAverageWorstPickupAndDeliveryVRPToursCalculator : SingleSuccessorOperator {
33    public ScopeTreeLookupParameter<IntValue> PickupViolationsParameter {
34      get { return (ScopeTreeLookupParameter<IntValue>)Parameters["PickupViolations"]; }
35    }
36    public ValueLookupParameter<IntValue> BestPickupViolationsParameter {
37      get { return (ValueLookupParameter<IntValue>)Parameters["BestPickupViolations"]; }
38    }
39    public ValueLookupParameter<DoubleValue> AveragePickupViolationsParameter {
40      get { return (ValueLookupParameter<DoubleValue>)Parameters["AveragePickupViolations"]; }
41    }
42    public ValueLookupParameter<IntValue> WorstPickupViolationsParameter {
43      get { return (ValueLookupParameter<IntValue>)Parameters["WorstPickupViolations"]; }
44    }
45
46    public BestAverageWorstPickupAndDeliveryVRPToursCalculator()
47      : base() {
48      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("PickupViolations", "The pickup violations of the VRP solutions which should be analyzed."));
49      Parameters.Add(new ValueLookupParameter<IntValue>("BestPickupViolations", "The best pickup violations value."));
50      Parameters.Add(new ValueLookupParameter<DoubleValue>("AveragePickupViolations", "The average pickup violations value of all solutions."));
51      Parameters.Add(new ValueLookupParameter<IntValue>("WorstPickupViolations", "The worst pickup violations value of all solutions."));
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new BestAverageWorstPickupAndDeliveryVRPToursCalculator(this, cloner);
56    }
57
58    private BestAverageWorstPickupAndDeliveryVRPToursCalculator(BestAverageWorstPickupAndDeliveryVRPToursCalculator original, Cloner cloner)
59      : base(original, cloner) {
60    }
61
62    [StorableConstructor]
63    private BestAverageWorstPickupAndDeliveryVRPToursCalculator(StorableConstructorFlag _) : base(_) { }
64
65    private void UpdatePickupViolations() {
66      ItemArray<IntValue> pickupViolations = PickupViolationsParameter.ActualValue;
67      if (pickupViolations.Length > 0) {
68        int min = int.MaxValue, max = int.MinValue, sum = 0;
69        for (int i = 0; i < pickupViolations.Length; i++) {
70          if (pickupViolations[i].Value < min) min = pickupViolations[i].Value;
71          if (pickupViolations[i].Value > max) max = pickupViolations[i].Value;
72          sum += pickupViolations[i].Value;
73        }
74
75        IntValue best = BestPickupViolationsParameter.ActualValue;
76        if (best == null) BestPickupViolationsParameter.ActualValue = new IntValue(min);
77        else best.Value = min;
78        DoubleValue average = AveragePickupViolationsParameter.ActualValue;
79        if (average == null) AveragePickupViolationsParameter.ActualValue = new DoubleValue(sum / pickupViolations.Length);
80        else average.Value = sum / pickupViolations.Length;
81        IntValue worst = WorstPickupViolationsParameter.ActualValue;
82        if (worst == null) WorstPickupViolationsParameter.ActualValue = new IntValue(max);
83        else worst.Value = max;
84      }
85    }
86
87    public override IOperation Apply() {
88      UpdatePickupViolations();
89
90      return base.Apply();
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.