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("ExchangeManipulator", "Exchange manipulation")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class ExchangeManipulator : PotvinManipulator {
|
---|
36 | [StorableConstructor]
|
---|
37 | private ExchangeManipulator(bool deserializing) : base(deserializing) { }
|
---|
38 | private ExchangeManipulator(ExchangeManipulator original, Cloner cloner)
|
---|
39 | : base(original, cloner) {
|
---|
40 | }
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new ExchangeManipulator(this, cloner);
|
---|
43 | }
|
---|
44 | public ExchangeManipulator()
|
---|
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 | do {
|
---|
61 | feasible = true;
|
---|
62 |
|
---|
63 | if (individual.Tours.Count > 1) {
|
---|
64 | int tour1Idx = random.Next(individual.Tours.Count);
|
---|
65 | int tour2Idx = random.Next(individual.Tours.Count - 1);
|
---|
66 | if (tour2Idx >= tour1Idx)
|
---|
67 | tour2Idx++;
|
---|
68 |
|
---|
69 | Tour tour1 = individual.Tours[tour1Idx];
|
---|
70 | Tour tour2 = individual.Tours[tour2Idx];
|
---|
71 |
|
---|
72 | int index1 = random.Next(tour1.Cities.Count);
|
---|
73 | int city1 = tour1.Cities[index1];
|
---|
74 |
|
---|
75 | int index2 = random.Next(tour2.Cities.Count);
|
---|
76 | int city2 = tour2.Cities[index2];
|
---|
77 |
|
---|
78 | if (!allowInfeasible) {
|
---|
79 | bool originalFeasible =
|
---|
80 | RouteFeasible(tour1, demand, capacity) &&
|
---|
81 | RouteFeasible(tour2, demand, capacity);
|
---|
82 |
|
---|
83 | if (originalFeasible) {
|
---|
84 | double routeLoad = 0;
|
---|
85 | for (int i = 0; i < tour1.Cities.Count; i++) {
|
---|
86 | if (i != index1)
|
---|
87 | routeLoad += demand[tour1.Cities[i]];
|
---|
88 | }
|
---|
89 | routeLoad += demand[city2];
|
---|
90 |
|
---|
91 | if (routeLoad > capacity.Value) {
|
---|
92 | feasible = false;
|
---|
93 | } else {
|
---|
94 | routeLoad = 0;
|
---|
95 | for (int i = 0; i < tour2.Cities.Count; i++) {
|
---|
96 | if (i != index2)
|
---|
97 | routeLoad += demand[tour2.Cities[i]];
|
---|
98 | }
|
---|
99 | routeLoad += demand[city1];
|
---|
100 |
|
---|
101 | if (routeLoad > capacity.Value) {
|
---|
102 | feasible = false;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (feasible) {
|
---|
110 | tour1.Cities.RemoveAt(index1);
|
---|
111 | tour1.Cities.Insert(index1, city2);
|
---|
112 |
|
---|
113 | tour2.Cities.RemoveAt(index2);
|
---|
114 | tour2.Cities.Insert(index2, city1);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | } while (!feasible);
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected override void Manipulate(IRandom random, PotvinEncoding individual) {
|
---|
121 | BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
|
---|
122 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
123 | DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
|
---|
124 | DoubleArray demand = DemandParameter.ActualValue;
|
---|
125 | DoubleValue capacity = CapacityParameter.ActualValue;
|
---|
126 |
|
---|
127 | bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
|
---|
128 | Apply(random, individual, demand, capacity, allowInfeasible);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|