[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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
| 29 | using HeuristicLab.Problems.VehicleRouting.Encodings;
|
---|
| 30 | using HeuristicLab.Problems.VehicleRouting;
|
---|
| 31 | using System;
|
---|
[9836] | 32 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[7145] | 33 |
|
---|
[7159] | 34 | namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
|
---|
| 35 | [Item("TwoOptManipulator", "Two opt manipulation")]
|
---|
[7145] | 36 | [StorableClass]
|
---|
[7159] | 37 | public sealed class TwoOptManipulator : PotvinManipulator {
|
---|
[7145] | 38 | [StorableConstructor]
|
---|
[7159] | 39 | private TwoOptManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 40 | private TwoOptManipulator(TwoOptManipulator original, Cloner cloner)
|
---|
[7145] | 41 | : base(original, cloner) {
|
---|
| 42 | }
|
---|
| 43 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[7159] | 44 | return new TwoOptManipulator(this, cloner);
|
---|
[7145] | 45 | }
|
---|
[7159] | 46 | public TwoOptManipulator()
|
---|
[7145] | 47 | : base() {
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[9836] | 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;
|
---|
[7145] | 53 |
|
---|
[9836] | 54 | bool feasible;
|
---|
| 55 | int count = 0;
|
---|
| 56 |
|
---|
| 57 | do {
|
---|
| 58 | feasible = true;
|
---|
[7145] | 59 | int tourIdx = random.Next(tours.Count);
|
---|
| 60 | Tour tour = tours[tourIdx];
|
---|
| 61 |
|
---|
[9836] | 62 | int first;
|
---|
| 63 | int length;
|
---|
| 64 | if (tour.Stops.Count == 2) {
|
---|
| 65 | first = 0;
|
---|
| 66 | length = 2;
|
---|
[7145] | 67 | } else {
|
---|
[9836] | 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 | }
|
---|
[7145] | 74 | }
|
---|
| 75 |
|
---|
[9836] | 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);
|
---|
[7145] | 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[9836] | 92 | } while(!feasible && count++ < 100);
|
---|
[7145] | 93 | }
|
---|
| 94 |
|
---|
| 95 | protected override void Manipulate(IRandom random, PotvinEncoding individual) {
|
---|
[9836] | 96 | bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
|
---|
| 97 | Apply(random, individual, ProblemInstance, allowInfeasible);
|
---|
[7145] | 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|