Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/OrOptManipulator.cs @ 12676

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

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

File size: 3.6 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("OrOptManipulator", "Or opt manipulation")]
36  [StorableClass]
37  public sealed class OrOptManipulator : PotvinManipulator {
38    [StorableConstructor]
39    private OrOptManipulator(bool deserializing) : base(deserializing) { }
40    private OrOptManipulator(OrOptManipulator original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new OrOptManipulator(this, cloner);
45    }
46    public OrOptManipulator()
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      bool feasible;
54      int count = 0;
55
56      do {
57        feasible = true;
58        int tourIdx = random.Next(tours.Count);
59        Tour tour = tours[tourIdx];
60
61        int segmentStart = random.Next(tour.Stops.Count);
62        int segmentLength;
63        if (segmentStart == 0) {
64          segmentLength = 1 + random.Next(tour.Stops.Count - 1);
65        }
66        else {
67          segmentLength = 1 + random.Next(tour.Stops.Count - segmentStart);
68        }
69
70        bool originalFeasible = instance.TourFeasible(tour, individual);
71
72        List<int> segment = tour.Stops.GetRange(segmentStart, segmentLength);
73        tour.Stops.RemoveRange(segmentStart, segmentLength);
74        int newPos;
75        if (tour.Stops.Count == 1)
76          newPos = 0;
77        else
78          newPos = random.Next(tour.Stops.Count - 1);
79
80        if (newPos >= segmentStart)
81          newPos++;
82        tour.Stops.InsertRange(newPos, segment);
83
84        if (!allowInfeasible && originalFeasible) {
85          feasible = instance.TourFeasible(tour, individual);
86          if (!feasible) {
87            tour.Stops.RemoveRange(newPos, segment.Count);
88            tour.Stops.InsertRange(segmentStart, segment);
89          }
90        }
91      } while (!feasible && count++ < 100);
92    }
93
94    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
95      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
96      Apply(random, individual, ProblemInstance, allowInfeasible);
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.