Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Operators/Moves/Shift/PotvinPDShiftMultiMoveGenerator.cs @ 8670

Last change on this file since 8670 was 8670, checked in by svonolfe, 12 years ago

Added first version of the dynamic vehicle routing addon (#1955)

File size: 4.6 KB
Line 
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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Optimization;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Parameters;
27using System.Collections.Generic;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29using HeuristicLab.Common;
30using HeuristicLab.Data;
31using HeuristicLab.Problems.VehicleRouting.Encodings.General;
32using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34using HeuristicLab.Problems.VehicleRouting;
35using HeuristicLab.Encodings.PermutationEncoding;
36
37namespace HeuristicLab.PDPSimulation.Operators {
38  [Item("DynPotvinPDShiftMultiMoveGenerator", "Generates shift moves from a given PDP encoding.")]
39  [StorableClass]
40  public sealed class DynPotvinPDShiftMultiMoveGenerator : DynPotvinPDShiftMoveGenerator, IMultiMoveGenerator, IMultiVRPMoveGenerator {
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
43    }
44
45    public IValueLookupParameter<IntValue> SampleSizeParameter {
46      get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new DynPotvinPDShiftMultiMoveGenerator(this, cloner);
51    }
52
53    [StorableConstructor]
54    private DynPotvinPDShiftMultiMoveGenerator(bool deserializing) : base(deserializing) { }
55
56    public DynPotvinPDShiftMultiMoveGenerator()
57      : base() {
58      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
59      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
60    }
61
62    private DynPotvinPDShiftMultiMoveGenerator(DynPotvinPDShiftMultiMoveGenerator original, Cloner cloner)
63      : base(original, cloner) {
64    }
65
66    protected override DynPotvinPDShiftMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
67      List<DynPotvinPDShiftMove> result = new List<DynPotvinPDShiftMove>();
68      IRandom rand = RandomParameter.ActualValue;
69      double samples = Math.Min(100, SampleSizeParameter.ActualValue.Value);
70
71      List<int> cities = new List<int>();
72      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
73      for (int i = 1; i <= individual.Cities; i++) {
74        if (pdp == null || pdp.GetDemand(i) >= 0 || pdp.GetPickupDeliveryLocation(i) <= 0 || pdp.GetPickupDeliveryLocation(i) == i)
75          cities.Add(i);
76      }
77
78      if (cities.Count >= 1) {
79        int max = individual.Tours.Count;
80        if (individual.Tours.Count >= problemInstance.Vehicles.Value)
81          max = max - 1;
82
83        if (max > 0) {
84          Dictionary<int, int> tours = new Dictionary<int, int>();
85          for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
86            for (int stop = 0; stop < individual.Tours[tourIdx].Stops.Count; stop++) {
87              int city = individual.Tours[tourIdx].Stops[stop];
88              tours[city] = tourIdx;
89            }
90          }
91
92          Permutation perm = new Permutation(PermutationTypes.Absolute, cities.Count * max);
93          perm.Randomize(rand);
94
95          samples = Math.Max(1, (int)(perm.Length * (samples / 100.0)));
96
97          for (int i = 0; i < Math.Min(perm.Length, samples); i++) {
98            int city = cities[perm[i] / max];
99            int oldTourIndex = tours[city];
100
101            int newTourIndex = perm[i] % max;
102            if (newTourIndex >= oldTourIndex)
103              newTourIndex++;
104
105            var move = new DynPotvinPDShiftMove(city, oldTourIndex, newTourIndex, individual);
106            result.Add(move);
107          }
108        }
109      }
110
111      return result.ToArray();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.