Index: branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/DifferentialEvolution.cs
===================================================================
--- branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/DifferentialEvolution.cs	(revision 13706)
+++ branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/DifferentialEvolution.cs	(revision 13710)
@@ -11,5 +11,4 @@
 using System;
 using System.Collections.Generic;
-using System.Linq;
 using System.Threading;
 
@@ -35,5 +34,6 @@
 
         private readonly IRandom _random = new MersenneTwister();
-     
+        private int evals;
+
         #region ParameterNames
         private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
@@ -206,5 +206,4 @@
             double[] bestPopulation = new double[Problem.ProblemSize.Value];
             double[] bestPopulationIteration = new double[Problem.ProblemSize.Value];
-            int evals = 0; // number of function evaluations
 
             //create initial population
@@ -212,6 +211,6 @@
             for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
             {
-                for (int j = 0; j < Problem.ProblemSize.Value; ++j)
-                  {
+                for (int j = 0; j < Problem.ProblemSize.Value; j++)
+                {
                     populationOld[i, j] = _random.NextDouble() * range + lb;
                 }
@@ -224,7 +223,7 @@
             double[] populationRow = new double[Problem.ProblemSize.Value];
 
-            bestPopulation = Get_ith_row(populationOld, best_index);
+            bestPopulation = getMatrixRow(populationOld, best_index);
             RealVector bestPopulationVector = new RealVector(bestPopulation);
-            double bestPopulationValue = Obj(bestPopulationVector, evals);
+            double bestPopulationValue = Obj(bestPopulationVector);
             RealVector selectionVector;
             RealVector trialVector;
@@ -234,13 +233,13 @@
             for (var i = 0; i < PopulationSizeParameter.Value.Value; i++)
             {
-                populationRow = Get_ith_row(populationOld, i);
+                populationRow = getMatrixRow(populationOld, i);
                 trialVector = new RealVector(populationRow);
 
-                qtrial = Obj(trialVector, evals);
+                qtrial = Obj(trialVector);
 
                 if (qtrial > bestPopulationValue)
                 {
                     bestPopulationVector = new RealVector(populationRow);
-                    bestPopulationValue = Obj(bestPopulationVector, evals);
+                    bestPopulationValue = Obj(bestPopulationVector);
                     best_index = i;
                 }
@@ -250,6 +249,6 @@
 
             // Loop until iteration limit reached or canceled.
-            //todo replace with a function
-            while (evals < MaximumEvaluations 
+            // todo replace with a function
+            while (ResultsEvaluations < MaximumEvaluations 
                 && !cancellationToken.IsCancellationRequested 
                 && bestPopulationValue > Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value)
@@ -259,12 +258,24 @@
                 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
                 {
-                    int r1 = _random.Next(0, PopulationSizeParameter.Value.Value),
-                        r2 = _random.Next(0, PopulationSizeParameter.Value.Value),
-                        r3 = _random.Next(0, PopulationSizeParameter.Value.Value);
-
-                    for (int j = 0; j < Get_ith_row(mutationPopulation, i).Length; j++)
+                    int r0, r1, r2;
+
+                    //assure the selected vectors r0, r1 and r2 are different
+                    do
+                     {
+                         r0 = _random.Next(0, PopulationSizeParameter.Value.Value);
+                     } while (r0 == i);
+                     do
+                     {
+                         r1 = _random.Next(0, PopulationSizeParameter.Value.Value);
+                     } while (r1 == i || r1 == r0);
+                     do
+                     {
+                         r2 = _random.Next(0, PopulationSizeParameter.Value.Value);
+                     } while (r2 == i || r2 == r0 || r2 == r1);
+
+                    for (int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++)
                     {
-                        mutationPopulation[i,j] = populationOld[r1,j] +
-                            ScalingFactorParameter.Value.Value * (populationOld[r2,j] - populationOld[r3,j]);
+                        mutationPopulation[i,j] = populationOld[r0,j] +
+                            ScalingFactorParameter.Value.Value * (populationOld[r1,j] - populationOld[r2,j]);
                         //check the problem upper and lower bounds
                         if (mutationPopulation[i,j] > ub) mutationPopulation[i,j] = ub;
@@ -276,6 +287,6 @@
                 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
                 {
-                    double rnbr = Math.Floor(_random.NextDouble() * Problem.ProblemSize.Value);
-                    for (int j = 0; j < Get_ith_row(mutationPopulation, i).Length; j++)
+                    double rnbr = _random.Next(0, Problem.ProblemSize.Value);
+                    for (int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++)
                     {
                         if (_random.NextDouble() <= CrossoverProbabilityParameter.Value.Value || j == rnbr)
@@ -293,14 +304,13 @@
                 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
                 {
-                    selectionVector = new RealVector(Get_ith_row(populationOld, i));
-                    trialVector = new RealVector(Get_ith_row(trialPopulation, i));
-
-                    var selectionEval = Obj(selectionVector, evals);
-                    var trailEval = Obj(trialVector, evals);
-
+                    selectionVector = new RealVector(getMatrixRow(populationOld, i));
+                    trialVector = new RealVector(getMatrixRow(trialPopulation, i));
+
+                    var selectionEval = Obj(selectionVector);
+                    var trailEval = Obj(trialVector);
 
                     if (trailEval < selectionEval)
                     {
-                        for (int j = 0; j < Get_ith_row(populationOld, i).Length; j++)
+                        for (int j = 0; j < getMatrixRow(populationOld, i).Length; j++)
                         {
                             populationOld[i, j] = trialPopulation[i, j];
@@ -312,7 +322,6 @@
                 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
                 {
-                    evals = evals + 1;
-                    selectionVector = new RealVector(Get_ith_row(populationOld, i));
-                    var quality = Obj(selectionVector, evals);
+                    selectionVector = new RealVector(getMatrixRow(populationOld, i));
+                    var quality = Obj(selectionVector);
                     if (quality < bestPopulationValue)
                     {
@@ -331,5 +340,5 @@
 
                 //update the results in view
-                if (evals % 100 == 0 ) ResultsQualitiesBest.Values.Add(bestPopulationValue);
+                if (iterations % 10 == 0 ) ResultsQualitiesBest.Values.Add(bestPopulationValue);
                 if (bestPopulationValue < Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value)
                 {
@@ -337,12 +346,11 @@
                 }
             }
-
         }
         
         //evaluate the vector
-        public double Obj(RealVector x, int evals)
+        public double Obj(RealVector x)
         {
             evals = evals + 1;
-            if(Problem.Maximization.Value)
+            if (Problem.Maximization.Value)
             return -Problem.Evaluator.Evaluate(x);
 
@@ -351,5 +359,5 @@
 
         // Get ith row from the matrix
-        public double[] Get_ith_row(double[,] Mat, int i)
+        public double[] getMatrixRow (double[,] Mat, int i)
         {
             double[] tmp = new double[Mat.GetUpperBound(1) + 1];
