#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.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using System.Drawing;
using System.Collections.Generic;
using HeuristicLab.Problems.VehicleRouting.Encodings.General;
using HeuristicLab.Problems.VehicleRouting.Variants;
using HeuristicLab.Problems.VehicleRouting.Interfaces;
using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
[Item("PotvinEncoding", "Represents a potvin encoding of VRP solutions. It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
[StorableClass]
public class PotvinEncoding : TourEncoding, ISingleDepotEncoding, ITimeWindowedEncoding, IHomogenousCapacitatedEncoding {
[Storable]
public List Unrouted { get; set; }
[Storable]
public double PenaltyFactor { get; set; }
public PotvinEncoding(IVRPProblemInstance instance)
: base(instance) {
Unrouted = new List();
PenaltyFactor = 1;
}
[StorableConstructor]
private PotvinEncoding(bool serializing)
: base(serializing) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new PotvinEncoding(this, cloner);
}
protected PotvinEncoding(PotvinEncoding original, Cloner cloner)
: base(original, cloner) {
this.Unrouted = new List(original.Unrouted);
this.PenaltyFactor = original.PenaltyFactor;
}
public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
PotvinEncoding solution = new PotvinEncoding(instance);
TourEncoding.ConvertFrom(encoding, solution, instance);
return solution;
}
public static PotvinEncoding ConvertFrom(List route, IVRPProblemInstance instance) {
PotvinEncoding solution = new PotvinEncoding(instance);
TourEncoding.ConvertFrom(route, solution);
return solution;
}
public double GetTourLength(Tour tour) {
double length = 0;
if (tour.Stops.Count > 0) {
List cities = new List();
cities.Add(0);
foreach (int city in tour.Stops) {
cities.Add(city);
}
cities.Add(0);
for (int i = 1; i < cities.Count; i++) {
length += ProblemInstance.GetDistance(cities[i - 1], cities[i]);
}
}
return length;
}
public int FindBestInsertionPlace(Tour tour, int city) {
int place = -1;
double minQuality = -1;
for (int i = 0; i <= tour.Stops.Count; i++) {
tour.Stops.Insert(i, city);
VRPEvaluation eval = ProblemInstance.EvaluatorParameter.Value.Evaluate(ProblemInstance, tour);
double quality = eval.Quality + eval.Penalty * (PenaltyFactor - 1.0);
if (place < 0 || quality < minQuality) {
place = i;
minQuality = quality;
}
tour.Stops.RemoveAt(i);
}
if (place == -1)
place = 0;
return place;
}
public bool FindInsertionPlace(int city, int routeToAvoid, out int route, out int place) {
route = -1;
place = -1;
double minDetour = 0;
for (int tour = 0; tour < Tours.Count; tour++) {
if (tour != routeToAvoid) {
double length = GetTourLength(Tours[tour]);
for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
Tours[tour].Stops.Insert(i, city);
VRPEvaluation eval = ProblemInstance.EvaluatorParameter.Value.Evaluate(
ProblemInstance, Tours[tour]);
if (ProblemInstance.EvaluatorParameter.Value.Feasible(eval)) {
double newLength = eval.Distance;
double detour = newLength - length;
if (route <= 0 || detour < minDetour) {
route = tour;
place = i;
minDetour = detour;
}
}
Tours[tour].Stops.RemoveAt(i);
}
}
}
return route >= 0 && place >= 0;
}
}
}