#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;
using HeuristicLab.Problems.VehicleRouting.Interfaces;
namespace HeuristicLab.Analysis.FitnessLandscape.VRP {
[Item("OrOptManipulator", "Or opt manipulation")]
[StorableClass]
public sealed class OrOptManipulator : PotvinManipulator {
[StorableConstructor]
private OrOptManipulator(bool deserializing) : base(deserializing) { }
private OrOptManipulator(OrOptManipulator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new OrOptManipulator(this, cloner);
}
public OrOptManipulator()
: base() {
}
public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, bool allowInfeasible) {
List tours = individual.Tours.FindAll(t => t.Stops.Count >= 2);
if (tours.Count == 0) return;
bool feasible;
int count = 0;
do {
feasible = true;
int tourIdx = random.Next(tours.Count);
Tour tour = tours[tourIdx];
int segmentStart = random.Next(tour.Stops.Count);
int segmentLength;
if (segmentStart == 0) {
segmentLength = 1 + random.Next(tour.Stops.Count - 1);
}
else {
segmentLength = 1 + random.Next(tour.Stops.Count - segmentStart);
}
bool originalFeasible = instance.TourFeasible(tour, individual);
List segment = tour.Stops.GetRange(segmentStart, segmentLength);
tour.Stops.RemoveRange(segmentStart, segmentLength);
int newPos;
if (tour.Stops.Count == 1)
newPos = 0;
else
newPos = random.Next(tour.Stops.Count - 1);
if (newPos >= segmentStart)
newPos++;
tour.Stops.InsertRange(newPos, segment);
if (!allowInfeasible && originalFeasible) {
feasible = instance.TourFeasible(tour, individual);
if (!feasible) {
tour.Stops.RemoveRange(newPos, segment.Count);
tour.Stops.InsertRange(segmentStart, segment);
}
}
} while (!feasible && count++ < 100);
}
protected override void Manipulate(IRandom random, PotvinEncoding individual) {
bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
Apply(random, individual, ProblemInstance, allowInfeasible);
}
}
}