Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/Moves/SimpleLocalSearch/StochasticSimpleLocalSearchSingleMoveGenerator.cs @ 4205

Last change on this file since 4205 was 4205, checked in by svonolfe, 14 years ago

Added MultiVRPMove (#1039)

File size: 3.7 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("StochasticSimpleLocalSearchSingleMoveGenerator", "Generates one random simple local search move from a given Alba VRP encoding.")]
33  [StorableClass]
34  public sealed class StochasticSimpleLocalSearchSingleMoveGenerator : SimpleLocalSearchMoveGenerator,
35    IStochasticOperator, ISingleMoveGenerator, IAlbaSimpleLocalSearchMoveOperator, IMultiVRPMoveGenerator {
36    #region IMultiVRPMoveOperator Members
37
38    public ILookupParameter VRPMoveParameter {
39      get { return (ILookupParameter)Parameters["SimpleLocalSearchMove"]; }
40    }
41
42    #endregion
43   
44    public ILookupParameter<IRandom> RandomParameter {
45      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
46    }
47   
48    [StorableConstructor]
49    private StochasticSimpleLocalSearchSingleMoveGenerator(bool deserializing) : base(deserializing) { }
50
51    public StochasticSimpleLocalSearchSingleMoveGenerator()
52      : base() {
53        Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
54    }
55
56    public static SimpleLocalSearchMove 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.Tours) {
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 (individual[currentTourEnd] < individual.Cities &&
78            currentTourEnd < individual.Length) {
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 SimpleLocalSearchMove(index1, index2, individual);
89    }
90
91    protected override SimpleLocalSearchMove[] GenerateMoves(AlbaEncoding individual) {
92      List<SimpleLocalSearchMove> moves = new List<SimpleLocalSearchMove>();
93
94      SimpleLocalSearchMove 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.