Пример #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);
        }
Пример #2
0
        // Utility function that sets up and executes the run, then asserts the results
        private PrivateObject DoRun(BinaryProblem problem, int maximumEvaluations, int seed, double bestQuality, int foundOn)
        {
            var solver = new HeuristicLab.Algorithms.ParameterlessPopulationPyramid.ParameterlessPopulationPyramid();

            solver.Problem            = problem;
            solver.MaximumEvaluations = maximumEvaluations;
            solver.Seed            = seed;
            solver.SetSeedRandomly = false;
            PrivateObject hidden = new PrivateObject(solver);

            try {
                var ct = new CancellationToken();
                hidden.Invoke("Initialize", ct);
                hidden.Invoke("Run", ct);
            } catch (OperationCanceledException) {
                // Ignore
            }

            Assert.AreEqual(maximumEvaluations, hidden.GetProperty("ResultsEvaluations"), "Total Evaluations");
            double foundQuality = (double)hidden.GetProperty("ResultsBestQuality");

            Assert.IsTrue(foundQuality.IsAlmost(bestQuality), string.Format("Expected <{0}> Actual <{1}>", bestQuality, foundQuality));
            Assert.AreEqual(foundOn, hidden.GetProperty("ResultsBestFoundOnEvaluation"), "Found On");

            return(hidden);
        }
Пример #3
0
 private EvaluationTracker(EvaluationTracker original, Cloner cloner)
   : base(original, cloner) {
   problem = cloner.Clone(original.problem);
   maxEvaluations = original.maxEvaluations;
   BestQuality = original.BestQuality;
   Evaluations = original.Evaluations;
   BestFoundOnEvaluation = original.BestFoundOnEvaluation;
   BestSolution = cloner.Clone(BestSolution);
 }
Пример #4
0
 private EvaluationTracker(EvaluationTracker original, Cloner cloner)
     : base(original, cloner)
 {
     problem               = cloner.Clone(original.problem);
     maxEvaluations        = original.maxEvaluations;
     BestQuality           = original.BestQuality;
     Evaluations           = original.Evaluations;
     BestFoundOnEvaluation = original.BestFoundOnEvaluation;
     BestSolution          = cloner.Clone(original.BestSolution);
 }
Пример #5
0
    public EvaluationTracker(BinaryProblem problem, int maxEvaluations) {
      this.problem = problem;
      this.maxEvaluations = maxEvaluations;
      BestSolution = new BinaryVector(Length);
      BestQuality = double.NaN;
      Evaluations = 0;
      BestFoundOnEvaluation = 0;

      if (Parameters.ContainsKey("Maximization")) Parameters.Remove("Maximization");
      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
    }
Пример #6
0
 // In the GECCO paper, Figure 3
 public static double ImproveUsingTree(LinkageTree tree, IList<BinaryVector> donors, BinaryVector solution, double fitness, BinaryProblem problem, IRandom rand) {
   var options = Enumerable.Range(0, donors.Count).ToArray();
   foreach (var cluster in tree.Clusters) {
     // Find a donor which has at least one gene value different
     // from the current solution for this cluster of genes
     bool donorFound = false;
     foreach (var donorIndex in options.ShuffleList(rand)) {
       // Attempt the donation
       fitness = Donate(solution, fitness, donors[donorIndex], cluster, problem, rand, out donorFound);
       if (donorFound) break;
     }
   }
   return fitness;
 }
Пример #7
0
        public EvaluationTracker(BinaryProblem problem, int maxEvaluations)
        {
            this.problem          = problem;
            this.maxEvaluations   = maxEvaluations;
            BestSolution          = new BinaryVector(Length);
            BestQuality           = double.NaN;
            Evaluations           = 0;
            BestFoundOnEvaluation = 0;

            if (Parameters.ContainsKey("Maximization"))
            {
                Parameters.Remove("Maximization");
            }
            Parameters.Add(new FixedValueParameter <BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue) new BoolValue(Maximization).AsReadOnly())
            {
                Hidden = true
            });
        }
    // Utility function that sets up and executes the run, then asserts the results
    private PrivateObject DoRun(BinaryProblem problem, int maximumEvaluations, int seed, double bestQuality, int foundOn) {
      var solver = new HeuristicLab.Algorithms.ParameterlessPopulationPyramid.ParameterlessPopulationPyramid();
      solver.Problem = problem;
      solver.MaximumEvaluations = maximumEvaluations;
      solver.Seed = seed;
      solver.SetSeedRandomly = false;
      PrivateObject hidden = new PrivateObject(solver);
      try {
        hidden.Invoke("Run", new CancellationToken());
      } catch (OperationCanceledException) {
        // Ignore
      }

      Assert.AreEqual(maximumEvaluations, hidden.GetProperty("ResultsEvaluations"), "Total Evaluations");
      double foundQuality = (double)hidden.GetProperty("ResultsBestQuality");
      Assert.IsTrue(foundQuality.IsAlmost(bestQuality), string.Format("Expected <{0}> Actual <{1}>", bestQuality, foundQuality));
      Assert.AreEqual(foundOn, hidden.GetProperty("ResultsBestFoundOnEvaluation"), "Found On");

      return hidden;
    }
Пример #9
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;
 }
Пример #10
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);
        }
Пример #11
0
        // In the GECCO paper, Figure 3
        public static double ImproveUsingTree(LinkageTree tree, IList <BinaryVector> donors, BinaryVector solution, double fitness, BinaryProblem problem, IRandom rand)
        {
            var options = Enumerable.Range(0, donors.Count).ToArray();

            foreach (var cluster in tree.Clusters)
            {
                // Find a donor which has at least one gene value different
                // from the current solution for this cluster of genes
                bool donorFound = false;
                foreach (var donorIndex in options.ShuffleList(rand))
                {
                    // Attempt the donation
                    fitness = Donate(solution, fitness, donors[donorIndex], cluster, problem, rand, out donorFound);
                    if (donorFound)
                    {
                        break;
                    }
                }
            }
            return(fitness);
        }
Пример #12
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;
 }