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 |
|
---|
22 | using HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Operators;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
31 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
34 | [Item("PotvinCustomerRelocationMoveMaker", "Peforms the customer relocation move on a given VRP encoding and updates the quality.")]
|
---|
35 | [StorableClass]
|
---|
36 | public class PotvinCustomerRelocationMoveMaker : PotvinMoveMaker, IPotvinCustomerRelocationMoveOperator, IMoveMaker {
|
---|
37 | public ILookupParameter<PotvinCustomerRelocationMove> CustomerRelocationMoveParameter {
|
---|
38 | get { return (ILookupParameter<PotvinCustomerRelocationMove>)Parameters["PotvinCustomerRelocationMove"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override ILookupParameter VRPMoveParameter {
|
---|
42 | get { return CustomerRelocationMoveParameter; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public ILookupParameter<VariableCollection> MemoriesParameter {
|
---|
46 | get { return (ILookupParameter<VariableCollection>)Parameters["Memories"]; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public IValueParameter<StringValue> AdditionFrequencyMemoryKeyParameter {
|
---|
50 | get { return (IValueParameter<StringValue>)Parameters["AdditionFrequencyMemoryKey"]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | private PotvinCustomerRelocationMoveMaker(bool deserializing) : base(deserializing) { }
|
---|
55 |
|
---|
56 | public PotvinCustomerRelocationMoveMaker()
|
---|
57 | : base() {
|
---|
58 | Parameters.Add(new LookupParameter<PotvinCustomerRelocationMove>("PotvinCustomerRelocationMove", "The moves that should be made."));
|
---|
59 |
|
---|
60 | Parameters.Add(new LookupParameter<VariableCollection>("Memories", "The TS memory collection."));
|
---|
61 | Parameters.Add(new ValueParameter<StringValue>("AdditionFrequencyMemoryKey", "The key that is used for the addition frequency in the TS memory.", new StringValue("AdditionFrequency")));
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
65 | return new PotvinCustomerRelocationMoveMaker(this, cloner);
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected PotvinCustomerRelocationMoveMaker(PotvinCustomerRelocationMoveMaker original, Cloner cloner)
|
---|
69 | : base(original, cloner) {
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static void Apply(PotvinEncoding solution, PotvinCustomerRelocationMove move, IVRPProblemInstance problemInstance) {
|
---|
73 | if (move.Tour >= solution.Tours.Count)
|
---|
74 | solution.Tours.Add(new Tour());
|
---|
75 | Tour tour = solution.Tours[move.Tour];
|
---|
76 |
|
---|
77 | Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
|
---|
78 | oldTour.Stops.Remove(move.City);
|
---|
79 | /*if (oldTour.Stops.Count == 0)
|
---|
80 | solution.Tours.Remove(oldTour);*/
|
---|
81 |
|
---|
82 | int place = solution.FindBestInsertionPlace(tour, move.City);
|
---|
83 | tour.Stops.Insert(place, move.City);
|
---|
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 | VRPToursParameter.ActualValue = newSolution;
|
---|
92 |
|
---|
93 | //reset move quality
|
---|
94 | VRPEvaluation eval = ProblemInstance.Evaluate(newSolution);
|
---|
95 | MoveQualityParameter.ActualValue.Value = eval.Quality;
|
---|
96 |
|
---|
97 | //update memory
|
---|
98 | VariableCollection memory = MemoriesParameter.ActualValue;
|
---|
99 | string key = AdditionFrequencyMemoryKeyParameter.Value.Value;
|
---|
100 |
|
---|
101 | if (memory != null) {
|
---|
102 | if (!memory.ContainsKey(key)) {
|
---|
103 | memory.Add(new Variable(key,
|
---|
104 | new ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue>()));
|
---|
105 | }
|
---|
106 | ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue> additionFrequency =
|
---|
107 | memory[key].Value as ItemDictionary<PotvinCustomerRelocationMoveAttribute, IntValue>;
|
---|
108 |
|
---|
109 | PotvinCustomerRelocationMoveAttribute attr = new PotvinCustomerRelocationMoveAttribute(0, move.Tour, move.City);
|
---|
110 | if (!additionFrequency.ContainsKey(attr))
|
---|
111 | additionFrequency[attr] = new IntValue(0);
|
---|
112 |
|
---|
113 | additionFrequency[attr].Value++;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|