Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/ConstraintRelaxation/PickupViolationsRelaxationVRPAnalyzer.cs @ 17717

Last change on this file since 17717 was 17717, checked in by abeham, 4 years ago

#2521: working on VRP (refactoring all the capabilities, features, and operator discovery)

File size: 6.5 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 HEAL.Attic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
31
32namespace HeuristicLab.Problems.VehicleRouting {
33  /// <summary>
34  /// An operator for adaptive constraint relaxation.
35  /// </summary>
36  [Item("PickupViolationsRelaxationVRPAnalyzer", "An operator for adaptively relaxing the pickup constraints.")]
37  [StorableType("bcf16a7c-5e16-4d96-8b6b-9d5a2ddc0420")]
38  public class PickupViolationsRelaxationVRPAnalyzer : SingleSuccessorOperator, IAnalyzer, IPickupAndDeliveryOperator {
39    [Storable] public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter { get; private set; }
40    [Storable] public IScopeTreeLookupParameter<IVRPEncodedSolution> VRPToursParameter { get; private set; }
41    [Storable] public IScopeTreeLookupParameter<CVRPPDTWEvaluation> EvaluationResultParameter { get; private set; }
42
43    [Storable] public IValueParameter<DoubleValue> SigmaParameter { get; private set; }
44    [Storable] public IValueParameter<DoubleValue> PhiParameter { get; private set; }
45    [Storable] public IValueParameter<DoubleValue> MinPenaltyFactorParameter { get; private set; }
46    [Storable] public IValueParameter<DoubleValue> MaxPenaltyFactorParameter { get; private set; }
47
48    [Storable] public IResultParameter<DoubleValue> CurrentPickupViolationPenaltyResult { get; private set; }
49
50
51    public bool EnabledByDefault {
52      get { return false; }
53    }
54
55    [StorableConstructor]
56    protected PickupViolationsRelaxationVRPAnalyzer(StorableConstructorFlag _) : base(_) { }
57    protected PickupViolationsRelaxationVRPAnalyzer(PickupViolationsRelaxationVRPAnalyzer original, Cloner cloner)
58      : base(original, cloner) {
59      ProblemInstanceParameter = cloner.Clone(original.ProblemInstanceParameter);
60      VRPToursParameter = cloner.Clone(original.VRPToursParameter);
61      EvaluationResultParameter = cloner.Clone(original.EvaluationResultParameter);
62      SigmaParameter = cloner.Clone(original.SigmaParameter);
63      PhiParameter = cloner.Clone(original.PhiParameter);
64      MinPenaltyFactorParameter = cloner.Clone(original.MinPenaltyFactorParameter);
65      MaxPenaltyFactorParameter = cloner.Clone(original.MaxPenaltyFactorParameter);
66      CurrentPickupViolationPenaltyResult = cloner.Clone(original.CurrentPickupViolationPenaltyResult);
67    }
68    public PickupViolationsRelaxationVRPAnalyzer()
69      : base() {
70      Parameters.Add(ProblemInstanceParameter = new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
71      Parameters.Add(VRPToursParameter = new ScopeTreeLookupParameter<IVRPEncodedSolution>("VRPTours", "The VRP tours which should be evaluated."));
72      Parameters.Add(EvaluationResultParameter = new ScopeTreeLookupParameter<CVRPPDTWEvaluation>("EvaluationResult", "The evaluations of the VRP solutions which should be analyzed."));
73
74      Parameters.Add(SigmaParameter = new ValueParameter<DoubleValue>("Sigma", "The sigma applied to the penalty factor.", new DoubleValue(0.5)));
75      Parameters.Add(PhiParameter = new ValueParameter<DoubleValue>("Phi", "The phi applied to the penalty factor.", new DoubleValue(0.5)));
76      Parameters.Add(MinPenaltyFactorParameter = new ValueParameter<DoubleValue>("MinPenaltyFactor", "The minimum penalty factor.", new DoubleValue(0.01)));
77      Parameters.Add(MaxPenaltyFactorParameter = new ValueParameter<DoubleValue>("MaxPenaltyFactor", "The maximum penalty factor.", new DoubleValue(100000)));
78      Parameters.Add(CurrentPickupViolationPenaltyResult = new ResultParameter<DoubleValue>("Current Pickup Violation Penalty", "The current penalty applied to violating pickup-dropoff precedence constraint.", new DoubleValue(double.NaN)));
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new PickupViolationsRelaxationVRPAnalyzer(this, cloner);
83    }
84
85    public override IOperation Apply() {
86      var pdp = (IPickupAndDeliveryProblemInstance)ProblemInstanceParameter.ActualValue;
87
88      ItemArray<CVRPPDTWEvaluation> evaluations = EvaluationResultParameter.ActualValue;
89
90      double sigma = SigmaParameter.Value.Value;
91      double phi = PhiParameter.Value.Value;
92      double minPenalty = MinPenaltyFactorParameter.Value.Value;
93      double maxPenalty = MaxPenaltyFactorParameter.Value.Value;
94
95      for (int j = 0; j < evaluations.Length; j++) {
96        evaluations[j].Quality -= evaluations[j].PickupViolations * pdp.PickupViolationPenalty.Value;
97      }
98
99      int validCount = 0;
100      for (int j = 0; j < evaluations.Length; j++) {
101        if (evaluations[j].PickupViolations == 0)
102          validCount++;
103      }
104
105      double factor = 1.0 - ((double)validCount / (double)evaluations.Length);
106
107      double min = pdp.PickupViolationPenalty.Value / (1 + sigma);
108      double max = pdp.PickupViolationPenalty.Value * (1 + phi);
109
110      pdp.CurrentPickupViolationPenalty = new DoubleValue(min + (max - min) * factor);
111      if (pdp.CurrentPickupViolationPenalty.Value < minPenalty)
112        pdp.CurrentPickupViolationPenalty.Value = minPenalty;
113      if (pdp.CurrentPickupViolationPenalty.Value > maxPenalty)
114        pdp.CurrentPickupViolationPenalty.Value = maxPenalty;
115
116      for (int j = 0; j < evaluations.Length; j++) {
117        evaluations[j].Quality += evaluations[j].PickupViolations * pdp.CurrentPickupViolationPenalty.Value;
118      }
119
120      CurrentPickupViolationPenaltyResult.ActualValue = new DoubleValue(pdp.CurrentPickupViolationPenalty.Value);
121
122      return base.Apply();
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.