Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/CustomerRelocation/PotvinCustomerRelocationMoveMaker.cs

Last change on this file was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 5.0 KB
RevLine 
[5127]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5127]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
[8053]22using HeuristicLab.Common;
[5127]23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[8053]28using HeuristicLab.Problems.VehicleRouting.Interfaces;
[5127]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 {
[8053]40      get { return CustomerRelocationMoveParameter; }
[5127]41    }
42
[5202]43    public ILookupParameter<VariableCollection> MemoriesParameter {
[8053]44      get { return (ILookupParameter<VariableCollection>)Parameters["Memories"]; }
[5127]45    }
46
[5202]47    public IValueParameter<StringValue> AdditionFrequencyMemoryKeyParameter {
48      get { return (IValueParameter<StringValue>)Parameters["AdditionFrequencyMemoryKey"]; }
[5127]49    }
50
51    [StorableConstructor]
[7906]52    protected PotvinCustomerRelocationMoveMaker(bool deserializing) : base(deserializing) { }
[5127]53
54    public PotvinCustomerRelocationMoveMaker()
55      : base() {
56      Parameters.Add(new LookupParameter<PotvinCustomerRelocationMove>("PotvinCustomerRelocationMove", "The moves that should be made."));
57
[5202]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")));
[5127]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
[6751]70    public static void Apply(PotvinEncoding solution, PotvinCustomerRelocationMove move, IVRPProblemInstance problemInstance) {
[5127]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);
[7790]82
83      solution.Repair();
[5127]84    }
85
86    protected override void PerformMove() {
87      PotvinCustomerRelocationMove move = CustomerRelocationMoveParameter.ActualValue;
88
[5867]89      PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
[6751]90      Apply(newSolution, move, ProblemInstance);
[6857]91      newSolution.Repair();
[5127]92      VRPToursParameter.ActualValue = newSolution;
93
94      //reset move quality
[6607]95      VRPEvaluation eval = ProblemInstance.Evaluate(newSolution);
[5127]96      MoveQualityParameter.ActualValue.Value = eval.Quality;
97
98      //update memory
[5202]99      VariableCollection memory = MemoriesParameter.ActualValue;
100      string key = AdditionFrequencyMemoryKeyParameter.Value.Value;
101
[5867]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>;
[5202]109
[5867]110        PotvinCustomerRelocationMoveAttribute attr = new PotvinCustomerRelocationMoveAttribute(0, move.Tour, move.City);
111        if (!additionFrequency.ContainsKey(attr))
112          additionFrequency[attr] = new IntValue(0);
[5127]113
[5867]114        additionFrequency[attr].Value++;
115      }
[5127]116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.