Example #1
0
        // In the GECCO paper, Section 2.1
        public static double ImproveToLocalOptimum(BinaryProblem problem, BinaryVector solution, double fitness, IRandom rand)
        {
            var tried = new HashSet <int>();

            do
            {
                var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
                foreach (var option in options)
                {
                    if (tried.Contains(option))
                    {
                        continue;
                    }
                    solution[option] = !solution[option];
                    double newFitness = problem.Evaluate(solution, rand);
                    if (problem.IsBetter(newFitness, fitness))
                    {
                        fitness = newFitness;
                        tried.Clear();
                    }
                    else
                    {
                        solution[option] = !solution[option];
                    }
                    tried.Add(option);
                }
            } while (tried.Count != solution.Length);
            return(fitness);
        }
        private static double Donate(BinaryVector solution, double fitness, BinaryVector source, IEnumerable <int> cluster, BinaryProblem problem, IRandom rand, out bool changed)
        {
            // keep track of which bits flipped to make the donation
            List <int> flipped = new List <int>();

            foreach (var index in cluster)
            {
                if (solution[index] != source[index])
                {
                    flipped.Add(index);
                    solution[index] = !solution[index];
                }
            }
            changed = flipped.Count > 0;
            if (changed)
            {
                double newFitness = problem.Evaluate(solution, rand);
                // if the original is strictly better, revert the change
                if (problem.IsBetter(fitness, newFitness))
                {
                    foreach (var index in flipped)
                    {
                        solution[index] = !solution[index];
                    }
                }
                else
                {
                    // new solution is no worse than original, keep change to solution
                    fitness = newFitness;
                }
            }
            return(fitness);
        }
Example #3
0
        public override double Evaluate(BinaryVector vector, IRandom random)
        {
            if (Evaluations >= maxEvaluations)
            {
                throw new OperationCanceledException("Maximum Evaluation Limit Reached");
            }
            Evaluations++;
            double fitness = problem.Evaluate(vector, random);

            if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality))
            {
                BestQuality           = fitness;
                BestSolution          = (BinaryVector)vector.Clone();
                BestFoundOnEvaluation = Evaluations;
            }
            return(fitness);
        }
Example #4
0
 private static double Donate(BinaryVector solution, double fitness, BinaryVector source, IEnumerable<int> cluster, BinaryProblem problem, IRandom rand, out bool changed) {
   // keep track of which bits flipped to make the donation
   List<int> flipped = new List<int>();
   foreach (var index in cluster) {
     if (solution[index] != source[index]) {
       flipped.Add(index);
       solution[index] = !solution[index];
     }
   }
   changed = flipped.Count > 0;
   if (changed) {
     double newFitness = problem.Evaluate(solution, rand);
     // if the original is strictly better, revert the change
     if (problem.IsBetter(fitness, newFitness)) {
       foreach (var index in flipped) {
         solution[index] = !solution[index];
       }
     } else {
       // new solution is no worse than original, keep change to solution
       fitness = newFitness;
     }
   }
   return fitness;
 }
Example #5
0
 // In the GECCO paper, Section 2.1
 public static double ImproveToLocalOptimum(BinaryProblem problem, BinaryVector solution, double fitness, IRandom rand) {
   var tried = new HashSet<int>();
   do {
     var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
     foreach (var option in options) {
       if (tried.Contains(option)) continue;
       solution[option] = !solution[option];
       double newFitness = problem.Evaluate(solution, rand);
       if (problem.IsBetter(newFitness, fitness)) {
         fitness = newFitness;
         tried.Clear();
       } else {
         solution[option] = !solution[option];
       }
       tried.Add(option);
     }
   } while (tried.Count != solution.Length);
   return fitness;
 }