[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;
|
---|
| 32 |
|
---|
[7159] | 33 | namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
|
---|
| 34 | [Item("TwoOptManipulator", "Two opt manipulation")]
|
---|
[7145] | 35 | [StorableClass]
|
---|
[7159] | 36 | public sealed class TwoOptManipulator : PotvinManipulator {
|
---|
[7145] | 37 | [StorableConstructor]
|
---|
[7159] | 38 | private TwoOptManipulator(bool deserializing) : base(deserializing) { }
|
---|
| 39 | private TwoOptManipulator(TwoOptManipulator original, Cloner cloner)
|
---|
[7145] | 40 | : base(original, cloner) {
|
---|
| 41 | }
|
---|
| 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[7159] | 43 | return new TwoOptManipulator(this, cloner);
|
---|
[7145] | 44 | }
|
---|
[7159] | 45 | public TwoOptManipulator()
|
---|
[7145] | 46 | : base() {
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public static void Apply(IRandom random, PotvinEncoding individual) {
|
---|
| 50 | List<Tour> tours = individual.Tours.FindAll(t => t.Cities.Count >= 4);
|
---|
| 51 |
|
---|
| 52 | if (tours.Count > 0) {
|
---|
| 53 | int tourIdx = random.Next(tours.Count);
|
---|
| 54 | Tour tour = tours[tourIdx];
|
---|
| 55 |
|
---|
| 56 | int a;
|
---|
| 57 | if (tour.Cities.Count == 4) {
|
---|
| 58 | a = 0;
|
---|
| 59 | } else if (tour.Cities.Count == 5) {
|
---|
| 60 | int idx = random.Next(4);
|
---|
| 61 | if (idx >= 2)
|
---|
| 62 | idx++;
|
---|
| 63 | a = idx;
|
---|
| 64 | } else {
|
---|
| 65 | a = random.Next(tour.Cities.Count);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | int b;
|
---|
| 69 | List<int> indices = new List<int>();
|
---|
| 70 | for (int i = 0; i < tour.Cities.Count; i++) {
|
---|
| 71 | if (Math.Abs(i - a) > 2) {
|
---|
| 72 | indices.Add(i);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | b = indices[random.Next(indices.Count)];
|
---|
| 76 |
|
---|
| 77 | if (b < a) {
|
---|
| 78 | int tmp = a;
|
---|
| 79 | a = b;
|
---|
| 80 | b = tmp;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | int index = a + 1;
|
---|
| 84 | int count = b - a - 1;
|
---|
| 85 | List<int> segment = tour.Cities.GetRange(index, count);
|
---|
| 86 | tour.Cities.RemoveRange(index, count);
|
---|
| 87 | segment.Reverse();
|
---|
| 88 | tour.Cities.InsertRange(index, segment);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | protected override void Manipulate(IRandom random, PotvinEncoding individual) {
|
---|
| 93 | Apply(random, individual);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|