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("PotvinVehicleAssignmentSingleMoveGenerator", "Generates a single vehicle assignment move from a given VRP encoding.")]
|
---|
32 | [StorableClass]
|
---|
33 | public sealed class PotvinVehicleAssignmentSingleMoveGenerator : PotvinVehicleAssignmentMoveGenerator,
|
---|
34 | ISingleMoveGenerator {
|
---|
35 | #region IMultiVRPMoveOperator Members
|
---|
36 |
|
---|
37 | public override ILookupParameter VRPMoveParameter {
|
---|
38 | get { return (ILookupParameter)Parameters["PotvinVehicleAssignmentMove"]; }
|
---|
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 PotvinVehicleAssignmentSingleMoveGenerator(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | private PotvinVehicleAssignmentSingleMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
53 |
|
---|
54 | public PotvinVehicleAssignmentSingleMoveGenerator()
|
---|
55 | : base() {
|
---|
56 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
57 | }
|
---|
58 |
|
---|
59 | private PotvinVehicleAssignmentSingleMoveGenerator(PotvinVehicleAssignmentSingleMoveGenerator original, Cloner cloner)
|
---|
60 | : base(original, cloner) {
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static PotvinVehicleAssignmentMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) {
|
---|
64 | if (individual.Tours.Count > 1) {
|
---|
65 | int tour1 = rand.Next(individual.Tours.Count);
|
---|
66 | int tour2 = rand.Next(problemInstance.Vehicles.Value);
|
---|
67 |
|
---|
68 | while (tour2 == tour1)
|
---|
69 | tour2 = rand.Next(problemInstance.Vehicles.Value);
|
---|
70 |
|
---|
71 | return new PotvinVehicleAssignmentMove(tour1, tour2, individual);
|
---|
72 | } else {
|
---|
73 | return null;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override PotvinVehicleAssignmentMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
|
---|
78 | List<PotvinVehicleAssignmentMove> result = new List<PotvinVehicleAssignmentMove>();
|
---|
79 |
|
---|
80 | PotvinVehicleAssignmentMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue);
|
---|
81 | if (move != null)
|
---|
82 | result.Add(move);
|
---|
83 |
|
---|
84 | return result.ToArray();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|