Index: branches/GP.Grammar.Editor/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/IterativeInsertionCreator.cs
===================================================================
--- branches/GP.Grammar.Editor/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/IterativeInsertionCreator.cs	(revision 6618)
+++ branches/GP.Grammar.Editor/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/IterativeInsertionCreator.cs	(revision 6618)
@@ -0,0 +1,170 @@
+﻿#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.Collections.Generic;
+using HeuristicLab.Common;
+using HeuristicLab.Core;
+using HeuristicLab.Optimization;
+using HeuristicLab.Parameters;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Data;
+using System;
+using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
+
+namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
+  [Item("IterativeInsertionCreator", "Creates a randomly initialized VRP solution using the iterative insertion Heuristic.")]
+  [StorableClass]
+  public sealed class IterativeInsertionCreator : DefaultRepresentationCreator, IStochasticOperator {
+    #region IStochasticOperator Members
+    public ILookupParameter<IRandom> RandomParameter {
+      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
+    }
+    #endregion
+
+    [StorableConstructor]
+    private IterativeInsertionCreator(bool deserializing) : base(deserializing) { }
+    private IterativeInsertionCreator(IterativeInsertionCreator original, Cloner cloner) : base(original, cloner) { }
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new IterativeInsertionCreator(this, cloner);
+    }
+
+    public IterativeInsertionCreator()
+      : base() {
+      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
+    }
+
+    private static double CalculateAngleToDepot(DoubleMatrix coordinates, int city) {
+      double dx = coordinates[0, 0];
+      double dy = coordinates[0, 1];
+
+      double cx = coordinates[city, 0];
+      double cy = coordinates[city, 1];
+
+      double alpha = Math.Atan((cx - dx) / (dy - cy)) * (180.0 / Math.PI);
+      if (cx > dx && cy > dy)
+        alpha = (90.0 + alpha) + 90.0;
+      else if (cx < dx && cy > dy)
+        alpha = alpha + 180.0;
+      else if (cx < dx && cy < dy)
+        alpha = (90.0 + alpha) + 270.0;
+
+      return alpha;
+    }
+
+    private int FindBestInsertionPlace(Tour tour, int city, DoubleArray dueTime,
+      DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand, DoubleValue capacity,
+      DistanceMatrix distMatrix) {
+      int place = -1;
+      double minQuality = -1;
+
+      for (int i = 0; i <= tour.Cities.Count; i++) {
+        tour.Cities.Insert(i, city);
+        
+        TourEvaluation evaluation = VRPEvaluator.EvaluateTour(tour,
+          dueTime, serviceTime, readyTime, demand, capacity,
+          new DoubleValue(0), new DoubleValue(0), new DoubleValue(1), new DoubleValue(100), new DoubleValue(100),
+            distMatrix);
+        double quality = evaluation.Quality;
+
+        if (place < 0 || quality < minQuality) {
+          place = i;
+          minQuality = quality;
+        }
+
+        tour.Cities.RemoveAt(i);
+      }
+
+      if (place == -1)
+        place = 0;
+
+      return place;
+    }
+
+    protected override List<int> CreateSolution() {
+      int cities = Cities;
+      int vehicles = VehiclesParameter.ActualValue.Value;
+      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
+      IRandom random = RandomParameter.ActualValue;
+      DoubleArray dueTime = DueTimeParameter.ActualValue;
+      DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
+      DoubleArray readyTime = ReadyTimeParameter.ActualValue;
+      DoubleArray demand = DemandParameter.ActualValue;
+      DoubleValue capacity = CapacityParameter.ActualValue;
+      BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
+
+      DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
+
+      PotvinEncoding solution = new PotvinEncoding();
+
+      List<int> customers = new List<int>();
+      for (int i = 0; i < Cities; i++)
+        customers.Add(i + 1);
+      customers.Sort(delegate(int city1, int city2) {
+            double angle1 = CalculateAngleToDepot(coordinates, city1);
+            double angle2 = CalculateAngleToDepot(coordinates, city2);
+
+            return angle1.CompareTo(angle2);
+          });
+
+      Tour currentTour = new Tour();
+      solution.Tours.Add(currentTour);
+      int j = random.Next(customers.Count);
+      for (int i = 0; i < customers.Count; i++) {
+        int index = (i + j) % customers.Count;
+
+        int stopIdx = FindBestInsertionPlace(currentTour, customers[index],
+          dueTime, serviceTime, readyTime, demand, capacity,
+            distMatrix);
+        currentTour.Cities.Insert(stopIdx, customers[index]);
+
+        TourEvaluation evaluation = VRPEvaluator.EvaluateTour(currentTour, 
+          dueTime, serviceTime, readyTime, demand, capacity, 
+          new DoubleValue(0), new DoubleValue(0), new DoubleValue(1), new DoubleValue(100), new DoubleValue(100),
+            distMatrix);
+        if (solution.Tours.Count < vehicles &&
+          ((evaluation.Overload > 0 || evaluation.Tardiness > 0))) {
+          currentTour.Cities.RemoveAt(stopIdx);
+
+          currentTour = new Tour();
+          solution.Tours.Add(currentTour);
+          currentTour.Cities.Add(customers[index]);
+        }
+      }
+
+      List<int> result = new List<int>();
+
+      bool first = true;
+      foreach (Tour tour in solution.GetTours()) {
+        if (first)
+          first = false;
+        else
+          result.Add(0);
+
+        result.AddRange(tour.Cities);
+      }
+
+      while (result.Count < cities + vehicles)
+        result.Add(0);
+
+      return result;
+    }
+  }
+}
Index: branches/GP.Grammar.Editor/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/PushForwardInsertionCreator.cs
===================================================================
--- branches/GP.Grammar.Editor/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/PushForwardInsertionCreator.cs	(revision 6284)
+++ branches/GP.Grammar.Editor/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/PushForwardInsertionCreator.cs	(revision 6618)
@@ -91,7 +91,6 @@
     }
 
-    private double CalculateDistance(int start, int end) {
+    private double CalculateDistance(int start, int end, DoubleMatrix coordinates) {
       double distance = 0.0;
-      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
 
       distance =
@@ -103,11 +102,10 @@
     }
 
-    private DoubleMatrix CreateDistanceMatrix() {
-      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
+    private DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) {
       DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows);
 
       for (int i = 0; i < distanceMatrix.Rows; i++) {
         for (int j = i; j < distanceMatrix.Columns; j++) {
-          double distance = CalculateDistance(i, j);
+          double distance = CalculateDistance(i, j, coordinates);
 
           distanceMatrix[i, j] = distance;
@@ -119,15 +117,11 @@
     }
 
-    private double Distance(int start, int end) {
+    private double Distance(int start, int end, DoubleMatrix coordinates, bool useDistanceMatrix) {
       double distance = 0.0;
 
-      if (UseDistanceMatrixParameter.ActualValue.Value) {
-        if (DistanceMatrixParameter.ActualValue == null) {
-          DistanceMatrixParameter.ActualValue = CreateDistanceMatrix();
-        }
-
-        distance = DistanceMatrixParameter.ActualValue[start, end];
-      } else {
-        distance = CalculateDistance(start, end);
+      if (useDistanceMatrix) {
+        distance = coordinates[start, end];
+      } else {  
+        distance = CalculateDistance(start, end, coordinates);
       }
 
@@ -135,23 +129,24 @@
     }
 
-    private double TravelDistance(List<int> route, int begin) {
+    private double TravelDistance(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix) {
       double distance = 0;
       for (int i = begin; i < route.Count - 1 && (i == begin || route[i] != 0); i++) {
-        distance += Distance(route[i], route[i + 1]);
+        distance += Distance(route[i], route[i + 1], coordinates, useDistanceMatrix);
       }
       return distance;
     }
 
-    private bool SubrouteConstraintsOK(List<int> route, int begin) {
+    private bool SubrouteConstraintsOK(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix, 
+      DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity) {
       double t = 0.0, o = 0.0;
       for (int i = begin + 1; i < route.Count; i++) {
-        t += Distance(route[i - 1], route[i]);
+        t += Distance(route[i - 1], route[i], coordinates, useDistanceMatrix);
         if (route[i] == 0) return (t < DueTimeParameter.ActualValue[0]); // violation on capacity constraint is handled below
         else {
-          if (t > DueTimeParameter.ActualValue[route[i]]) return false;
-          t = Math.Max(ReadyTimeParameter.ActualValue[route[i]], t);
-          t += ServiceTimeParameter.ActualValue[route[i]];
-          o += DemandParameter.ActualValue[route[i]];
-          if (o > CapacityParameter.ActualValue.Value) return false; // premature exit on capacity constraint violation
+          if (t > dueTime[route[i]]) return false;
+          t = Math.Max(readyTime[route[i]], t);
+          t += serviceTime[route[i]];
+          o += demand[route[i]];
+          if (o > capacity.Value) return false; // premature exit on capacity constraint violation
         }
       }
@@ -159,15 +154,16 @@
     }
 
-    private bool SubrouteTardinessOK(List<int> route, int begin) {
+    private bool SubrouteTardinessOK(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix, 
+      DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime) {
       double t = 0.0;
       for (int i = begin + 1; i < route.Count; i++) {
-        t += Distance(route[i - 1], route[i]);
+        t += Distance(route[i - 1], route[i], coordinates, useDistanceMatrix);
         if (route[i] == 0) {
-          if (t < DueTimeParameter.ActualValue[0]) return true;
+          if (t < dueTime[0]) return true;
           else return false;
         } else {
-          if (t > DueTimeParameter.ActualValue[route[i]]) return false;
-          t = Math.Max(ReadyTimeParameter.ActualValue[route[i]], t);
-          t += ServiceTimeParameter.ActualValue[route[i]];
+          if (t > dueTime[route[i]]) return false;
+          t = Math.Max(readyTime[route[i]], t);
+          t += serviceTime[route[i]];
         }
       }
@@ -175,20 +171,20 @@
     }
 
-    private bool SubrouteLoadOK(List<int> route, int begin) {
+    private bool SubrouteLoadOK(List<int> route, int begin, DoubleValue capacity, DoubleArray demand) {
       double o = 0.0;
       for (int i = begin + 1; i < route.Count; i++) {
-        if (route[i] == 0) return (o < CapacityParameter.ActualValue.Value);
+        if (route[i] == 0) return (o < capacity.Value);
         else {
-          o += DemandParameter.ActualValue[route[i]];
-        }
-      }
-      return (o < CapacityParameter.ActualValue.Value);
+          o += demand[route[i]];
+        }
+      }
+      return (o < capacity.Value);
     }
 
     protected override List<int> CreateSolution() {
-      double alpha, beta, gamma;
-      alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue);
-      beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue);
-      gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue);
+      //double alpha, beta, gamma;
+      double alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue);
+      double beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue);
+      double gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue);
 
       double x0 = CoordinatesParameter.ActualValue[0, 0];
@@ -203,4 +199,21 @@
       int indexOfCustomer = -1;
 
+      int vehicles = VehiclesParameter.ActualValue.Value;
+      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
+      DoubleArray dueTime = DueTimeParameter.ActualValue;
+      DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
+      DoubleArray readyTime = ReadyTimeParameter.ActualValue;
+      DoubleArray demand = DemandParameter.ActualValue;
+      DoubleValue capacity = CapacityParameter.ActualValue;
+
+      bool useDistanceMatrix = UseDistanceMatrixParameter.ActualValue.Value;
+      if (useDistanceMatrix) {
+        if (DistanceMatrixParameter.ActualValue == null) {
+          DistanceMatrixParameter.ActualValue = CreateDistanceMatrix(coordinates);
+        }
+
+        coordinates = DistanceMatrixParameter.ActualValue;
+      }
+
       /*-----------------------------------------------------------------------------
        * generate cost list
@@ -208,9 +221,9 @@
        */
       for (int i = 1; i <= Cities; i++) {
-        distance = Distance(i, 0);
-        if (CoordinatesParameter.ActualValue[i, 0] < x0) distance = -distance;
+        distance = Distance(i, 0, coordinates, useDistanceMatrix);
+        if (coordinates[i, 0] < x0) distance = -distance;
         cost = -alpha * distance + // distance 0 <-> City[i]
                  beta * (DueTimeParameter.ActualValue[i]) + // latest arrival time
-                 gamma * (Math.Asin((CoordinatesParameter.ActualValue[i, 1] - y0) / distance) / 360 * distance); // polar angle
+                 gamma * (Math.Asin((coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle
 
         index = 0;
@@ -229,5 +242,6 @@
       int customer = -1;
       int subTourCount = 1;
-      List<int> route = new List<int>(Cities + VehiclesParameter.ActualValue.Value - 1);
+
+      List<int> route = new List<int>(Cities + vehicles - 1);
       minimumCost = double.MaxValue;
       indexOfMinimumCost = -1;
@@ -244,6 +258,6 @@
             route.Insert(i, (int)unroutedList[c]);
             if (route[currentRoute] != 0) { throw new Exception("currentRoute not depot"); }
-            cost = TravelDistance(route, currentRoute);
-            if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute)) {
+            cost = TravelDistance(route, currentRoute, coordinates, useDistanceMatrix);
+            if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute, coordinates, useDistanceMatrix, dueTime, readyTime, serviceTime, demand, capacity)) {
               minimumCost = cost;
               indexOfMinimumCost = i;
@@ -275,5 +289,5 @@
         customer = -1;
       } while (unroutedList.Count > 0);
-      while (route.Count < Cities + VehiclesParameter.ActualValue.Value - 1)
+      while (route.Count < Cities + vehicles)
         route.Add(0);
 
