1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Encodings;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
|
---|
33 | [Item("TwoOptStarManipulator", "Two opt star manipulation")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class TwoOptStarManipulator : PotvinManipulator {
|
---|
36 | [StorableConstructor]
|
---|
37 | private TwoOptStarManipulator(bool deserializing) : base(deserializing) { }
|
---|
38 | private TwoOptStarManipulator(TwoOptStarManipulator original, Cloner cloner)
|
---|
39 | : base(original, cloner) {
|
---|
40 | }
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new TwoOptStarManipulator(this, cloner);
|
---|
43 | }
|
---|
44 | public TwoOptStarManipulator()
|
---|
45 | : base() {
|
---|
46 | }
|
---|
47 |
|
---|
48 | private static bool RouteFeasible(Tour tour, DoubleArray demand, DoubleValue capacity) {
|
---|
49 | double routeLoad = 0;
|
---|
50 | for (int i = 0; i < tour.Cities.Count; i++) {
|
---|
51 | routeLoad += demand[tour.Cities[i]];
|
---|
52 | }
|
---|
53 |
|
---|
54 | return routeLoad <= capacity.Value;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static void Apply(IRandom random, PotvinEncoding individual, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) {
|
---|
58 | bool feasible;
|
---|
59 |
|
---|
60 | //consider creating new tour
|
---|
61 | individual.Tours.Add(new Tour());
|
---|
62 |
|
---|
63 | do {
|
---|
64 | feasible = true;
|
---|
65 |
|
---|
66 | int route1Idx = random.Next(individual.Tours.Count);
|
---|
67 | int route2Idx = random.Next(individual.Tours.Count - 1);
|
---|
68 | if (route2Idx >= route1Idx)
|
---|
69 | route2Idx++;
|
---|
70 |
|
---|
71 | Tour route1 = individual.Tours[route1Idx];
|
---|
72 | Tour route2 = individual.Tours[route2Idx];
|
---|
73 |
|
---|
74 | int x1 = random.Next(route1.Cities.Count + 1);
|
---|
75 | int x2 = random.Next(route2.Cities.Count + 1);
|
---|
76 |
|
---|
77 | if (!allowInfeasible) {
|
---|
78 | bool originalFeasible =
|
---|
79 | RouteFeasible(route1, demand, capacity) &&
|
---|
80 | RouteFeasible(route2, demand, capacity);
|
---|
81 |
|
---|
82 | if (originalFeasible) {
|
---|
83 | double routeLoad = 0;
|
---|
84 | for (int i = 0; i < x1; i++)
|
---|
85 | routeLoad += demand[route1.Cities[i]];
|
---|
86 | for (int i = x2; i < route2.Cities.Count; i++)
|
---|
87 | routeLoad += demand[route2.Cities[i]];
|
---|
88 |
|
---|
89 | if (routeLoad > capacity.Value) {
|
---|
90 | feasible = false;
|
---|
91 | } else {
|
---|
92 | routeLoad = 0;
|
---|
93 | for (int i = 0; i < x2; i++)
|
---|
94 | routeLoad += demand[route2.Cities[i]];
|
---|
95 | for (int i = x1; i < route1.Cities.Count; i++)
|
---|
96 | routeLoad += demand[route1.Cities[i]];
|
---|
97 |
|
---|
98 | if (routeLoad > capacity.Value) {
|
---|
99 | feasible = false;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (feasible) {
|
---|
106 | int count = route1.Cities.Count - x1;
|
---|
107 | List<int> segmentX1 = new List<int>();
|
---|
108 | if (count > 0) {
|
---|
109 | segmentX1 = route1.Cities.GetRange(x1, count);
|
---|
110 | route1.Cities.RemoveRange(x1, count);
|
---|
111 | }
|
---|
112 |
|
---|
113 | count = route2.Cities.Count - x2;
|
---|
114 | List<int> segmentX2 = new List<int>();
|
---|
115 | if (count > 0) {
|
---|
116 | segmentX2 = route2.Cities.GetRange(x2, count);
|
---|
117 | route2.Cities.RemoveRange(x2, count);
|
---|
118 | }
|
---|
119 |
|
---|
120 | route1.Cities.AddRange(segmentX2);
|
---|
121 | route2.Cities.AddRange(segmentX1);
|
---|
122 | }
|
---|
123 | } while (!feasible);
|
---|
124 |
|
---|
125 | individual.Tours.RemoveAll(t => t.Cities.Count == 0);
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected override void Manipulate(IRandom random, PotvinEncoding individual) {
|
---|
129 | BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
|
---|
130 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
131 | DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
|
---|
132 | DoubleArray demand = DemandParameter.ActualValue;
|
---|
133 | DoubleValue capacity = CapacityParameter.ActualValue;
|
---|
134 |
|
---|
135 | bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
|
---|
136 | Apply(random, individual, demand, capacity, allowInfeasible);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|