source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/Moves/IntraRouteInversion/StochasticIntraRouteInversionSingleMoveGenerator.cs @ 4330

Last change on this file since 4330 was 4330, checked in by svonolfe, 9 years ago

Fixed a small bug in the route inversion move generator (#1039)

File size: 3.8 KB
Line 
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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Optimization;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
27using HeuristicLab.Parameters;
28using System.Collections.Generic;
29using HeuristicLab.Problems.VehicleRouting.Encodings.General;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
32  [Item("StochasticIntraRouteInversionSingleMoveGenerator", "Generates one random intra route inversion move from a given VRP encoding.")]
33  [StorableClass]
34  public sealed class StochasticIntraRouteInversionSingleMoveGenerator : IntraRouteInversionMoveGenerator,
35    IStochasticOperator, ISingleMoveGenerator, IAlbaIntraRouteInversionMoveOperator, IMultiVRPMoveGenerator {
36    #region IMultiVRPMoveOperator Members
37
38    public ILookupParameter VRPMoveParameter {
39      get { return (ILookupParameter)Parameters["IntraRouteInversionMove"]; }
40    }
41
42    #endregion
43   
44    public ILookupParameter<IRandom> RandomParameter {
45      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
46    }
47   
48    [StorableConstructor]
49    private StochasticIntraRouteInversionSingleMoveGenerator(bool deserializing) : base(deserializing) { }
50
51    public StochasticIntraRouteInversionSingleMoveGenerator()
52      : base() {
53        Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
54    }
55
56    public static IntraRouteInversionMove Apply(AlbaEncoding individual, int cities, IRandom rand) {
57      int index1 = -1;
58      int index2 = -1;
59
60      List<Tour> validTours = new List<Tour>();
61      foreach (Tour tour in individual.GetTours()) {
62        if (tour.Cities.Count >= 4)
63          validTours.Add(tour);
64      }
65
66      if (validTours.Count > 0) {
67        Tour chosenTour = validTours[rand.Next(validTours.Count)];
68        int currentTourStart = -1;
69        for (int i = 0; i < individual.Length; i++) {
70          if (individual[i] + 1 == chosenTour.Cities[0]) {
71            currentTourStart = i;
72            break;
73          }
74        }
75
76        int currentTourEnd = currentTourStart;
77        while (currentTourEnd < individual.Length && 
78          individual[currentTourEnd] < individual.Cities) {
79          currentTourEnd++;
80        }
81
82        int tourLength = currentTourEnd - currentTourStart;
83        int a = rand.Next(tourLength - 3);
84        index1 = currentTourStart + a;
85        index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
86      }
87
88      return new IntraRouteInversionMove(index1, index2, individual);
89    }
90
91    protected override IntraRouteInversionMove[] GenerateMoves(AlbaEncoding individual) {
92      List<IntraRouteInversionMove> moves = new List<IntraRouteInversionMove>();
93
94      IntraRouteInversionMove move = Apply(individual, Cities, RandomParameter.ActualValue);
95      if(move != null)
96        moves.Add(move);
97
98      return moves.ToArray();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.