Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Operators/Moves/Rearrange/PotvinPDRearrangeSingleMoveGenerator.cs @ 8670

Last change on this file since 8670 was 8670, checked in by svonolfe, 12 years ago

Added first version of the dynamic vehicle routing addon (#1955)

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.Parameters;
27using System.Collections.Generic;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29using HeuristicLab.Common;
30using HeuristicLab.Data;
31using HeuristicLab.Problems.VehicleRouting.Encodings.General;
32using HeuristicLab.Problems.VehicleRouting.Variants;
33using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
34
35namespace HeuristicLab.PDPSimulation.Operators {
36  [Item("DynPotvinPDRearrangeSingleMoveGenerator", "Generates a single rearrange move from a given PDP encoding.")]
37  [StorableClass]
38  public sealed class DynPotvinPDRearrangeSingleMoveGenerator : DynPotvinPDRearrangeMoveGenerator,
39    ISingleMoveGenerator {   
40    public ILookupParameter<IRandom> RandomParameter {
41      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
42    }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new DynPotvinPDRearrangeSingleMoveGenerator(this, cloner);
46    }
47
48    [StorableConstructor]
49    private DynPotvinPDRearrangeSingleMoveGenerator(bool deserializing) : base(deserializing) { }
50
51    public DynPotvinPDRearrangeSingleMoveGenerator()
52      : base() {
53      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
54    }
55
56    private DynPotvinPDRearrangeSingleMoveGenerator(DynPotvinPDRearrangeSingleMoveGenerator original, Cloner cloner)
57      : base(original, cloner) {
58    }
59
60    public static DynPotvinPDRearrangeMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) {
61      List<int> cities = new List<int>();
62
63      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
64      for (int i = 1; i <= individual.Cities; i++) {
65        if (pdp == null || pdp.GetDemand(i) >= 0 || pdp.GetPickupDeliveryLocation(i) <= 0 || pdp.GetPickupDeliveryLocation(i) == i)
66          cities.Add(i);
67      }
68
69      if (cities.Count > 0) {
70        int city = cities[rand.Next(cities.Count)];
71        int tour = individual.Tours.FindIndex(t => t.Stops.Contains(city));
72        return new DynPotvinPDRearrangeMove(city, tour, individual);
73      } else {
74        return null;
75      }
76    }
77
78    protected override DynPotvinPDRearrangeMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) {
79      List<DynPotvinPDRearrangeMove> result = new List<DynPotvinPDRearrangeMove>();
80
81      DynPotvinPDRearrangeMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue);
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.