Index: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GreedyRandomizedSolutionCreator.cs
===================================================================
--- branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GreedyRandomizedSolutionCreator.cs	(revision 15553)
+++ branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/SolutionCreators/GreedyRandomizedSolutionCreator.cs	(revision 15555)
@@ -33,4 +33,7 @@
 
 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
+  /// <summary>
+  /// This is an implementation of the algorithm described in Mateus, G.R., Resende, M.G.C. & Silva, R.M.A. J Heuristics (2011) 17: 527. https://doi.org/10.1007/s10732-010-9144-0
+  /// </summary>
   [Item("GreedyRandomizedSolutionCreator", "Creates a solution according to the procedure described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.")]
   [StorableClass]
@@ -60,6 +63,9 @@
     public static IntegerVector CreateSolution(IRandom random, GQAPInstance problemInstance,
       int maximumTries, bool createMostFeasibleSolution, CancellationToken cancelToken) {
+      var weights = problemInstance.Weights;
+      var distances = problemInstance.Distances;
       var demands = problemInstance.Demands;
       var capacities = problemInstance.Capacities.ToArray();
+      var transportCosts = problemInstance.TransportationCosts;
       var equipments = demands.Length;
       var locations = capacities.Length;
@@ -69,48 +75,92 @@
       double minViolation = double.MaxValue;
       int[] bestAssignment = null;
-      var CF = new bool[equipments]; // set of chosen facilities / equipments
-      var T = new List<int>(equipments); // set of facilities / equpiments that can be assigned to the set of chosen locations (CL)
+      var F = new List<int>(equipments); // set of (initially) all facilities / equipments
+      var CF = new List<int>(equipments); // set of chosen facilities / equipments
+      var L = new List<int>(locations); // set of (initially) all locations
       var CL_list = new List<int>(locations); // list of chosen locations
       var CL_selected = new bool[locations]; // bool decision if location is chosen
-      var F = new List<int>(equipments); // set of (initially) all facilities / equipments
-      var L = new List<int>(locations); // set of (initially) all locations
-      
+      var T = new List<int>(equipments); // set of facilities / equpiments that can be assigned to the set of chosen locations (CL)
+      var H = new double[locations]; // proportions for choosing locations in stage 1
+      var W = new double[equipments]; // proportions for choosing facilities in stage 2
+      var Z = new double[locations]; // proportions for choosing locations in stage 2
+
       while (maximumTries <= 0 || tries < maximumTries) {
         cancelToken.ThrowIfCancellationRequested();
 
-        Array.Copy(capacities, slack, locations);
-        Array.Clear(CF, 0, equipments);
-        Array.Clear(CL_selected, 0, locations);
-        CL_list.Clear();
-        T.Clear();
-
-        F.Clear(); F.AddRange(Enumerable.Range(0, equipments));
-        L.Clear(); L.AddRange(Enumerable.Range(0, locations));
-
-        double threshold = 1.0;
-        do {
-          if (L.Count > 0 && random.NextDouble() < threshold) {
-            int l = L.SampleRandom(random);
-            L.Remove(l);
-            CL_list.Add(l);
-            CL_selected[l] = true;
-            T = new List<int>(WhereDemandEqualOrLess(F, GetMaximumSlack(slack, CL_selected), demands));
-          }
-          if (T.Count > 0) {
-            var fidx = Enumerable.Range(0, T.Count).SampleRandom(random);
-            var f = T[fidx];
-            T.RemoveAt(fidx);
-            F.Remove(f); // TODO: slow
-            CF[f] = true;
-            int l = WhereSlackGreaterOrEqual(CL_list, demands[f], slack).SampleRandom(random);
-            assignment[f] = l + 1;
+        Array.Copy(capacities, slack, locations); // line 2 of Algorihm 2
+        CF.Clear(); // line 2 of Algorihm 2
+        Array.Clear(CL_selected, 0, locations); // line 2 of Algorihm 2
+        CL_list.Clear(); // line 2 of Algorihm 2
+        T.Clear(); // line 2 of Algorihm 2
+
+        F.Clear(); F.AddRange(Enumerable.Range(0, equipments)); // line 2 of Algorihm 2
+        L.Clear(); L.AddRange(Enumerable.Range(0, locations)); // line 2 of Algorihm 2
+
+        double threshold = 1.0; // line 3 of Algorithm 2
+        do { // line 4 of Algorithm 2
+          if (L.Count > 0 && random.NextDouble() < threshold) { // line 5 of Algorithm 2
+            // H is the proportion that a location is chosen
+            // The paper doesn't mention what happens if the candidate list CL
+            // does not contain an element in which case according to the formula
+            // all H_k elements would be 0 which would be equal to random selection
+            var HH = H.Select((v, i) => new { Index = i, Value = v })
+              .Where(x => !CL_selected[x.Index])
+              .Select(x => x.Value);
+            int l = L.SampleProportional(random, 1, HH, false, false).Single(); // line 6 of Algorithm 2
+            L.Remove(l); // line 7 of Algorithm 2
+            CL_list.Add(l); // line 7 of Algorithm 2
+            CL_selected[l] = true; // line 7 of Algorithm 2
+            for (var k = 0; k < locations; k++)
+              if (!CL_selected[k])
+                H[k] += capacities[k] * capacities[l] / distances[k, l];
+            T = new List<int>(WhereDemandEqualOrLess(F, GetMaximumSlack(slack, CL_selected), demands)); // line 8 of Algorithm 2
+          }
+          if (T.Count > 0) { // line 10 of Algorithm 2
+            // W is the proportion that an equipment is chosen
+            Array.Clear(W, 0, W.Length);
+            var wk = 0;
+            foreach (var k in T) {
+              for (var h = 0; h < equipments; h++) {
+                if (k == h) continue;
+                W[wk] += weights[k, h];
+              }
+              W[wk] *= demands[k];
+              wk++;
+            }
+            var f = T.SampleProportional(random, 1, W.Take(T.Count), false, false) // line 11 of Algorithm 2
+              .Single();
+            T.Remove(f); // line 12 of Algorithm 2
+            F.Remove(f); // line 12 of Algorithm 2
+            CF.Add(f); // line 12 of Algorithm 2
+            var R = WhereSlackGreaterOrEqual(CL_list, demands[f], slack).ToList(); // line 13 of Algorithm 2
+            // Z is the proportion that a location is chosen in stage 2
+            Array.Clear(Z, 0, Z.Length);
+            var zk = 0;
+            foreach (var k in R) {
+              // d is an increase in fitness if f would be assigned to location k
+              var d = problemInstance.InstallationCosts[f, k];
+              foreach (var i in CF) {
+                if (assignment[i] == 0) continue; // i is unassigned
+                var j = assignment[i] - 1;
+                d += transportCosts * weights[f, i] * distances[k, j];
+              }
+              foreach (var h in CL_list) {
+                if (k == h) continue;
+                Z[zk] += slack[k] * capacities[h] / (d * distances[k, h]);
+              }
+              zk++;
+            }
+            int l = R.SampleProportional(random, 1, Z.Take(R.Count), false, false).Single(); // line 14 of Algorithm 2
+            assignment[f] = l + 1; // line 15 of Algorithm 2
             slack[l] -= demands[f];
-            T = new List<int>(WhereDemandEqualOrLess(F, GetMaximumSlack(slack, CL_selected), demands));
-            threshold = 1.0 - (double)T.Count / Math.Max(F.Count, 1.0);
-          }
-        } while (T.Count > 0 || L.Count > 0);
+            T = new List<int>(WhereDemandEqualOrLess(F, GetMaximumSlack(slack, CL_selected), demands)); // line 16 of Algorithm 2
+            threshold = 1.0 - (double)T.Count / Math.Max(F.Count, 1.0); // line 17 of Algorithm 2
+          }
+        } while (T.Count > 0 || L.Count > 0); // line 19 of Algorithm 2
+
         if (maximumTries > 0) tries++;
+
         if (F.Count == 0) {
-          bestAssignment = assignment;
+          bestAssignment = assignment.Select(x => x - 1).ToArray();
           break;
         } else if (createMostFeasibleSolution) {
@@ -130,15 +180,15 @@
           double violation = slack.Select(x => x < 0 ? -x : 0).Sum();
           if (violation < minViolation) {
-            bestAssignment = assignment;
-            assignment = new int[equipments];
+            bestAssignment = assignment.Select(x => x - 1).ToArray();
             minViolation = violation;
           }
+          assignment = new int[equipments];
         }
       }
 
-      if (bestAssignment == null || bestAssignment.Any(x => x == 0))
+      if (bestAssignment == null)
         throw new InvalidOperationException(String.Format("No solution could be found in {0} tries.", maximumTries));
 
-      return new IntegerVector(bestAssignment.Select(x => x - 1).ToArray());
+      return new IntegerVector(bestAssignment);
     }
 
