#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
using HeuristicLab.Problems.VehicleRouting.Encodings;
using HeuristicLab.Problems.VehicleRouting;
using System;
namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
[Item("TwoOptManipulator", "Two opt manipulation")]
[StorableClass]
public sealed class TwoOptManipulator : PotvinManipulator {
[StorableConstructor]
private TwoOptManipulator(bool deserializing) : base(deserializing) { }
private TwoOptManipulator(TwoOptManipulator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new TwoOptManipulator(this, cloner);
}
public TwoOptManipulator()
: base() {
}
public static void Apply(IRandom random, PotvinEncoding individual) {
List tours = individual.Tours.FindAll(t => t.Cities.Count >= 4);
if (tours.Count > 0) {
int tourIdx = random.Next(tours.Count);
Tour tour = tours[tourIdx];
int a;
if (tour.Cities.Count == 4) {
a = 0;
} else if (tour.Cities.Count == 5) {
int idx = random.Next(4);
if (idx >= 2)
idx++;
a = idx;
} else {
a = random.Next(tour.Cities.Count);
}
int b;
List indices = new List();
for (int i = 0; i < tour.Cities.Count; i++) {
if (Math.Abs(i - a) > 2) {
indices.Add(i);
}
}
b = indices[random.Next(indices.Count)];
if (b < a) {
int tmp = a;
a = b;
b = tmp;
}
int index = a + 1;
int count = b - a - 1;
List segment = tour.Cities.GetRange(index, count);
tour.Cities.RemoveRange(index, count);
segment.Reverse();
tour.Cities.InsertRange(index, segment);
}
}
protected override void Manipulate(IRandom random, PotvinEncoding individual) {
Apply(random, individual);
}
}
}