Index: /branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/LocalImprovement/AlbaLambdaInterchangeLocalImprovementOperator.cs
===================================================================
--- /branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/LocalImprovement/AlbaLambdaInterchangeLocalImprovementOperator.cs	(revision 6608)
+++ /branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/LocalImprovement/AlbaLambdaInterchangeLocalImprovementOperator.cs	(revision 6608)
@@ -0,0 +1,151 @@
+﻿#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 <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using HeuristicLab.Optimization;
+using HeuristicLab.Core;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Data;
+using HeuristicLab.Common;
+using HeuristicLab.Parameters;
+using HeuristicLab.Operators;
+using HeuristicLab.Problems.VehicleRouting.Interfaces;
+
+namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
+  [Item("AlbaLambdaInterchangeLocalImprovementOperator", "Takes a solution and finds the local optimum with respect to the lambda interchange neighborhood by decending along the steepest gradient.")]
+  [StorableClass]
+  public class AlbaLambdaInterchangeLocalImprovementOperator : VRPOperator, IStochasticOperator, ILocalImprovementOperator {
+    public Type ProblemType {
+      get { return typeof(VehicleRoutingProblem); }
+    }
+
+    [Storable]
+    private VehicleRoutingProblem problem;
+    public IProblem Problem {
+      get { return problem; }
+      set { problem = (VehicleRoutingProblem)value; }
+    }
+
+    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
+      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
+    }
+
+    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
+      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
+    }
+
+    public ILookupParameter<ResultCollection> ResultsParameter {
+      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
+    }
+
+    public ILookupParameter<IVRPEncoding> VRPToursParameter {
+      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
+    }
+
+    public ILookupParameter<DoubleValue> QualityParameter {
+      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
+    }
+
+    public IValueParameter<IntValue> LambdaParameter {
+      get { return (IValueParameter<IntValue>)Parameters["Lambda"]; }
+    }
+
+    public IValueParameter<IntValue> SampleSizeParameter {
+      get { return (IValueParameter<IntValue>)Parameters["SampleSize"]; }
+    }
+
+    public ILookupParameter<IRandom> RandomParameter {
+      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
+    }
+
+    [StorableConstructor]
+    protected AlbaLambdaInterchangeLocalImprovementOperator(bool deserializing) : base(deserializing) { }
+    protected AlbaLambdaInterchangeLocalImprovementOperator(AlbaLambdaInterchangeLocalImprovementOperator original, Cloner cloner)
+      : base(original, cloner) {
+        this.problem = cloner.Clone(original.problem);
+    }
+    public AlbaLambdaInterchangeLocalImprovementOperator()
+      : base() {
+      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum amount of iterations that should be performed (note that this operator will abort earlier when a local optimum is reached.", new IntValue(10000)));
+      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The amount of evaluated solutions (here a move is counted only as 4/n evaluated solutions with n being the length of the permutation)."));
+      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
+      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours to be manipulated."));
+      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
+      Parameters.Add(new ValueParameter<IntValue>("Lambda", "The lambda value.", new IntValue(1)));
+      Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The number of moves to generate.", new IntValue(2000)));
+      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
+    }
+
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new AlbaLambdaInterchangeLocalImprovementOperator(this, cloner);
+    }
+
+    public static void Apply(AlbaEncoding solution, int maxIterations, 
+      int lambda, int samples, IRandom random, IVRPProblemInstance problemInstance, ref double quality, out int evaluatedSolutions) {
+      evaluatedSolutions = 0;
+
+      for (int i = 0; i < maxIterations; i++) {
+        AlbaLambdaInterchangeMove bestMove = null;
+        foreach (AlbaLambdaInterchangeMove move in AlbaStochasticLambdaInterchangeMultiMoveGenerator.GenerateAllMoves(solution, problemInstance, lambda, samples, random)) {
+          AlbaEncoding newSolution = solution.Clone() as AlbaEncoding;
+          AlbaLambdaInterchangeMoveMaker.Apply(newSolution, move);
+          double moveQuality =
+            problemInstance.Evaluate(newSolution).Quality;
+
+          evaluatedSolutions++;
+          if (moveQuality < quality || quality == -1) {
+            quality = moveQuality;
+            bestMove = move;
+          }
+        }
+        if (bestMove != null) 
+          AlbaLambdaInterchangeMoveMaker.Apply(solution, bestMove);
+      }
+    }
+
+    public override IOperation Apply() {
+      int maxIterations = MaximumIterationsParameter.ActualValue.Value;
+      AlbaEncoding solution = null;
+
+      if (VRPToursParameter.ActualValue is AlbaEncoding)
+        solution = VRPToursParameter.ActualValue as AlbaEncoding;
+      else
+        VRPToursParameter.ActualValue = solution = AlbaEncoding.ConvertFrom(VRPToursParameter.ActualValue, ProblemInstance);
+
+      int lambda = LambdaParameter.Value.Value;
+      int samples = SampleSizeParameter.Value.Value;
+      IRandom random = RandomParameter.ActualValue;
+
+      double quality = QualityParameter.ActualValue.Value;
+      int evaluatedSolutions;
+
+      Apply(solution, maxIterations, lambda, samples, random, ProblemInstance, ref quality, out evaluatedSolutions);
+
+      EvaluatedSolutionsParameter.ActualValue.Value += evaluatedSolutions;
+      QualityParameter.ActualValue.Value = quality;
+
+      return base.Apply();
+    }
+  }
+}
Index: /branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinInsertionBasedCrossover.cs
===================================================================
--- /branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinInsertionBasedCrossover.cs	(revision 6608)
+++ /branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinInsertionBasedCrossover.cs	(revision 6608)
@@ -0,0 +1,279 @@
+﻿#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 <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using HeuristicLab.Common;
+using HeuristicLab.Core;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using System.Collections.Generic;
+using HeuristicLab.Data;
+using System;
+using HeuristicLab.Parameters;
+
+namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
+  [Item("PotvinInsertionBasedCrossover", "The IBX crossover for VRP representations. It is implemented as described in Berger, J and Solois, M and Begin, R (1998). A hybrid genetic algorithm for the vehicle routing problem with time windows. LNCS 1418. Springer, London 114-127.")]
+  [StorableClass]
+  public sealed class PotvinInsertionBasedCrossover : PotvinCrossover {
+    public IValueParameter<IntValue> Length {
+      get { return (IValueParameter<IntValue>)Parameters["Length"]; }
+    }
+
+    [StorableConstructor]
+    private PotvinInsertionBasedCrossover(bool deserializing) : base(deserializing) { }
+    private PotvinInsertionBasedCrossover(PotvinInsertionBasedCrossover original, Cloner cloner)
+      : base(original, cloner) {
+    }
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new PotvinInsertionBasedCrossover(this, cloner);
+    }
+    public PotvinInsertionBasedCrossover()
+      : base() {
+      Parameters.Add(new ValueParameter<IntValue>("Length", "The maximum length of the replaced route.", new IntValue(1)));
+    }
+
+    private static int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) {
+      int tourIndex = -1;
+
+      double sum = 0.0;
+      double[] probabilities = new double[individual.Tours.Count];
+      for (int i = 0; i < individual.Tours.Count; i++) {
+        probabilities[i] = 1.0 / ((double)individual.Tours[i].Stops.Count / (double)individual.Cities);
+        sum += probabilities[i];
+      }
+
+      double rand = random.NextDouble() * sum;
+      double cumulatedProbabilities = 0.0;
+      int index = 0;
+      while (tourIndex == -1 && index < probabilities.Length) {
+        if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
+          tourIndex = index;
+
+        cumulatedProbabilities += probabilities[index];
+        index++;
+      }
+
+      return tourIndex;
+    }
+
+    private double CalculateCentroidDistance(Tour t1, Tour t2, DoubleMatrix coordinates) {
+      double xSum = 0;
+      double ySum = 0;
+      double c1X, c1Y, c2X, c2Y;
+
+      for (int i = 0; i < t1.Stops.Count; i++) {
+        xSum += coordinates[t1.Stops[i], 0];
+        ySum += coordinates[t1.Stops[i], 1];
+      }
+      c1X = xSum / t1.Stops.Count;
+      c1Y = ySum / t1.Stops.Count;
+
+      for (int i = 0; i < t2.Stops.Count; i++) {
+        xSum += coordinates[t2.Stops[i], 0];
+        ySum += coordinates[t2.Stops[i], 1];
+      }
+      c2X = xSum / t1.Stops.Count;
+      c2Y = ySum / t1.Stops.Count;
+
+      return Math.Sqrt(
+           (c1X - c2X) * (c1X - c2X) +
+           (c1Y - c2Y) * (c1Y - c2Y));
+    }
+
+    private double CalculateMeanCentroidDistance(Tour t1, IList<Tour> tours, DoubleMatrix coordinates) {
+      double sum = 0;
+
+      for (int i = 0; i < tours.Count; i++) {
+        sum += CalculateCentroidDistance(t1, tours[i], coordinates);
+      }
+
+      return sum / tours.Count;
+    }
+
+    private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour) {
+      int cityIndex = -1;
+
+      double sum = 0.0;
+      double[] probabilities = new double[tour.Stops.Count];
+      for (int i = 0; i < tour.Stops.Count; i++) {
+        int next;
+        if (i + 1 >= tour.Stops.Count)
+          next = 0;
+        else
+          next = tour.Stops[i + 1];
+        double distance = ProblemInstance.GetDistance(
+          tour.Stops[i], next);
+
+        int prev;
+        if (i - 1 < 0)
+          prev = 0;
+        else
+          prev = tour.Stops[i - 1];
+        distance += ProblemInstance.GetDistance(
+          tour.Stops[i], prev);
+
+        probabilities[i] = distance;
+        sum += probabilities[i];
+      }
+
+      double rand = random.NextDouble() * sum;
+      double cumulatedProbabilities = 0.0;
+      int index = 0;
+      while (cityIndex == -1 && index < probabilities.Length) {
+        if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
+          cityIndex = index;
+
+        cumulatedProbabilities += probabilities[index];
+        index++;
+      }
+
+      return cityIndex;
+    }
+
+    private bool FindRouteInsertionPlace(
+      Tour tour,
+      int city, bool allowInfeasible, out int place) {
+      place = -1;
+      bool bestFeasible = false;
+      double minDetour = 0;
+
+      for (int i = 0; i <= tour.Stops.Count; i++) {
+        double length = tour.GetTourLength(ProblemInstance);
+
+        tour.Stops.Insert(i, city);
+
+        bool feasible = ProblemInstance.Feasible(tour);
+
+        if (feasible || allowInfeasible && !bestFeasible) {
+          double newLength = tour.GetTourLength(ProblemInstance);
+          double detour = newLength - length;
+
+          if (place < 0 || detour < minDetour || feasible && !bestFeasible) {
+            place = i;
+            minDetour = detour;
+
+            if (feasible)
+              bestFeasible = true;
+          }
+        }
+
+        tour.Stops.RemoveAt(i);
+      }
+
+      return place >= 0;
+    }
+
+    private ICollection<int> GetUnrouted(PotvinEncoding solution, int cities) {
+      HashSet<int> undiscovered = new HashSet<int>();
+      for (int i = 1; i <= cities; i++) {
+        undiscovered.Add(i);
+      }
+
+      foreach (Tour tour in solution.Tours) {
+        foreach (int city in tour.Stops)
+          undiscovered.Remove(city);
+      }
+
+      return undiscovered;
+    }
+
+    protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) {
+      PotvinEncoding child = new PotvinEncoding(ProblemInstance);
+
+      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
+
+      List<Tour> R1 = new List<Tour>();
+      PotvinEncoding p1Clone = parent1.Clone() as PotvinEncoding;
+
+      int length = Math.Min(Length.Value.Value, parent1.Tours.Count) + 1;
+      int k = random.Next(1, length);
+      for (int i = 0; i < k; i++) {
+        int index = SelectRandomTourBiasedByLength(random, p1Clone);
+        R1.Add(p1Clone.Tours[index]);
+        p1Clone.Tours.RemoveAt(index);
+      }
+
+      foreach (Tour r1 in R1) {
+        List<int> R2 = new List<int>();
+
+        double r = CalculateMeanCentroidDistance(r1, parent2.Tours, ProblemInstance.Coordinates);
+        foreach (Tour tour in parent2.Tours) {
+          if (CalculateCentroidDistance(r1, tour, ProblemInstance.Coordinates) <= r) {
+            R2.AddRange(tour.Stops);
+          }
+        }
+
+        Tour childTour = new Tour();
+        childTour.Stops.AddRange(r1.Stops);
+
+        //DESTROY - remove cities from r1
+        int removed = random.Next(1, r1.Stops.Count + 1);
+        for (int i = 0; i < removed; i++) {
+          childTour.Stops.RemoveAt(SelectCityBiasedByNeighborDistance(random, childTour));
+        }
+
+        //REPAIR - add cities from R2
+        int maxCount = random.Next(1, Math.Min(5, R2.Count));
+        int count = 0;
+
+        while (count < maxCount && R2.Count != 0) {
+          PotvinEncoding newChild = child.Clone() as PotvinEncoding;
+          newChild.Tours.Add(childTour);
+
+          int index = random.Next(R2.Count);
+          int city = R2[index];
+          R2.RemoveAt(index);
+
+          int place = -1;
+          if (FindRouteInsertionPlace(childTour, city, allowInfeasible, out place)) {
+            childTour.Stops.Insert(place, city);
+
+            if (!Repair(random, child, childTour, ProblemInstance, allowInfeasible)) {
+              childTour.Stops.RemoveAt(place);
+            } else {
+              count++;
+            }
+          }
+        }
+
+        child.Tours.Add(childTour);
+        Repair(random, child, childTour, ProblemInstance, allowInfeasible);
+      }
+
+      for (int i = 0; i < p1Clone.Tours.Count; i++) {
+        Tour childTour = p1Clone.Tours[i].Clone() as Tour;
+        child.Tours.Add(childTour);
+        Repair(random, child, childTour, ProblemInstance, allowInfeasible);
+      }
+
+      //route unrouted customers
+      child.Unrouted.AddRange(GetUnrouted(child, ProblemInstance.Cities.Value));
+      bool success = RouteUnrouted(child, allowInfeasible);
+
+      if (success || allowInfeasible)
+        return child;
+      else {
+        if (random.NextDouble() < 0.5)
+          return parent1.Clone() as PotvinEncoding;
+        else
+          return parent2.Clone() as PotvinEncoding;
+      }
+    }
+  }
+}
