Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/RelocateManipulator.cs @ 8191

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

#1703 correct some typos

File size: 4.5 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("RelocateManipulator", "Relocate manipulation")]
[7145]34  [StorableClass]
[7159]35  public sealed class RelocateManipulator : PotvinManipulator {
[7145]36    [StorableConstructor]
[7159]37    private RelocateManipulator(bool deserializing) : base(deserializing) { }
38    private RelocateManipulator(RelocateManipulator original, Cloner cloner)
[7145]39      : base(original, cloner) {
40    }
41    public override IDeepCloneable Clone(Cloner cloner) {
[7159]42      return new RelocateManipulator(this, cloner);
[7145]43    }
[7159]44    public RelocateManipulator()
[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
60      do {
61        feasible = true;
62
63        int cities = individual.Cities;
64        int city = 1 + random.Next(cities);
65        Tour originalTour = individual.Tours.Find(t => t.Cities.Contains(city));
66        //consider creating new route
67        individual.Tours.Add(new Tour());
68
69        int position = 1 + random.Next(cities + individual.Tours.Count - 1);
70        if (position >= city) {
71          position++;
72        }
73
74        int originalPosition = originalTour.Cities.IndexOf(city);
75        originalTour.Cities.RemoveAt(originalPosition);
76
77        Tour insertionTour;
78        int insertionPosition;
79        if (position <= cities) {
80          insertionTour = individual.Tours.Find(t => t.Cities.Contains(position));
81          insertionPosition = insertionTour.Cities.IndexOf(position) + 1;
82        } else {
83          insertionTour = individual.Tours[position - cities - 1];
84          insertionPosition = 0;
85        }
86
87        if (!allowInfeasible && insertionTour != originalTour) {
88          bool originalFeasible = RouteFeasible(insertionTour, demand, capacity);
89
90          if (originalFeasible) {
91            double routeLoad = 0;
92            for (int i = 0; i < insertionTour.Cities.Count; i++) {
93              routeLoad += demand[insertionTour.Cities[i]];
94            }
95            routeLoad += demand[city];
96
97            feasible = routeLoad <= capacity.Value;
98          }
99        }
100
101        if (feasible) {
102          insertionTour.Cities.Insert(insertionPosition, city);
103        } else {
104          originalTour.Cities.Insert(originalPosition, city);
105        }
106
107        individual.Tours.RemoveAll(t => t.Cities.Count == 0);
108      } while (!feasible);
109    }
110
111    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
112      BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
113      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
114      DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
115      DoubleArray demand = DemandParameter.ActualValue;
116      DoubleValue capacity = CapacityParameter.ActualValue;
117
118      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
119      Apply(random, individual, demand, capacity, allowInfeasible);
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.