Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/TwoOptStarManipulator.cs @ 7145

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

Implemented manipulation operators for FLA (#1703)

File size: 5.1 KB
Line 
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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
29using HeuristicLab.Problems.VehicleRouting.Encodings;
30using HeuristicLab.Problems.VehicleRouting;
31
32namespace HeuristicLab.Anaylsis.FitnessLandscape.VRP {
33  [Item("TwoOptStarMainpulator", "Two opt star manipulation")]
34  [StorableClass]
35  public sealed class TwoOptStarMainpulator : PotvinManipulator {
36    [StorableConstructor]
37    private TwoOptStarMainpulator(bool deserializing) : base(deserializing) { }
38    private TwoOptStarMainpulator(TwoOptStarMainpulator original, Cloner cloner)
39      : base(original, cloner) {
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new TwoOptStarMainpulator(this, cloner);
43    }
44    public TwoOptStarMainpulator()
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 route1Idx = random.Next(individual.Tours.Count);
65          int route2Idx = random.Next(individual.Tours.Count - 1);
66          if (route2Idx >= route1Idx)
67            route2Idx++;
68
69          Tour route1 = individual.Tours[route1Idx];
70          Tour route2 = individual.Tours[route2Idx];
71
72          int x1 = random.Next(route1.Cities.Count + 1);
73          int x2 = random.Next(route2.Cities.Count + 1);
74
75          if (!allowInfeasible) {
76            bool originalFeasible =
77              RouteFeasible(route1, demand, capacity) &&
78              RouteFeasible(route2, demand, capacity);
79
80            if (originalFeasible) {
81              double routeLoad = 0;
82              for (int i = 0; i < x1; i++)
83                routeLoad += demand[route1.Cities[i]];
84              for (int i = x2; i < route2.Cities.Count; i++)
85                routeLoad += demand[route2.Cities[i]];
86
87              if (routeLoad > capacity.Value) {
88                feasible = false;
89              } else {
90                routeLoad = 0;
91                for (int i = 0; i < x2; i++)
92                  routeLoad += demand[route2.Cities[i]];
93                for (int i = x1; i < route1.Cities.Count; i++)
94                  routeLoad += demand[route1.Cities[i]];
95
96                if (routeLoad > capacity.Value) {
97                  feasible = false;
98                }
99              }
100            }
101          }
102
103          if (feasible) {
104            int count = route1.Cities.Count - x1;
105            List<int> segmentX1 = new List<int>();
106            if (count > 0) {
107              segmentX1 = route1.Cities.GetRange(x1, count);
108              route1.Cities.RemoveRange(x1, count);
109            }
110
111            count = route2.Cities.Count - x2;
112            List<int> segmentX2 = new List<int>();
113            if (count > 0) {
114              segmentX2 = route2.Cities.GetRange(x2, count);
115              route2.Cities.RemoveRange(x2, count);
116            }
117
118            route1.Cities.AddRange(segmentX2);
119            route2.Cities.AddRange(segmentX1);
120
121            individual.Tours.RemoveAll(t => t.Cities.Count == 0);
122          }
123        }
124      } while (!feasible);
125    }
126
127    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
128      BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
129      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
130      DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
131      DoubleArray demand = DemandParameter.ActualValue;
132      DoubleValue capacity = CapacityParameter.ActualValue;
133
134      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
135      Apply(random, individual, demand, capacity, allowInfeasible);
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.