#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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 HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Common;
using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
using HeuristicLab.Problems.VehicleRouting.Interfaces;
using HeuristicLab.Problems.VehicleRouting.Variants;
namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
[Item("PotvinPDRearrangeMoveMaker", "Peforms the rearrange move on a given PDP encoding and updates the quality.")]
[StorableClass]
public class PotvinPDRearrangeMoveMaker : PotvinMoveMaker, IPotvinPDRearrangeMoveOperator, IMoveMaker {
public ILookupParameter PDRearrangeMoveParameter {
get { return (ILookupParameter)Parameters["PotvinPDRearrangeMove"]; }
}
public override ILookupParameter VRPMoveParameter {
get { return PDRearrangeMoveParameter; }
}
[StorableConstructor]
private PotvinPDRearrangeMoveMaker(bool deserializing) : base(deserializing) { }
public PotvinPDRearrangeMoveMaker()
: base() {
Parameters.Add(new LookupParameter("PotvinPDRearrangeMove", "The moves that should be made."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new PotvinPDRearrangeMoveMaker(this, cloner);
}
protected PotvinPDRearrangeMoveMaker(PotvinPDRearrangeMoveMaker original, Cloner cloner)
: base(original, cloner) {
}
public static void InsertPair(PotvinEncoding solution, Tour tour, int source, int target, IVRPProblemInstance problemInstance, int positionToAvoid = -1, int positionToAvoid2 = -1) {
int stops = tour.Stops.Count;
VRPEvaluation eval = problemInstance.Evaluate(tour);
double minCosts = double.MaxValue;
int sourceLocation = -1;
int targetLocation = -1;
for (int i = 0; i <= stops; i++) {
tour.Stops.Insert(i, source);
VRPEvaluation tourEval = problemInstance.Evaluate(tour);
double sourceCosts = tourEval.Quality - eval.Quality;
for (int j = i + 1; j <= stops + 1; j++) {
if (positionToAvoid != i || positionToAvoid2 != j || stops == 0) {
bool feasible;
double targetCosts = problemInstance.GetInsertionCosts(tourEval, target, 0, j, out feasible);
double costs = sourceCosts + targetCosts;
if (costs < minCosts) {
minCosts = costs;
sourceLocation = i;
targetLocation = j;
}
}
}
tour.Stops.Remove(source);
}
tour.Stops.Insert(sourceLocation, source);
tour.Stops.Insert(targetLocation, target);
}
public static void Apply(PotvinEncoding solution, PotvinPDRearrangeMove move, IVRPProblemInstance problemInstance) {
Tour tour = solution.Tours[move.Tour];
int position = tour.Stops.IndexOf(move.City);
IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
if (pdp != null) {
int location = pdp.PickupDeliveryLocation[move.City];
Tour tour2 = solution.Tours.Find(t => t.Stops.Contains(location));
int position2 = tour2.Stops.IndexOf(location);
tour.Stops.Remove(move.City);
tour2.Stops.Remove(location);
PotvinPDRearrangeMoveMaker.InsertPair(solution, tour, move.City, location, problemInstance, position, position2);
} else {
tour.Stops.Remove(move.City);
int place = solution.FindBestInsertionPlace(tour, move.City, position);
tour.Stops.Insert(place, move.City);
}
}
protected override void PerformMove() {
PotvinPDRearrangeMove move = PDRearrangeMoveParameter.ActualValue;
PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
Apply(newSolution, move, ProblemInstance);
VRPToursParameter.ActualValue = newSolution;
//reset move quality
VRPEvaluation eval = ProblemInstance.Evaluate(newSolution);
MoveQualityParameter.ActualValue.Value = eval.Quality;
}
}
}