Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added operations for the Alba encoding (#1039)

File size: 3.5 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;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
31  [Item("StochasticSimpleLocalSearchSingleMoveGenerator", "Generates one random simple local search move from a given Alba VRP encoding.")]
32  [StorableClass]
33  public sealed class StochasticSimpleLocalSearchSingleMoveGenerator : SimpleLocalSearchMoveGenerator, IStochasticOperator, ISingleMoveGenerator, IAlbaSimpleLocalSearchMoveOperator {
34    public ILookupParameter<IRandom> RandomParameter {
35      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
36    }
37   
38    [StorableConstructor]
39    private StochasticSimpleLocalSearchSingleMoveGenerator(bool deserializing) : base(deserializing) { }
40
41    public StochasticSimpleLocalSearchSingleMoveGenerator()
42      : base() {
43        Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
44    }
45
46    public static SimpleLocalSearchMove Apply(AlbaEncoding individual, int cities, IRandom rand) {
47      int index1 = -1;
48      int index2 = -1;
49
50      List<Tour> validTours = new List<Tour>();
51      foreach (Tour tour in individual.Tours) {
52        if (tour.Cities.Count >= 4)
53          validTours.Add(tour);
54      }
55
56      if (validTours.Count > 0) {
57        Tour chosenTour = validTours[rand.Next(validTours.Count)];
58        int currentTourStart = -1;
59        for (int i = 0; i < individual.Length; i++) {
60          if (individual[i] + 1 == chosenTour.Cities[0]) {
61            currentTourStart = i;
62            break;
63          }
64        }
65
66        int currentTourEnd = currentTourStart;
67        while (individual[currentTourEnd] < individual.Cities &&
68            currentTourEnd < individual.Length) {
69          currentTourEnd++;
70        }
71
72        int tourLength = currentTourEnd - currentTourStart;
73        int a = rand.Next(tourLength - 3);
74        index1 = currentTourStart + a;
75        index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
76      }
77
78      return new SimpleLocalSearchMove(index1, index2, individual);
79    }
80
81    protected override SimpleLocalSearchMove[] GenerateMoves(AlbaEncoding individual) {
82      List<SimpleLocalSearchMove> moves = new List<SimpleLocalSearchMove>();
83
84      SimpleLocalSearchMove move = Apply(individual, Cities, RandomParameter.ActualValue);
85      if(move != null)
86        moves.Add(move);
87
88      return moves.ToArray();
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.