DoCrossover() public static method

Perform DE crossover.
public static DoCrossover ( Variant crossover, double CR, int n, double w, double x, double &y, double g, double agents, RandomOps randomSet ) : void
crossover Variant Crossover variant to be performed.
CR double Crossover probability.
n int Dimensionality for problem.
w double Differential weight (vector).
x double Current agent position.
y double Potentially new agent position.
g double Population's best known position.
agents double Entire population.
randomSet RandomOps Random-set used for drawing distinct agents.
return void
示例#1
0
        /// <summary>
        /// Perform one optimization run and return the best found solution.
        /// </summary>
        /// <param name="parameters">Control parameters for the optimizer.</param>
        public override Result Optimize(double[] parameters)
        {
            Debug.Assert(parameters != null && parameters.Length == Dimensionality);

            // Signal beginning of optimization run.
            Problem.BeginOptimizationRun();

            // Retrieve parameters specific to JDE method.
            int numAgents = GetNumAgents(parameters);

            double FInit = GetFInit(parameters);
            double Fl    = GetFl(parameters);
            double Fu    = GetFu(parameters);
            double tauF  = GetTauF(parameters);

            double CRInit = GetCRInit(parameters);
            double CRl    = GetCRl(parameters);
            double CRu    = GetCRu(parameters);
            double tauCR  = GetTauCR(parameters);

            // Adjust CR parameters to remain within [0,1]
            if (CRl + CRu > 1)
            {
                CRu = 1 - CRl;
            }

            // Get problem-context.
            double[] lowerInit = Problem.LowerInit;
            double[] upperInit = Problem.UpperInit;
            int      n         = Problem.Dimensionality;

            // Allocate agent positions and associated fitnesses.
            double[][] agents   = Tools.NewMatrix(numAgents, n);
            double[]   fitness  = new double[numAgents];
            bool[]     feasible = new bool[numAgents];
            double[]   y        = new double[n];
            double[]   w        = new double[n];
            double[]   F        = new double[numAgents];
            double[]   CR       = new double[numAgents];

            // Initialize 'self-adaptive' parameters.
            Tools.Initialize(ref F, FInit);
            Tools.Initialize(ref CR, CRInit);

            // Random set for picking distinct agents.
            RandomOps.Set randomSet = new RandomOps.Set(Globals.Random, numAgents);

            // Iteration variables.
            int i, j;

            // Fitness variables.
            double[] g         = null;
            double   gFitness  = Problem.MaxFitness;
            bool     gFeasible = false;

            // Initialize all agents.
            // This counts as iterations below.
            for (j = 0; j < numAgents && Problem.Continue(j, gFitness, gFeasible); j++)
            {
                // Refer to the j'th agent as x.
                double[] x = agents[j];

                // Initialize agent-position in search-space.
                Tools.InitializeUniform(ref x, lowerInit, upperInit);

                // Enforce constraints and evaluate feasibility.
                feasible[j] = Problem.EnforceConstraints(ref x);

                // Compute fitness of initial position.
                fitness[j] = Problem.Fitness(x, feasible[j]);

                // Update population's best known position if it either does not exist or,
                // if feasibility is same or better and fitness is an improvement.
                if (Tools.BetterFeasibleFitness(gFeasible, feasible[j], gFitness, fitness[j]))
                {
                    g         = agents[j];
                    gFitness  = fitness[j];
                    gFeasible = feasible[j];
                }

                // Trace fitness of best found solution.
                Trace(j, gFitness, gFeasible);
            }

            for (i = numAgents; Problem.Continue(i, gFitness, gFeasible);)
            {
                Debug.Assert(numAgents > 0);

                for (j = 0; j < numAgents && Problem.Continue(i, gFitness, gFeasible); j++, i++)
                {
                    // Reset the random-set used for picking distinct agents.
                    // Exclude the j'th agent (also referred to as x).
                    randomSet.ResetExclude(j);

                    // Refer to the j'th agent as x.
                    double[] x = agents[j];

                    // JDE 'Self-adaptive' parameters.
                    double newF  = (Globals.Random.Bool(tauF)) ? (Globals.Random.Uniform(Fl, Fl + Fu)) : (F[j]);
                    double newCR = (Globals.Random.Bool(tauCR)) ? (Globals.Random.Uniform(CRl, CRl + CRu)) : (CR[j]);

                    // Initialize crossover-weight vector.
                    Tools.Initialize(ref w, newF);

                    // Perform crossover.
                    DECrossover.DoCrossover(_crossover, newCR, n, w, x, ref y, g, agents, randomSet);

                    // Enforce constraints and evaluate feasibility.
                    bool newFeasible = Problem.EnforceConstraints(ref y);

                    // Compute fitness if feasibility (constraint satisfaction) is same or better.
                    if (Tools.BetterFeasible(feasible[j], newFeasible))
                    {
                        // Compute new fitness.
                        double newFitness = Problem.Fitness(y, fitness[j], feasible[j], newFeasible);

                        // Update agent in case of fitness improvement.
                        if (Tools.BetterFeasibleFitness(feasible[j], newFeasible, fitness[j], newFitness))
                        {
                            // Update agent's position.
                            y.CopyTo(agents[j], 0);

                            // Update agent's fitness.
                            fitness[j] = newFitness;

                            // Update agent's feasibility.
                            feasible[j] = newFeasible;

                            // Update population's best known position,
                            // if feasibility is same or better and fitness is an improvement.
                            if (Tools.BetterFeasibleFitness(gFeasible, newFeasible, gFitness, newFitness))
                            {
                                g         = agents[j];
                                gFitness  = newFitness;
                                gFeasible = newFeasible;
                            }

                            // JDE 'Self-adaptive' parameters.
                            // Keep the new parameters because they led to fitness improvement.
                            F[j]  = newF;
                            CR[j] = newCR;
                        }
                    }

                    // Trace fitness of best found solution.
                    Trace(i, gFitness, gFeasible);
                }
            }

            // Signal end of optimization run.
            Problem.EndOptimizationRun();

            // Return best-found solution and fitness.
            return(new Result(g, gFitness, gFeasible, i));
        }
示例#2
0
        /// <summary>
        /// Perform one optimization run and return the best found solution.
        /// </summary>
        /// <param name="parameters">Control parameters for the optimizer.</param>
        public override Result Optimize(double[] parameters)
        {
            Debug.Assert(parameters != null && parameters.Length == Dimensionality);

            // Signal beginning of optimization run.
            Problem.BeginOptimizationRun();

            // Retrieve parameters specific to DE method.
            int    numAgents = GetNumAgents(parameters);
            double CR        = GetCR(parameters);
            double F         = GetF(parameters);
            double FMid      = GetFMid(parameters);
            double FRange    = GetFRange(parameters);

            // Get problem-context.
            double[] lowerInit = Problem.LowerInit;
            double[] upperInit = Problem.UpperInit;
            int      n         = Problem.Dimensionality;

            // Allocate agent positions and associated fitnesses and feasibility.
            double[][] agents   = Tools.NewMatrix(numAgents, n);
            double[]   fitness  = new double[numAgents];
            bool[]     feasible = new bool[numAgents];
            double[]   y        = new double[n];

            // Allocate differential weight vector.
            double[] w = new double[n];

            // Initialize differential weight vector, if no dithering is wanted.
            if (_dither == DitherVariant.None)
            {
                // Initialize crossover-weight vector.
                // Same value for all elements, vectors, and generations.
                Tools.Initialize(ref w, F);
            }

            // Random set for picking distinct agents.
            RandomOps.Set randomSet = new RandomOps.Set(Globals.Random, numAgents);

            // Iteration variables.
            int i, j;

            // Fitness variables.
            double[] g         = null;
            double   gFitness  = Problem.MaxFitness;
            bool     gFeasible = false;

            // Initialize all agents.
            // This counts as iterations below.
            for (j = 0; j < numAgents && Problem.Continue(j, gFitness, gFeasible); j++)
            {
                // Refer to the j'th agent as x.
                double[] x = agents[j];

                // Initialize agent-position in search-space.
                Tools.InitializeUniform(ref x, lowerInit, upperInit);

                // Enforce constraints and evaluate feasibility.
                feasible[j] = Problem.EnforceConstraints(ref x);

                // Compute fitness of initial position.
                fitness[j] = Problem.Fitness(x, feasible[j]);

                // Update population's best known position if it either does not exist or,
                // if feasibility is same or better and fitness is an improvement.
                if (Tools.BetterFeasibleFitness(gFeasible, feasible[j], gFitness, fitness[j]))
                {
                    g         = agents[j];
                    gFitness  = fitness[j];
                    gFeasible = feasible[j];
                }

                // Trace fitness of best found solution.
                Trace(j, gFitness, gFeasible);
            }

            for (i = numAgents; Problem.Continue(i, gFitness, gFeasible);)
            {
                Debug.Assert(numAgents > 0);

                // Perform dithering of differential weight, depending on dither variant wanted.
                if (_dither == DitherVariant.Generation)
                {
                    // Initialize differential-weight vector. Generation-based.
                    Tools.Initialize(ref w, Globals.Random.Uniform(FMid - FRange, FMid + FRange));
                }

                for (j = 0; j < numAgents && Problem.Continue(i, gFitness, gFeasible); j++, i++)
                {
                    // Perform dithering of differential weight, depending on dither variant wanted.
                    if (_dither == DitherVariant.Vector)
                    {
                        // Initialize differential-weight vector. Vector-based.
                        Tools.Initialize(ref w, Globals.Random.Uniform(FMid - FRange, FMid + FRange));
                    }
                    else if (_dither == DitherVariant.Element)
                    {
                        // Initialize differential-weight vector. Element-based.
                        Tools.InitializeUniform(ref w, FMid - FRange, FMid + FRange);
                    }

                    // Reset the random-set used for picking distinct agents.
                    // Exclude the j'th agent (also referred to as x).
                    randomSet.ResetExclude(j);

                    // Refer to the j'th agent as x.
                    double[] x = agents[j];

                    // Perform crossover.
                    DECrossover.DoCrossover(_crossover, CR, n, w, x, ref y, g, agents, randomSet);

                    // Enforce constraints and evaluate feasibility.
                    bool newFeasible = Problem.EnforceConstraints(ref y);

                    // Compute fitness if feasibility (constraint satisfaction) is same or better.
                    if (Tools.BetterFeasible(feasible[j], newFeasible))
                    {
                        // Compute new fitness.
                        double newFitness = Problem.Fitness(y, fitness[j], feasible[j], newFeasible);

                        // Update agent in case of fitness improvement.
                        if (Tools.BetterFeasibleFitness(feasible[j], newFeasible, fitness[j], newFitness))
                        {
                            // Update agent's position.
                            y.CopyTo(agents[j], 0);

                            // Update agent's fitness.
                            fitness[j] = newFitness;

                            // Update agent's feasibility.
                            feasible[j] = newFeasible;

                            // Update population's best known position,
                            // if feasibility is same or better and fitness is an improvement.
                            if (Tools.BetterFeasibleFitness(gFeasible, newFeasible, gFitness, newFitness))
                            {
                                g         = agents[j];
                                gFitness  = newFitness;
                                gFeasible = newFeasible;
                            }
                        }
                    }

                    // Trace fitness of best found solution.
                    Trace(i, gFitness, gFeasible);
                }
            }

            // Signal end of optimization run.
            Problem.EndOptimizationRun();

            // Return best-found solution and fitness.
            return(new Result(g, gFitness, gFeasible, i));
        }