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 HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
24 | using HeuristicLab.Parameters;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
33 | [Item("PotvinManipulator", "A VRP manipulation operation.")]
|
---|
34 | [StorableClass]
|
---|
35 | public abstract class PotvinManipulator : VRPManipulator, IStochasticOperator, IPotvinOperator {
|
---|
36 | public ILookupParameter<IRandom> RandomParameter {
|
---|
37 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public IValueParameter<BoolValue> AllowInfeasibleSolutions {
|
---|
41 | get { return (IValueParameter<BoolValue>)Parameters["AllowInfeasibleSolutions"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | protected PotvinManipulator(bool deserializing) : base(deserializing) { }
|
---|
46 |
|
---|
47 | public PotvinManipulator() {
|
---|
48 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
|
---|
49 | Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false)));
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected PotvinManipulator(PotvinManipulator original, Cloner cloner)
|
---|
53 | : base(original, cloner) {
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected abstract void Manipulate(IRandom random, PotvinEncoding individual);
|
---|
57 |
|
---|
58 | protected int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) {
|
---|
59 | int tourIndex = -1;
|
---|
60 |
|
---|
61 | double sum = 0.0;
|
---|
62 | double[] probabilities = new double[individual.Tours.Count];
|
---|
63 | for (int i = 0; i < individual.Tours.Count; i++) {
|
---|
64 | probabilities[i] = 1.0 / ((double)individual.Tours[i].Stops.Count / (double)ProblemInstance.Cities.Value);
|
---|
65 | sum += probabilities[i];
|
---|
66 | }
|
---|
67 |
|
---|
68 | for (int i = 0; i < probabilities.Length; i++)
|
---|
69 | probabilities[i] = probabilities[i] / sum;
|
---|
70 |
|
---|
71 | double rand = random.NextDouble();
|
---|
72 | double cumulatedProbabilities = 0.0;
|
---|
73 | int index = 0;
|
---|
74 | while (tourIndex == -1 && index < probabilities.Length) {
|
---|
75 | if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
|
---|
76 | tourIndex = index;
|
---|
77 |
|
---|
78 | cumulatedProbabilities += probabilities[index];
|
---|
79 | index++;
|
---|
80 | }
|
---|
81 |
|
---|
82 | return tourIndex;
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected bool FindInsertionPlace(PotvinEncoding individual, int city, int routeToAvoid, bool allowInfeasible, out int route, out int place) {
|
---|
86 | return individual.FindInsertionPlace(
|
---|
87 | city, routeToAvoid, allowInfeasible, out route, out place);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override IOperation Apply() {
|
---|
91 | IVRPEncoding solution = VRPToursParameter.ActualValue;
|
---|
92 | if (!(solution is PotvinEncoding)) {
|
---|
93 | VRPToursParameter.ActualValue = PotvinEncoding.ConvertFrom(solution, ProblemInstance);
|
---|
94 | }
|
---|
95 |
|
---|
96 | Manipulate(RandomParameter.ActualValue, VRPToursParameter.ActualValue as PotvinEncoding);
|
---|
97 | (VRPToursParameter.ActualValue as PotvinEncoding).Repair();
|
---|
98 |
|
---|
99 | return base.Apply();
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|