Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/TwoOptManipulator.cs @ 15465

Last change on this file since 15465 was 9836, checked in by epitzer, 11 years ago

Fix VRP FLA manipulators and adapt to VRP-3.4 (#1703)

File size: 3.5 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;
31using System;
32using HeuristicLab.Problems.VehicleRouting.Interfaces;
33
34namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
35  [Item("TwoOptManipulator", "Two opt manipulation")]
36  [StorableClass]
37  public sealed class TwoOptManipulator : PotvinManipulator {
38    [StorableConstructor]
39    private TwoOptManipulator(bool deserializing) : base(deserializing) { }
40    private TwoOptManipulator(TwoOptManipulator original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new TwoOptManipulator(this, cloner);
45    }
46    public TwoOptManipulator()
47      : base() {
48    }
49
50    public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, bool allowInfeasible) {
51      List<Tour> tours = individual.Tours.FindAll(t => t.Stops.Count >= 2);
52      if (tours.Count == 0) return;
53
54      bool feasible;
55      int count = 0;
56
57      do {
58        feasible = true;
59        int tourIdx = random.Next(tours.Count);
60        Tour tour = tours[tourIdx];
61
62        int first;
63        int length;
64        if (tour.Stops.Count == 2) {
65          first = 0;
66          length = 2;
67        } else {
68          first = random.Next(tour.Stops.Count - 1);
69          if (first == tour.Stops.Count - 2) {
70            length = tour.Stops.Count - first;
71          } else {
72            length = random.Next(tour.Stops.Count - first - 1) + 2;
73          }
74        }
75
76        var originalFeasible = instance.TourFeasible(tour, individual);
77
78        var segment = tour.Stops.GetRange(first, length);
79        tour.Stops.RemoveRange(first, length);
80        segment.Reverse();
81        tour.Stops.InsertRange(first, segment);
82
83        if (!allowInfeasible && originalFeasible) {
84          feasible = instance.TourFeasible(tour, individual);
85          if (!feasible) {
86            tour.Stops.RemoveRange(first, length);
87            segment.Reverse();
88            tour.Stops.InsertRange(first, segment);
89          }
90        }
91
92      } while(!feasible && count++ < 100);
93    }
94
95    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
96      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
97      Apply(random, individual, ProblemInstance, allowInfeasible);
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.