Exemple #1
0
 //! \name CostFunction interface
 //@{
 public override double value(Vector freeParameters)
 {
     mapFreeParameters(freeParameters);
     return(costFunction_.value(actualParameters_));
 }
Exemple #2
0
 //! call cost function computation and increment evaluation counter
 public double value(Vector x)
 {
     ++functionEvaluation_;
     return(costFunction_.value(x));
 }
        protected void crossover(List <Candidate> oldPopulation,
                                 List <Candidate> population,
                                 List <Candidate> mutantPopulation,
                                 List <Candidate> mirrorPopulation,
                                 CostFunction costFunction)
        {
            if (configuration().crossoverIsAdaptive)
            {
                adaptCrossover();
            }

            Vector mutationProbabilities = getMutationProbabilities(population);

            List <Vector> crossoverMask = new InitializedList <Vector>(population.Count);

            crossoverMask.ForEach((ii, vv) => crossoverMask[ii] = new Vector(population.First().values.size(), 1.0));

            List <Vector> invCrossoverMask = new InitializedList <Vector>(population.Count);

            invCrossoverMask.ForEach((ii, vv) => invCrossoverMask[ii] = new Vector(population.First().values.size(), 1.0));

            getCrossoverMask(crossoverMask, invCrossoverMask, mutationProbabilities);

            // crossover of the old and mutant population
            for (int popIter = 0; popIter < population.Count; popIter++)
            {
                population[popIter].values = Vector.DirectMultiply(oldPopulation[popIter].values, invCrossoverMask[popIter])
                                             + Vector.DirectMultiply(mutantPopulation[popIter].values,
                                                                     crossoverMask[popIter]);
                // immediately apply bounds if specified
                if (configuration().applyBounds)
                {
                    for (int memIter = 0; memIter < population[popIter].values.size(); memIter++)
                    {
                        if (population[popIter].values[memIter] > upperBound_[memIter])
                        {
                            population[popIter].values[memIter] = upperBound_[memIter]
                                                                  + rng_.nextReal()
                                                                  * (mirrorPopulation[popIter].values[memIter]
                                                                     - upperBound_[memIter]);
                        }
                        if (population[popIter].values[memIter] < lowerBound_[memIter])
                        {
                            population[popIter].values[memIter] = lowerBound_[memIter]
                                                                  + rng_.nextReal()
                                                                  * (mirrorPopulation[popIter].values[memIter]
                                                                     - lowerBound_[memIter]);
                        }
                    }
                }

                // evaluate objective function as soon as possible to avoid unnecessary loops
                try
                {
                    population[popIter].cost = costFunction.value(population[popIter].values);

                    if (Double.IsNaN(population[popIter].cost))
                    {
                        population[popIter].cost = Double.MaxValue;
                    }
                }
                catch
                {
                    population[popIter].cost = Double.MaxValue;
                }
            }
        }