[4204] | 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;
|
---|
[4205] | 29 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
[4204] | 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
[4206] | 32 | [Item("StochasticIntraRouteInversionSingleMoveGenerator", "Generates one random intra route inversion move from a given VRP encoding.")]
|
---|
[4204] | 33 | [StorableClass]
|
---|
[4206] | 34 | public sealed class StochasticIntraRouteInversionSingleMoveGenerator : IntraRouteInversionMoveGenerator,
|
---|
| 35 | IStochasticOperator, ISingleMoveGenerator, IAlbaIntraRouteInversionMoveOperator, IMultiVRPMoveGenerator {
|
---|
[4205] | 36 | #region IMultiVRPMoveOperator Members
|
---|
| 37 |
|
---|
| 38 | public ILookupParameter VRPMoveParameter {
|
---|
[4206] | 39 | get { return (ILookupParameter)Parameters["IntraRouteInversionMove"]; }
|
---|
[4205] | 40 | }
|
---|
| 41 |
|
---|
| 42 | #endregion
|
---|
| 43 |
|
---|
[4204] | 44 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 45 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | [StorableConstructor]
|
---|
[4206] | 49 | private StochasticIntraRouteInversionSingleMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
[4204] | 50 |
|
---|
[4206] | 51 | public StochasticIntraRouteInversionSingleMoveGenerator()
|
---|
[4204] | 52 | : base() {
|
---|
| 53 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[4206] | 56 | public static IntraRouteInversionMove Apply(AlbaEncoding individual, int cities, IRandom rand) {
|
---|
[4204] | 57 | int index1 = -1;
|
---|
| 58 | int index2 = -1;
|
---|
| 59 |
|
---|
| 60 | List<Tour> validTours = new List<Tour>();
|
---|
[4230] | 61 | foreach (Tour tour in individual.GetTours()) {
|
---|
[4204] | 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;
|
---|
[4330] | 77 | while (currentTourEnd < individual.Length &&
|
---|
| 78 | individual[currentTourEnd] < individual.Cities) {
|
---|
[4204] | 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 |
|
---|
[4206] | 88 | return new IntraRouteInversionMove(index1, index2, individual);
|
---|
[4204] | 89 | }
|
---|
| 90 |
|
---|
[4206] | 91 | protected override IntraRouteInversionMove[] GenerateMoves(AlbaEncoding individual) {
|
---|
| 92 | List<IntraRouteInversionMove> moves = new List<IntraRouteInversionMove>();
|
---|
[4204] | 93 |
|
---|
[4206] | 94 | IntraRouteInversionMove move = Apply(individual, Cities, RandomParameter.ActualValue);
|
---|
[4204] | 95 | if(move != null)
|
---|
| 96 | moves.Add(move);
|
---|
| 97 |
|
---|
| 98 | return moves.ToArray();
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|