1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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("PotvinTwoOptStarSingleMoveGenerator", "Generates a single two opt star move from a given VRP encoding.")]
|
---|
32 | [StorableClass]
|
---|
33 | public sealed class PotvinTwoOptStarSingleMoveGenerator : PotvinTwoOptStarMoveGenerator,
|
---|
34 | ISingleMoveGenerator {
|
---|
35 | #region IMultiVRPMoveOperator Members
|
---|
36 |
|
---|
37 | public override ILookupParameter VRPMoveParameter {
|
---|
38 | get { return (ILookupParameter)Parameters["PotvinTwoOptStarMove"]; }
|
---|
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 PotvinTwoOptStarSingleMoveGenerator(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | private PotvinTwoOptStarSingleMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
53 |
|
---|
54 | public PotvinTwoOptStarSingleMoveGenerator()
|
---|
55 | : base() {
|
---|
56 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
57 | }
|
---|
58 |
|
---|
59 | private PotvinTwoOptStarSingleMoveGenerator(PotvinTwoOptStarSingleMoveGenerator original, Cloner cloner)
|
---|
60 | : base(original, cloner) {
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static PotvinTwoOptStarMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) {
|
---|
64 | int route1Idx = rand.Next(individual.Tours.Count);
|
---|
65 | int route2Idx = rand.Next(individual.Tours.Count - 1);
|
---|
66 | if (route2Idx >= route1Idx)
|
---|
67 | route2Idx++;
|
---|
68 |
|
---|
69 | Tour route1 = individual.Tours[route1Idx];
|
---|
70 | Tour route2 = individual.Tours[route2Idx];
|
---|
71 |
|
---|
72 | int x1 = rand.Next(route1.Stops.Count + 1);
|
---|
73 | int x2 = rand.Next(route2.Stops.Count + 1);
|
---|
74 |
|
---|
75 | return new PotvinTwoOptStarMove(route1Idx, x1, route2Idx, x2, individual);
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected override PotvinTwoOptStarMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
|
---|
79 | List<PotvinTwoOptStarMove> result = new List<PotvinTwoOptStarMove>();
|
---|
80 |
|
---|
81 | PotvinTwoOptStarMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue);
|
---|
82 | if (move != null)
|
---|
83 | result.Add(move);
|
---|
84 |
|
---|
85 | return result.ToArray();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|