1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
31 | [Item("PotvinCustomerRelocationSingleMoveGenerator", "Generates a single customer relocation move from a given VRP encoding.")]
|
---|
32 | [StorableClass]
|
---|
33 | public sealed class PotvinCustomerRelocationSingleMoveGenerator : PotvinCustomerRelocationMoveGenerator,
|
---|
34 | ISingleMoveGenerator {
|
---|
35 | #region IMultiVRPMoveOperator Members
|
---|
36 |
|
---|
37 | public override ILookupParameter VRPMoveParameter {
|
---|
38 | get { return (ILookupParameter)Parameters["PotvinCustomerRelocationMove"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | #endregion
|
---|
42 |
|
---|
43 | public ILookupParameter<IRandom> RandomParameter {
|
---|
44 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
48 | return new PotvinCustomerRelocationSingleMoveGenerator(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | private PotvinCustomerRelocationSingleMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
53 |
|
---|
54 | public PotvinCustomerRelocationSingleMoveGenerator()
|
---|
55 | : base() {
|
---|
56 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
57 | }
|
---|
58 |
|
---|
59 | private PotvinCustomerRelocationSingleMoveGenerator(PotvinCustomerRelocationSingleMoveGenerator original, Cloner cloner)
|
---|
60 | : base(original, cloner) {
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static PotvinCustomerRelocationMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) {
|
---|
64 | int city = 1 + rand.Next(individual.Cities);
|
---|
65 | Tour oldTour = individual.Tours.Find(t => t.Stops.Contains(city));
|
---|
66 | int oldTourIndex = individual.Tours.IndexOf(oldTour);
|
---|
67 |
|
---|
68 | int max = individual.Tours.Count;
|
---|
69 | if (individual.Tours.Count < problemInstance.Vehicles.Value)
|
---|
70 | max = max - 1;
|
---|
71 |
|
---|
72 | int newTourIndex = rand.Next(max);
|
---|
73 | if (newTourIndex >= oldTourIndex)
|
---|
74 | newTourIndex++;
|
---|
75 |
|
---|
76 | return new PotvinCustomerRelocationMove(city, oldTourIndex, newTourIndex, individual);
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected override PotvinCustomerRelocationMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
|
---|
80 | List<PotvinCustomerRelocationMove> result = new List<PotvinCustomerRelocationMove>();
|
---|
81 |
|
---|
82 | PotvinCustomerRelocationMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue);
|
---|
83 | if (move != null)
|
---|
84 | result.Add(move);
|
---|
85 |
|
---|
86 | return result.ToArray();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|