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.Problems.VehicleRouting.Encodings.Alba;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
33 | [Item("AlbaStochasticIntraRouteInversionSingleMoveGenerator", "Generates one random intra route inversion move from a given VRP encoding. It is implemented as described in Alba, E. and Dorronsoro, B. (2004). Solving the Vehicle Routing Problem by Using Cellular Genetic Algorithms.")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class AlbaStochasticIntraRouteInversionSingleMoveGenerator : AlbaIntraRouteInversionMoveGenerator,
|
---|
36 | IStochasticOperator, ISingleMoveGenerator, IAlbaIntraRouteInversionMoveOperator, IMultiVRPMoveGenerator {
|
---|
37 | #region IMultiVRPMoveOperator Members
|
---|
38 |
|
---|
39 | public override ILookupParameter VRPMoveParameter {
|
---|
40 | get { return (ILookupParameter)Parameters["AlbaIntraRouteInversionMove"]; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | public ILookupParameter<IRandom> RandomParameter {
|
---|
46 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | private AlbaStochasticIntraRouteInversionSingleMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
51 |
|
---|
52 | public AlbaStochasticIntraRouteInversionSingleMoveGenerator()
|
---|
53 | : base() {
|
---|
54 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static AlbaIntraRouteInversionMove Apply(AlbaEncoding individual, int cities, IRandom rand) {
|
---|
58 | int index1 = -1;
|
---|
59 | int index2 = -1;
|
---|
60 |
|
---|
61 | List<Tour> validTours = new List<Tour>();
|
---|
62 | foreach (Tour tour in individual.GetTours()) {
|
---|
63 | if (tour.Stops.Count >= 4)
|
---|
64 | validTours.Add(tour);
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (validTours.Count > 0) {
|
---|
68 | Tour chosenTour = validTours[rand.Next(validTours.Count)];
|
---|
69 | int currentTourStart = -1;
|
---|
70 | for (int i = 0; i < individual.Length; i++) {
|
---|
71 | if (individual[i] + 1 == chosenTour.Stops[0]) {
|
---|
72 | currentTourStart = i;
|
---|
73 | break;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | int currentTourEnd = currentTourStart;
|
---|
78 | while (currentTourEnd < individual.Length &&
|
---|
79 | individual[currentTourEnd] < cities) {
|
---|
80 | currentTourEnd++;
|
---|
81 | }
|
---|
82 |
|
---|
83 | int tourLength = currentTourEnd - currentTourStart;
|
---|
84 | int a = rand.Next(tourLength - 3);
|
---|
85 | index1 = currentTourStart + a;
|
---|
86 | index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
|
---|
87 | }
|
---|
88 |
|
---|
89 | return new AlbaIntraRouteInversionMove(index1, index2, individual);
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected override AlbaIntraRouteInversionMove[] GenerateMoves(AlbaEncoding individual, IVRPProblemInstance problemInstance) {
|
---|
93 | List<AlbaIntraRouteInversionMove> moves = new List<AlbaIntraRouteInversionMove>();
|
---|
94 |
|
---|
95 | AlbaIntraRouteInversionMove move = Apply(individual, problemInstance.Cities.Value, RandomParameter.ActualValue);
|
---|
96 | if(move != null)
|
---|
97 | moves.Add(move);
|
---|
98 |
|
---|
99 | return moves.ToArray();
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|