#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 HeuristicLab.Problems.VehicleRouting.Interfaces;
namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
[Item("TwoOptStarManipulator", "Two opt star manipulation")]
[StorableClass]
public sealed class TwoOptStarManipulator : PotvinManipulator {
[StorableConstructor]
private TwoOptStarManipulator(bool deserializing) : base(deserializing) { }
private TwoOptStarManipulator(TwoOptStarManipulator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new TwoOptStarManipulator(this, cloner);
}
public TwoOptStarManipulator()
: base() {
}
public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) {
bool feasible;
int count = 0;
//consider creating new tour
individual.Tours.Add(new Tour());
do {
feasible = true;
int route1Idx = random.Next(individual.Tours.Count);
int route2Idx = random.Next(individual.Tours.Count - 1);
if (route2Idx >= route1Idx)
route2Idx++;
Tour route1 = individual.Tours[route1Idx];
Tour route2 = individual.Tours[route2Idx];
int x1 = random.Next(route1.Stops.Count + 1);
int x2 = random.Next(route2.Stops.Count + 1);
var originalFeasible =
problemInstance.TourFeasible(route1, individual) &&
problemInstance.TourFeasible(route2, individual);
SwapSegments(x1, route1, x2, route2);
if (!allowInfeasible && originalFeasible) {
feasible = problemInstance.TourFeasible(route1, individual) &&
problemInstance.TourFeasible(route2, individual);
if (!feasible) SwapSegments(x1, route1, x2, route2);
}
} while (!feasible && count++ < 100);
individual.Tours.RemoveAll(t => t.Stops.Count == 0);
}
private static void SwapSegments(int x1, Tour route1, int x2, Tour route2) {
int count = route1.Stops.Count - x1;
List segmentX1 = new List();
if (count > 0) {
segmentX1 = route1.Stops.GetRange(x1, count);
route1.Stops.RemoveRange(x1, count);
}
count = route2.Stops.Count - x2;
List segmentX2 = new List();
if (count > 0) {
segmentX2 = route2.Stops.GetRange(x2, count);
route2.Stops.RemoveRange(x2, count);
}
route1.Stops.AddRange(segmentX2);
route2.Stops.AddRange(segmentX1);
}
protected override void Manipulate(IRandom random, PotvinEncoding individual) {
bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
Apply(random, individual, ProblemInstance, allowInfeasible);
}
}
}