Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinManipulator.cs @ 4174

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

Refactored VRP, added some Potvin operators (WIP) (#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 HeuristicLab.Core;
23using HeuristicLab.Encodings.PermutationEncoding;
24using HeuristicLab.Parameters;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
30  [StorableClass]
31  public abstract class PotvinManipulator : VRPManipulator, IStochasticOperator {
32    public ILookupParameter<IRandom> RandomParameter {
33      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
34    }
35
36    public PotvinManipulator() {
37      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
38    }
39
40    protected abstract void Manipulate(IRandom random, PotvinEncoding individual);
41
42    protected int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) {
43      int tourIndex = -1;
44
45      double sum = 0.0;
46      double[] probabilities = new double[individual.Tours.Count];
47      for (int i = 0; i < individual.Tours.Count; i++) {
48        probabilities[i] = 1.0 / ((double)individual.Tours[i].Cities.Count / (double)Cities);
49        sum += probabilities[i];
50      }
51
52      for (int i = 0; i < probabilities.Length; i++)
53        probabilities[i] = probabilities[i] / sum;
54
55      double rand = random.NextDouble();
56      double cumulatedProbabilities = 0.0;
57      int index = 0;
58      while (tourIndex == -1 && index < probabilities.Length) {
59        if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
60          tourIndex = index;
61
62        cumulatedProbabilities += probabilities[index];
63        index++;
64      }
65
66      return tourIndex;
67    }
68
69    protected bool FindInsertionPlace(PotvinEncoding individual, int city, int routeToAvoid, out int route, out int place) {
70      return individual.FindInsertionPlace(
71        DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue, ReadyTimeParameter.ActualValue,
72        DemandParameter.ActualValue, CapacityParameter.ActualValue, CoordinatesParameter.ActualValue,
73        DistanceMatrixParameter, UseDistanceMatrixParameter.ActualValue,
74        city, routeToAvoid, out route, out place);
75    }
76   
77    public override IOperation Apply() {
78      IVRPEncoding solution = VRPSolutionParameter.ActualValue;
79      if (!(solution is PotvinEncoding)) {
80        VRPSolutionParameter.ActualValue = PotvinEncoding.ConvertFrom(solution);
81      }
82     
83      Manipulate(RandomParameter.ActualValue, VRPSolutionParameter.ActualValue as PotvinEncoding);
84
85      return base.Apply();
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.