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 System;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Optimization;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using System.Collections.Generic;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
32 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
33 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
34 | using HeuristicLab.Problems.VehicleRouting;
|
---|
35 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
36 |
|
---|
37 | namespace 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 | }
|
---|