Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8228 was 8053, checked in by ascheibe, 13 years ago

#1722 fixed more licensing information and source formatting

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
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 ILookupParameter<VariableCollection> MemoriesParameter {
44      get { return (ILookupParameter<VariableCollection>)Parameters["Memories"]; }
45    }
46
47    public IValueParameter<StringValue> AdditionFrequencyMemoryKeyParameter {
48      get { return (IValueParameter<StringValue>)Parameters["AdditionFrequencyMemoryKey"]; }
49    }
50
51    [StorableConstructor]
52    protected PotvinCustomerRelocationMoveMaker(bool deserializing) : base(deserializing) { }
53
54    public PotvinCustomerRelocationMoveMaker()
55      : base() {
56      Parameters.Add(new LookupParameter<PotvinCustomerRelocationMove>("PotvinCustomerRelocationMove", "The moves that should be made."));
57
58      Parameters.Add(new LookupParameter<VariableCollection>("Memories", "The TS memory collection."));
59      Parameters.Add(new ValueParameter<StringValue>("AdditionFrequencyMemoryKey", "The key that is used for the addition frequency in the TS memory.", new StringValue("AdditionFrequency")));
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new PotvinCustomerRelocationMoveMaker(this, cloner);
64    }
65
66    protected PotvinCustomerRelocationMoveMaker(PotvinCustomerRelocationMoveMaker original, Cloner cloner)
67      : base(original, cloner) {
68    }
69
70    public static void Apply(PotvinEncoding solution, PotvinCustomerRelocationMove move, IVRPProblemInstance problemInstance) {
71      if (move.Tour >= solution.Tours.Count)
72        solution.Tours.Add(new Tour());
73      Tour tour = solution.Tours[move.Tour];
74
75      Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
76      oldTour.Stops.Remove(move.City);
77      /*if (oldTour.Stops.Count == 0)
78        solution.Tours.Remove(oldTour);*/
79
80      int place = solution.FindBestInsertionPlace(tour, move.City);
81      tour.Stops.Insert(place, move.City);
82
83      solution.Repair();
84    }
85
86    protected override void PerformMove() {
87      PotvinCustomerRelocationMove move = CustomerRelocationMoveParameter.ActualValue;
88
89      PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
90      Apply(newSolution, move, ProblemInstance);
91      newSolution.Repair();
92      VRPToursParameter.ActualValue = newSolution;
93
94      //reset move quality
95      VRPEvaluation eval = ProblemInstance.Evaluate(newSolution);
96      MoveQualityParameter.ActualValue.Value = eval.Quality;
97
98      //update memory
99      VariableCollection memory = MemoriesParameter.ActualValue;
100      string key = AdditionFrequencyMemoryKeyParameter.Value.Value;
101
102      if (memory != null) {
103        if (!memory.ContainsKey(key)) {
104          memory.Add(new Variable(key,
105              new ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue>()));
106        }
107        ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue> additionFrequency =
108          memory[key].Value as ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue>;
109
110        PotvinCustomerRelocationMoveAttribute attr = new PotvinCustomerRelocationMoveAttribute(0, move.Tour, move.City);
111        if (!additionFrequency.ContainsKey(attr))
112          additionFrequency[attr] = new IntValue(0);
113
114        additionFrequency[attr].Value++;
115      }
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.