Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/TwoOptStar/PotvinTwoOptStarSingleMoveGenerator.cs @ 12012

Last change on this file since 12012 was 12012, checked in by ascheibe, 9 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.3 KB
RevLine 
[7689]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7689]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
[8053]22using System.Collections.Generic;
23using HeuristicLab.Common;
[7689]24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
[8053]26using HeuristicLab.Parameters;
[7689]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
[7865]31  [Item("PotvinTwoOptStarSingleMoveGenerator", "Generates a single two opt star move from a given VRP encoding.")]
[7689]32  [StorableClass]
[7865]33  public sealed class PotvinTwoOptStarSingleMoveGenerator : PotvinTwoOptStarMoveGenerator,
[7689]34    ISingleMoveGenerator {
35    #region IMultiVRPMoveOperator Members
36
37    public override ILookupParameter VRPMoveParameter {
[7865]38      get { return (ILookupParameter)Parameters["PotvinTwoOptStarMove"]; }
[7689]39    }
40
41    #endregion
[8053]42
[7689]43    public ILookupParameter<IRandom> RandomParameter {
44      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
[7865]48      return new PotvinTwoOptStarSingleMoveGenerator(this, cloner);
[7689]49    }
50
51    [StorableConstructor]
[7865]52    private PotvinTwoOptStarSingleMoveGenerator(bool deserializing) : base(deserializing) { }
[7689]53
[7865]54    public PotvinTwoOptStarSingleMoveGenerator()
[7689]55      : base() {
56      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
57    }
58
[7906]59    private PotvinTwoOptStarSingleMoveGenerator(PotvinTwoOptStarSingleMoveGenerator original, Cloner cloner)
[7689]60      : base(original, cloner) {
61    }
62
[7865]63    public static PotvinTwoOptStarMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) {
[7689]64      int route1Idx = rand.Next(individual.Tours.Count);
65      int route2Idx = rand.Next(individual.Tours.Count - 1);
66      if (route2Idx >= route1Idx)
67        route2Idx++;
68
69      Tour route1 = individual.Tours[route1Idx];
70      Tour route2 = individual.Tours[route2Idx];
71
72      int x1 = rand.Next(route1.Stops.Count + 1);
73      int x2 = rand.Next(route2.Stops.Count + 1);
74
[7865]75      return new PotvinTwoOptStarMove(route1Idx, x1, route2Idx, x2, individual);
[7689]76    }
77
[7865]78    protected override PotvinTwoOptStarMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
79      List<PotvinTwoOptStarMove> result = new List<PotvinTwoOptStarMove>();
[7689]80
[7865]81      PotvinTwoOptStarMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue);
[7689]82      if (move != null)
83        result.Add(move);
84
85      return result.ToArray();
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.