Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/ConstraintRelaxation/TimeWindowed/TimeWindowRelaxationVRPAnalyzer.cs @ 6711

Last change on this file since 6711 was 6711, checked in by svonolfe, 13 years ago

Added adaptive constraint relaxation for all VRP variants (#1177)

File size: 6.1 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Problems.VehicleRouting.Variants;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
32using HeuristicLab.Parameters;
33using HeuristicLab.Data;
34using HeuristicLab.Common;
35
36namespace HeuristicLab.Problems.VehicleRouting {
37  /// <summary>
38  /// An operator for adaptive constraint relaxation.
39  /// </summary>
40  [Item("TimeWindowRelaxationVRPAnalyzer", "An operator for adaptively relaxing the time window constraints.")]
41  [StorableClass]
42  public class TimeWindowRelaxationVRPAnalyzer: SingleSuccessorOperator, IAnalyzer, ITimeWindowedOperator {
43    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
44      get { return (ILookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
45    }
46    public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
47      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
48    }
49    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
50      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
51    }
52
53    public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
54      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
55    }
56
57    public IValueParameter<DoubleValue> SigmaParameter {
58      get { return (IValueParameter<DoubleValue>)Parameters["Sigma"]; }
59    }
60    public IValueParameter<DoubleValue> PhiParameter {
61      get { return (IValueParameter<DoubleValue>)Parameters["Phi"]; }
62    }
63    public IValueParameter<DoubleValue> MinPenaltyFactorParameter {
64      get { return (IValueParameter<DoubleValue>)Parameters["MinPenaltyFactor"]; }
65    }
66
67    public ValueLookupParameter<ResultCollection> ResultsParameter {
68      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
69    }
70
71    [StorableConstructor]
72    private TimeWindowRelaxationVRPAnalyzer(bool deserializing) : base(deserializing) { }
73
74    public TimeWindowRelaxationVRPAnalyzer()
75      : base() {
76      Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
77      Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
78      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
79
80      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
81
82      Parameters.Add(new ValueParameter<DoubleValue>("Sigma", "The sigma applied to the penalty factor.", new DoubleValue(0.04)));
83      Parameters.Add(new ValueParameter<DoubleValue>("Phi", "The phi applied to the penalty factor.", new DoubleValue(0.01)));
84      Parameters.Add(new ValueParameter<DoubleValue>("MinPenaltyFactor", "The minimum penalty factor.", new DoubleValue(0.01)));
85
86      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new TimeWindowRelaxationVRPAnalyzer(this, cloner);
91    }
92
93    private TimeWindowRelaxationVRPAnalyzer(TimeWindowRelaxationVRPAnalyzer original, Cloner cloner)
94      : base(original, cloner) {
95    }
96
97    public override IOperation Apply() {
98      ITimeWindowedProblemInstance vrptw = ProblemInstanceParameter.ActualValue as ITimeWindowedProblemInstance;
99      ResultCollection results = ResultsParameter.ActualValue;
100     
101      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
102      ItemArray<DoubleValue> tardiness = TardinessParameter.ActualValue;
103     
104      double sigma = SigmaParameter.Value.Value;
105      double phi = PhiParameter.Value.Value;
106      double minPenalty = MinPenaltyFactorParameter.Value.Value;
107
108      for (int j = 0; j < qualities.Length; j++) {
109        qualities[j].Value -= tardiness[j].Value * vrptw.TardinessPenalty.Value;
110      }
111
112      int validCount = 0;
113      for (int j = 0; j < qualities.Length; j++) {
114        if (tardiness[j].Value == 0)
115          validCount++;
116      }
117
118      double factor = 1.0 - ((double)validCount / (double)qualities.Length);
119
120      double min = vrptw.TardinessPenalty.Value / (1 + sigma);
121      double max = vrptw.TardinessPenalty.Value * (1 + phi);
122
123      vrptw.TardinessPenalty = new DoubleValue(min + (max - min) * factor);
124      if (vrptw.TardinessPenalty.Value < minPenalty)
125        vrptw.TardinessPenalty.Value = minPenalty;
126
127      for (int j = 0; j < qualities.Length; j++) {
128        qualities[j].Value += tardiness[j].Value * vrptw.TardinessPenalty.Value;
129      }
130
131      if (!results.ContainsKey("Current Tardiness Penalty")) {
132        results.Add(new Result("Current Tardiness Penalty", new DoubleValue(vrptw.TardinessPenalty.Value)));
133      } else {
134        (results["Current Tardiness Penalty"].Value as DoubleValue).Value = vrptw.TardinessPenalty.Value;
135      }
136
137      return base.Apply();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.