Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/CustomerRelocation/PotvinCustomerRelocationMoveMaker.cs @ 5867

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

Merged changes from trunk into branch (#1177)

File size: 6.3 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.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Common;
29using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
32  [Item("PotvinCustomerRelocationMoveMaker", "Peforms the customer relocation move on a given VRP encoding and updates the quality.")]
33  [StorableClass]
34  public class PotvinCustomerRelocationMoveMaker : PotvinMoveMaker, IPotvinCustomerRelocationMoveOperator, IMoveMaker {
35    public ILookupParameter<PotvinCustomerRelocationMove> CustomerRelocationMoveParameter {
36      get { return (ILookupParameter<PotvinCustomerRelocationMove>)Parameters["PotvinCustomerRelocationMove"]; }
37    }
38
39    public override ILookupParameter VRPMoveParameter {
40         get { return CustomerRelocationMoveParameter; }
41    }
42
43    public IValueParameter<DoubleValue> SigmaParameter {
44      get { return (IValueParameter<DoubleValue>)Parameters["Sigma"]; }
45    }
46
47    public IValueParameter<DoubleValue> MinPenaltyFactorParameter {
48      get { return (IValueParameter<DoubleValue>)Parameters["MinPenaltyFactor"]; }
49    }
50
51    public IValueParameter<DoubleValue> MaxPenaltyFactorParameter {
52      get { return (IValueParameter<DoubleValue>)Parameters["MaxPenaltyFactor"]; }
53    }
54
55    public ILookupParameter<VariableCollection> MemoriesParameter {
56      get { return (ILookupParameter<VariableCollection>)Parameters["Memories"]; }
57    }
58
59    public IValueParameter<StringValue> AdditionFrequencyMemoryKeyParameter {
60      get { return (IValueParameter<StringValue>)Parameters["AdditionFrequencyMemoryKey"]; }
61    }
62
63    [StorableConstructor]
64    private PotvinCustomerRelocationMoveMaker(bool deserializing) : base(deserializing) { }
65
66    public PotvinCustomerRelocationMoveMaker()
67      : base() {
68      Parameters.Add(new LookupParameter<PotvinCustomerRelocationMove>("PotvinCustomerRelocationMove", "The moves that should be made."));
69      Parameters.Add(new ValueParameter<DoubleValue>("Sigma", "The sigma applied to the penalty factor.", new DoubleValue(0.5)));
70      Parameters.Add(new ValueParameter<DoubleValue>("MinPenaltyFactor", "The minimum penalty factor.", new DoubleValue(-1000.0)));
71      Parameters.Add(new ValueParameter<DoubleValue>("MaxPenaltyFactor", "The maximum penalty factor.", new DoubleValue(1000.0)));
72
73      Parameters.Add(new LookupParameter<VariableCollection>("Memories", "The TS memory collection."));
74      Parameters.Add(new ValueParameter<StringValue>("AdditionFrequencyMemoryKey", "The key that is used for the addition frequency in the TS memory.", new StringValue("AdditionFrequency")));
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new PotvinCustomerRelocationMoveMaker(this, cloner);
79    }
80
81    protected PotvinCustomerRelocationMoveMaker(PotvinCustomerRelocationMoveMaker original, Cloner cloner)
82      : base(original, cloner) {
83    }
84
85    public static void Apply(PotvinEncoding solution, PotvinCustomerRelocationMove move) {
86      if (move.Tour >= solution.Tours.Count)
87        solution.Tours.Add(new Tour());
88      Tour tour = solution.Tours[move.Tour];
89
90      Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
91      oldTour.Stops.Remove(move.City);
92      /*if (oldTour.Stops.Count == 0)
93        solution.Tours.Remove(oldTour);*/
94
95      int place = solution.FindBestInsertionPlace(tour, move.City);
96
97      tour.Stops.Insert(place, move.City);
98    }
99
100    protected override void PerformMove() {
101      PotvinCustomerRelocationMove move = CustomerRelocationMoveParameter.ActualValue;
102
103      PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
104      Apply(newSolution, move);
105      VRPToursParameter.ActualValue = newSolution;
106
107      //reset move quality
108      VRPEvaluation eval = ProblemInstance.EvaluatorParameter.Value.Evaluate(ProblemInstance, newSolution);
109      MoveQualityParameter.ActualValue.Value = eval.Quality;
110
111      //update penalty factor
112      double sigma = SigmaParameter.Value.Value;
113      if (ProblemInstance.EvaluatorParameter.Value.Feasible(eval)) {
114        newSolution.PenaltyFactor /= (1 + sigma);
115        if (newSolution.PenaltyFactor < MinPenaltyFactorParameter.Value.Value)
116          newSolution.PenaltyFactor = MinPenaltyFactorParameter.Value.Value;
117      } else {
118        newSolution.PenaltyFactor *= (1 + sigma);
119        if (newSolution.PenaltyFactor > MaxPenaltyFactorParameter.Value.Value)
120          newSolution.PenaltyFactor = MaxPenaltyFactorParameter.Value.Value;
121      }
122
123      //update memory
124      VariableCollection memory = MemoriesParameter.ActualValue;
125      string key = AdditionFrequencyMemoryKeyParameter.Value.Value;
126
127      if (memory != null) {
128        if (!memory.ContainsKey(key)) {
129          memory.Add(new Variable(key,
130              new ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue>()));
131        }
132        ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue> additionFrequency =
133          memory[key].Value as ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue>;
134
135        PotvinCustomerRelocationMoveAttribute attr = new PotvinCustomerRelocationMoveAttribute(0, move.Tour, move.City);
136        if (!additionFrequency.ContainsKey(attr))
137          additionFrequency[attr] = new IntValue(0);
138
139        additionFrequency[attr].Value++;
140      }
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.