Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8694 was 7159, checked in by epitzer, 13 years ago

#1703 correct some typos

File size: 5.0 KB
RevLine 
[7145]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
[7159]32namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
33  [Item("TwoOptStarManipulator", "Two opt star manipulation")]
[7145]34  [StorableClass]
[7159]35  public sealed class TwoOptStarManipulator : PotvinManipulator {
[7145]36    [StorableConstructor]
[7159]37    private TwoOptStarManipulator(bool deserializing) : base(deserializing) { }
38    private TwoOptStarManipulator(TwoOptStarManipulator original, Cloner cloner)
[7145]39      : base(original, cloner) {
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
[7159]42      return new TwoOptStarManipulator(this, cloner);
[7145]43    }
[7159]44    public TwoOptStarManipulator()
[7145]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
[7151]60      //consider creating new tour
61      individual.Tours.Add(new Tour());
62
[7145]63      do {
64        feasible = true;
65
[7151]66        int route1Idx = random.Next(individual.Tours.Count);
67        int route2Idx = random.Next(individual.Tours.Count - 1);
68        if (route2Idx >= route1Idx)
69          route2Idx++;
[7145]70
[7151]71        Tour route1 = individual.Tours[route1Idx];
72        Tour route2 = individual.Tours[route2Idx];
[7145]73
[7151]74        int x1 = random.Next(route1.Cities.Count + 1);
75        int x2 = random.Next(route2.Cities.Count + 1);
[7145]76
[7151]77        if (!allowInfeasible) {
78          bool originalFeasible =
79            RouteFeasible(route1, demand, capacity) &&
80            RouteFeasible(route2, demand, capacity);
[7145]81
[7151]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++)
[7145]96                routeLoad += demand[route1.Cities[i]];
97
98              if (routeLoad > capacity.Value) {
99                feasible = false;
100              }
101            }
102          }
[7151]103        }
[7145]104
[7151]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          }
[7145]112
[7151]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          }
[7145]119
[7151]120          route1.Cities.AddRange(segmentX2);
121          route2.Cities.AddRange(segmentX1);
[7145]122        }
123      } while (!feasible);
[7151]124
125      individual.Tours.RemoveAll(t => t.Cities.Count == 0);
[7145]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}
Note: See TracBrowser for help on using the repository browser.