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 | 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 | }
|
---|