work in progress ... = How to ... implement a new VRPEncoding = == Goals == One way to implement a new VRP variant is by implementing a new {{{VRPEncoding}}} and new {{{Operators}}}. This is a suitable possibility if the VRP variant requires additional decision variables to optimize. Based on these new decision variables, new {{{Operators}}} for ''crossing'' and ''mutating'' solutions in the search process of algorithms (e.g. a ''Genetic Algorithm'') are required. A {{{VRPEncoding}}} is the representation of a solution candidate for a given {{{ProblemInstance}}} (see [wiki:UsersHowtosImplementANewVRPProblemInstance How to ... implement a new VRP ProblemInstance]). There are different ways of representing and operating on tours of a VRP solution. ([http://link.springer.com/chapter/10.1007%2F978-3-642-27549-4_42 source]) Different {{{Encodings}}} require different {{{Operators}}} to work on. This way of extending the vehicle routing problem allows you to modify the search process and yielding results with additinal decition variables. After this tutorial you will be able to implement you own VRP variants that require additional decision variables. You will also have a basic understanding of the concepts and architecture concerning the {{{VRPEncoding}}}, the {{{VRPOperator}}} concepts and related components. == The Multi Trip Vehicle Routing Problem == To show the procedure of implementing a new {{{VRPEncoding}}}, the implementation of the ''Multi Trip Vehicle Routing Problem (MTVRP)'' ([http://www.jstor.org/discover/10.2307/3009960?uid=3737528&uid=2&uid=4&sid=21103529629063 source]) is demonstrated. The MTVRP allows vehicles to return to the depot, refill their cargo and therefore can satisfy multiple tours. In addition the maximum driving distance of each vehicle is limited. Usually MTVRP solutions require much fewer vehicles compared to standard VRP variants because a vehicle can drive multiple tours. In this tutorial, the MTVRP is based on the {{{Capacitated Vehicle Routing Problem (CVRP)}}}. In addition we have to specify the maximum distance a vehicle can drive as well the penalty for exceeding the maximum distance. To implement the new {{{Encoding}}} we extend the existing {{{PotvingEncoding}}} ([http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.16.8719 source]). In addition, the available operators for the {{{PotvinEncoding}}} will be extended in order to make use of the new data in the encoding. The advantage of extending an existing encoding is that we can reuse the original operators in the algorithm. If you like to implement a complete new encoding you also have to implement all operators yourself. == Prerequisites == Before you start, make sure you have the latest version of the HeuristicLab source code ready. Then create a new plugin called {{{HeuristicLab.MultiTripVRP}}}. For additional help for creating a new plugin, see [wiki:UsersHowtosImplementPluginsStepByStep How to ... create HeuristicLab plugins (step by step)]. Your plugin need an additional dependency onto the {{{HeuristicLab.Problems.VehicleRouting}}} plugin. The plugin should look like this: {{{ #!cs [Plugin("HeuristicLab.MultiTripVRP", "1.0.0.0")] [PluginFile("HeuristicLab.MultiTripVRP.dll", PluginFileType.Assembly)] [PluginDependency("HeuristicLab.Problems.VehicleRouting", "3.4")] public class MultiTripVRPPlugin : PluginBase { } }}} In addition you will need references onto following HeuristicLab assemblies: - HeuristicLab.Collections - HeuristicLab.Common - HeuristicLab.Core - HeuristicLab.Data - HeuristicLab.Encodings.PermutationEncoding - HeuristicLab.Operators - HeuristicLab.Optimization - HeuristicLab.Parameters - HeuristicLab.Persistence - '''HeuristicLab.Problems.VehicleRouting''' = Implementing new Encoding = The first thing to do is to create the new encoding. If you want to implement a whole new encoding, you start by deriving from {{{TourEncoding}}}. Then you have to store the assignments vehicles to tours as well as additional data, like our ''tour delimiters'' for the ''multi trip VRP''. In the case of this tutorial we derive from the existing {{{PotvinEncoding}}} which stores the vehicle assignments already. {{{ #!cs [Item("MultiTripEncoding", "Represents a potvin encoding of VRP solutions adapted for multi trip VRPs.")] [StorableClass] public class MultiTripEncoding : PotvinEncoding { [StorableConstructor] protected MultiTripEncoding(bool serializing) : base(serializing) { } public override IDeepCloneable Clone(Cloner cloner) { return new MultiTripEncoding(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { AttachEventHandlers(); } } }}} To implement the multi trip VRP we need to store when a tour is interrupted and the vehicle drives back to the depot. We store this information in a {{{List>}}} which indicates whether the tour is interrupted after the stop or not. A more compact possibility is to store the delimiters as bit in an integer, but then you are limited by the size of an integer (32 bit). {{{ #!cs [Storable] public List> TripDelimiters { get; private set; } public MultiTripEncoding(IVRPProblemInstance instance) : base(instance) { TripDelimiters = new List>(); AttachEventHandlers(); } protected MultiTripEncoding(MultiTripEncoding original, Cloner cloner) : base(original, cloner) { TripDelimiters = new List>(original.TripDelimiters.Count); foreach (var delimiters in original.TripDelimiters) { TripDelimiters.Add(new List(delimiters)); } } protected MultiTripEncoding(MultiTripEncoding original, Cloner cloner) : base(original, cloner) { TripDelimiters = new List>(original.TripDelimiters.Count); foreach (var delimiters in original.TripDelimiters) { TripDelimiters.Add(new List(delimiters)); } } }}} In order to keep the delimiters synchronized with the actual tours we need to register event handlers on the {{{Tours}}} collection. When a tour is added or removed we also have to add or remove the delimiters. {{{ #!cs private void AttachEventHandlers() { Tours.ItemsAdded += new CollectionItemsChangedEventHandler>(Tours_ItemsAdded); Tours.ItemsRemoved += new CollectionItemsChangedEventHandler>(Tours_ItemsRemoved); } void Tours_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) { foreach (var added in e.Items.Except(e.OldItems).OrderBy(x => x.Index)) { TripDelimiters.Insert(added.Index, new List()); } } void Tours_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) { foreach (var removed in e.OldItems.Except(e.Items).OrderByDescending(x => x.Index)) { TripDelimiters.RemoveAt(removed.Index); } } }}} In addition we implement a method for retrieving the delimiters for a tour as array, and to flip a delimiter on specific position in a tour. These methods are used for the {{{VRPOperators}}} and the {{{VRPEvaluator}}} later. We also specify a method for converting an existing {{{PotvinEncoding}}} and new delimiters to a new {{{MultiTripEncoding}}}. {{{ #!cs public bool[] GetDelimiters(int tour) { if (tour < TripDelimiters.Count) { return TripDelimiters[tour].ToArray(); } else { return new bool[0]; } } public void FlipDelimiter(int tour, int index) { if (tour < TripDelimiters.Count) { if (TripDelimiters[tour].Count <= index) { for (int i = TripDelimiters[tour].Count; i <= index; i++) { TripDelimiters[tour].Add(false); } } TripDelimiters[tour][index] = !TripDelimiters[tour][index]; } } public static MultiTripEncoding ConvertFrom(IVRPProblemInstance instance, PotvinEncoding orig, List> tripDelimiters = null) { MultiTripEncoding result = new MultiTripEncoding(instance); result.Tours.AddRange(orig.Tours); for (int i = 0; i < result.Tours.Count; i++) { result.VehicleAssignment[i] = orig.VehicleAssignment[i]; } if (tripDelimiters != null) result.TripDelimiters = tripDelimiters; return result; } }}} = Sample Foundations = In order to implement the sample we need a new {{{ProblemInstance}}} and a new {{{Evaluator}}} for the '''MultiTrip VRP''' variant. For detailed information about creating new {{{VRPInstances}}} and {{{VRPEvaluators}}} see [wiki:UsersHowtosImplementANewVRPProblemInstance How to ... implement a new VRP ProblemInstance] and [wiki:UsersHowtosImplementANewVRPEvaluator How to ... implement a new VRP Evaluator]. == MultiTripVRPInstance == The {{{MultiTripVRPInstance}}} only needs to store the additional {{{MaxDistance}}} parameter for the vehicle as well as the penalty for exceeding it. {{{ #!cs public interface IMultiTripProblemInstance { DoubleValue MaxDistance { get; set; } DoubleValue MaxDistancePenalty { get; set; } } }}} For the implementation the usual HL specific cloning and persistence code is necessary. {{{ #!cs [Item("MultiTripVRPInstance", "Represents a single depot multi trip CVRP instance.")] [StorableClass] public class MultiTripVRPInstance : CVRPProblemInstance, IMultiTripProblemInstance { protected IValueParameter MaxDistanceParameter { get { return (IValueParameter)Parameters["MaxDistance"]; } } public DoubleValue MaxDistance { get { return MaxDistanceParameter.Value; } set { MaxDistanceParameter.Value = value; } } protected IValueParameter MaxDistancePenaltyParameter { get { return (IValueParameter)Parameters["MaxDistancePenalty"]; } } public DoubleValue MaxDistancePenalty { get { return MaxDistancePenaltyParameter.Value; } set { MaxDistancePenaltyParameter.Value = value; } } [StorableConstructor] protected MultiTripVRPInstance(bool deserializing) : base(deserializing) { } public MultiTripVRPInstance() { Parameters.Add(new ValueParameter("MaxDistance", "The max distance of each vehicle.", new DoubleValue(100))); Parameters.Add(new ValueParameter("MaxDistancePenalty", "The distance penalty considered in the evaluation.", new DoubleValue(100))); AttachEventHandlers(); } public override IDeepCloneable Clone(Cloner cloner) { return new MultiTripVRPInstance(this, cloner); } protected MultiTripVRPInstance(MultiTripVRPInstance original, Cloner cloner) : base(original, cloner) { AttachEventHandlers(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { AttachEventHandlers(); } private void AttachEventHandlers() { MaxDistanceParameter.ValueChanged += new EventHandler(MaxDistanceParameter_ValueChanged); MaxDistancePenaltyParameter.ValueChanged += new EventHandler(MaxDistancePenaltyParameter_ValueChanged); } void MaxDistancePenaltyParameter_ValueChanged(object sender, EventArgs e) { MaxDistancePenaltyParameter.Value.ValueChanged += new EventHandler(ValueChanged); EvalBestKnownSolution(); } void MaxDistanceParameter_ValueChanged(object sender, EventArgs e) { MaxDistanceParameter.Value.ValueChanged += new EventHandler(ValueChanged); EvalBestKnownSolution(); } void ValueChanged(object sender, EventArgs e) { EvalBestKnownSolution(); } } }}} We will override more methods in the {{{MultiTripProblemInstance}}} class when we deal with '''operators''' in this tutorial later. == MultiTripEvaluator == For the evaluator we copy the {{{CVRPEvaluator}}} and modify it. Now we consider the travel distance after a stopover on the depot and calculate the maximum distance violation. {{{ #!cs public class MultiTripEvaluation : CVRPEvaluation { public double MaxDistanceViolation { get; set; } } [Item("MultiTripEvaluator", "Represents a single depot CVRP evaluator with multiple trips.")] [StorableClass] public class MultiTripEvaluator : CVRPEvaluator { public ILookupParameter MaxDistanceViolationParameter { get { return (ILookupParameter)Parameters["MaxDistanceViolation"]; } } protected override VRPEvaluation CreateTourEvaluation() { return new MultiTripEvaluation(); } protected override void InitResultParameters() { base.InitResultParameters(); MaxDistanceViolationParameter.ActualValue = new DoubleValue(0); } protected override void SetResultParameters(VRPEvaluation tourEvaluation) { base.SetResultParameters(tourEvaluation); MaxDistanceViolationParameter.ActualValue.Value = (tourEvaluation as MultiTripEvaluation).MaxDistanceViolation; } [StorableConstructor] protected MultiTripEvaluator(bool deserializing) : base(deserializing) { } public MultiTripEvaluator() { Parameters.Add(new LookupParameter("MaxDistanceViolation", "The distance violation.")); } public override IDeepCloneable Clone(Cloner cloner) { return new MultiTripEvaluator(this, cloner); } protected MultiTripEvaluator(MultiTripEvaluator original, Cloner cloner) : base(original, cloner) { } } }}} In the evaluation process we need to consider the additional travel distance after a vehicle drives back to the depot. Therefore we add the distance from the current stop to the depot and the distance between the depot and the next stop instead of the direct distance. After a tour we have to check the maximum driven distance whether it exceeds the maximum driven distance of a vehicle or not. In case of a violation of the limit we increment the penalty. {{{ #!cs protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) { TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour))); ; eval.InsertionInfo.AddTourInsertionInfo(tourInfo); double originalQuality = eval.Quality; IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance; MultiTripVRPInstance multiTripInstance = instance as MultiTripVRPInstance; DoubleArray demand = instance.Demand; double overweight = 0.0; double distance = 0.0; double capacity = cvrpInstance.Capacity.Value; double spareCapacity = capacity; var tripDelimiters = new bool[0]; if(solution is MultiTripEncoding) tripDelimiters = ((MultiTripEncoding)solution).GetDelimiters(solution.GetTourIndex(tour)); //simulate a tour, start and end at depot for (int i = 0; i <= tour.Stops.Count; i++) { int start = 0; if (i > 0) start = tour.Stops[i - 1]; int end = 0; if (i < tour.Stops.Count) end = tour.Stops[i]; //drive there double currentDistace = 0; if (i > 0 && tripDelimiters.Length >= i && tripDelimiters[i - 1]) { currentDistace += instance.GetDistance(start, 0, solution); currentDistace += instance.GetDistance(0, end, solution); spareCapacity = capacity; } else { currentDistace += instance.GetDistance(start, end, solution); } distance += currentDistace; spareCapacity -= demand[end]; if (spareCapacity < 0) { overweight += -spareCapacity; spareCapacity = 0; } CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity); tourInfo.AddStopInsertionInfo(stopInfo); } eval.Quality += instance.FleetUsageFactor.Value; eval.Quality += instance.DistanceFactor.Value * distance; eval.Distance += distance; eval.VehicleUtilization += 1; (eval as CVRPEvaluation).Overload += overweight; double penalty = overweight * cvrpInstance.OverloadPenalty.Value; eval.Penalty += penalty; eval.Quality += penalty; tourInfo.Penalty = penalty; tourInfo.Quality = eval.Quality - originalQuality; if (distance > multiTripInstance.MaxDistance.Value) { double maxDistanceViolation = (distance - (multiTripInstance.MaxDistance.Value)); penalty = maxDistanceViolation * multiTripInstance.MaxDistancePenalty.Value; eval.Penalty += penalty; eval.Quality += penalty; (eval as MultiTripEvaluation).MaxDistanceViolation += maxDistanceViolation; } } }}} Because the increment based implementation is not trivial and not topic of this tutorial, we evaluate the tour at whole and calculate the distance to the previous solution. {{{ #!cs protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer, out bool feasible) { if (!(solution is MultiTripEncoding)) { return base.GetTourInsertionCosts(instance, solution, tourInsertionInfo, index, customer, out feasible); } else { MultiTripEncoding individual = (solution as MultiTripEncoding); int tourIdx = -1; for (int i = 0; i < individual.Tours.Count; i++) { if (solution.GetVehicleAssignment(i) == tourInsertionInfo.Vehicle) tourIdx = i; } Tour tour = individual.Tours[tourIdx]; tour.Stops.Insert(index, customer); VRPEvaluation newEval = instance.EvaluateTour(tour, individual); tour.Stops.RemoveAt(index); feasible = instance.Feasible(newEval); return newEval.Quality - tourInsertionInfo.Quality; } } }}} = Implementing new Operators = == Foundations == == Link Encoding/Operators to VRP variants == == Implementing new MultiTripOperator == == Implementing new Creator == == Implementing new Manipulator == == Implement new Crossover == = Result = == Configure Algorithm == == Interpretation ==