1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HEAL.Fossil;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
32 | [Item("PotvinCustomerRelocationMoveMaker", "Peforms the customer relocation move on a given VRP encoding and updates the quality.")]
|
---|
33 | [StorableType("B79B87F4-A39A-4618-BD0C-E1D40E821152")]
|
---|
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(StorableConstructorFlag _) : base(_) { }
|
---|
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 | }
|
---|