/// <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); Debug.Assert(numAgents > 0); // Get problem-context. double[] lowerBound = Problem.LowerBound; double[] upperBound = Problem.UpperBound; double[] lowerInit = Problem.LowerInit; double[] upperInit = Problem.UpperInit; int n = Problem.Dimensionality; // Allocate agent positions and associated fitnesses. double[][] agentsX = Tools.NewMatrix(numAgents, n); double[][] agentsY = Tools.NewMatrix(numAgents, n); double[] fitnessX = new double[numAgents]; double[] fitnessY = new double[numAgents]; bool[] feasibleX = new bool[numAgents]; bool[] feasibleY = new bool[numAgents]; // 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. (Non-parallel) for (j = 0; j < numAgents; j++) { // Initialize agent-position in search-space. Tools.InitializeUniform(ref agentsX[j], lowerInit, upperInit); // Enforce constraints and evaluate feasibility. feasibleX[j] = Problem.EnforceConstraints(ref agentsX[j]); } // This counts as iterations below. // Compute fitness of initial position. System.Threading.Tasks.Parallel.For(0, numAgents, Globals.ParallelOptions, (jPar) => { fitnessX[jPar] = Problem.Fitness(agentsX[jPar], feasibleX[jPar]); }); // Update best found position. (Non-parallel) for (j = 0; j < numAgents; j++) { if (Tools.BetterFeasibleFitness(gFeasible, feasibleX[j], gFitness, fitnessX[j])) { g = agentsX[j]; gFitness = fitnessX[j]; gFeasible = feasibleX[j]; } // Trace fitness of best found solution. Trace(j, gFitness, gFeasible); } for (i = numAgents; Problem.Continue(i, gFitness, gFeasible);) { // 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)); } // Update agent positions. (Non-parallel) for (j = 0; j < numAgents; j++) { // 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); // Perform crossover. DECrossover.DoCrossover(_crossover, CR, n, w, agentsX[j], ref agentsY[j], g, agentsX, randomSet); } // Compute new fitness. (Parallel) System.Threading.Tasks.Parallel.For(0, numAgents, Globals.ParallelOptions, (jPar) => { // Enforce constraints and evaluate feasibility. feasibleY[jPar] = Problem.EnforceConstraints(ref agentsY[jPar]); // Compute fitness if feasibility (constraint satisfaction) is same or better. if (Tools.BetterFeasible(feasibleX[jPar], feasibleY[jPar])) { fitnessY[jPar] = Problem.Fitness(agentsY[jPar], fitnessX[jPar], feasibleX[jPar], feasibleY[jPar]); } }); // Update agent positions. (Non-parallel) for (j = 0; j < numAgents; j++, i++) { // Update agent in case feasibility is same or better and fitness is improvement. if (Tools.BetterFeasibleFitness(feasibleX[j], feasibleY[j], fitnessX[j], fitnessY[j])) { // Update agent's position. agentsY[j].CopyTo(agentsX[j], 0); // Update agent's fitness. fitnessX[j] = fitnessY[j]; // Update agent's feasibility. feasibleX[j] = feasibleY[j]; // Update swarm's best known position. if (Tools.BetterFeasibleFitness(gFeasible, feasibleX[j], gFitness, fitnessX[j])) { g = agentsX[j]; gFitness = fitnessX[j]; gFeasible = feasibleX[j]; } } // 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)); }
/// <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); Debug.Assert(numAgents > 0); // Get problem-context. double[] lowerBound = Problem.LowerBound; double[] upperBound = Problem.UpperBound; double[] lowerInit = Problem.LowerInit; double[] upperInit = Problem.UpperInit; int n = Problem.Dimensionality; // Allocate agent positions and associated fitnesses. double[][] agentsX = Tools.NewMatrix(numAgents, n); double[][] agentsY = Tools.NewMatrix(numAgents, n); double[] fitnessX = new double[numAgents]; double[] fitnessY = new double[numAgents]; bool[] feasibleX = new bool[numAgents]; bool[] feasibleY = new bool[numAgents]; // 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. (Non-parallel) for (j = 0; j < numAgents; j++) { // Initialize agent-position in search-space. Tools.InitializeUniform(ref agentsX[j], lowerInit, upperInit); // Enforce constraints and evaluate feasibility. feasibleX[j] = Problem.EnforceConstraints(ref agentsX[j]); } // This counts as iterations below. // Compute fitness of initial position. System.Threading.Tasks.Parallel.For(0, numAgents, Globals.ParallelOptions, (jPar) => { fitnessX[jPar] = Problem.Fitness(agentsX[jPar], feasibleX[jPar]); }); // Update best found position. (Non-parallel) for (j = 0; j < numAgents; j++) { if (Tools.BetterFeasibleFitness(gFeasible, feasibleX[j], gFitness, fitnessX[j])) { g = agentsX[j]; gFitness = fitnessX[j]; gFeasible = feasibleX[j]; } // Trace fitness of best found solution. Trace(j, gFitness, gFeasible); } for (i = numAgents; Problem.Continue(i, gFitness, gFeasible); ) { // 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)); } // Update agent positions. (Non-parallel) for (j = 0; j < numAgents; j++) { // 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); // Perform crossover. DECrossover.DoCrossover(_crossover, CR, n, w, agentsX[j], ref agentsY[j], g, agentsX, randomSet); } // Compute new fitness. (Parallel) System.Threading.Tasks.Parallel.For(0, numAgents, Globals.ParallelOptions, (jPar) => { // Enforce constraints and evaluate feasibility. feasibleY[jPar] = Problem.EnforceConstraints(ref agentsY[jPar]); // Compute fitness if feasibility (constraint satisfaction) is same or better. if (Tools.BetterFeasible(feasibleX[jPar], feasibleY[jPar])) { fitnessY[jPar] = Problem.Fitness(agentsY[jPar], fitnessX[jPar], feasibleX[jPar], feasibleY[jPar]); } }); // Update agent positions. (Non-parallel) for (j = 0; j < numAgents; j++, i++) { // Update agent in case feasibility is same or better and fitness is improvement. if (Tools.BetterFeasibleFitness(feasibleX[j], feasibleY[j], fitnessX[j], fitnessY[j])) { // Update agent's position. agentsY[j].CopyTo(agentsX[j], 0); // Update agent's fitness. fitnessX[j] = fitnessY[j]; // Update agent's feasibility. feasibleX[j] = feasibleY[j]; // Update swarm's best known position. if (Tools.BetterFeasibleFitness(gFeasible, feasibleX[j], gFitness, fitnessX[j])) { g = agentsX[j]; gFitness = fitnessX[j]; gFeasible = feasibleX[j]; } } // 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); }
static void DoTest(RandomOps.Random Rand) { int i; Console.WriteLine("RNG name: {0}", Rand.Name); Console.WriteLine(); if (TestByte) { Console.WriteLine("Byte()"); int[] ByteCounts = new int[Byte.MaxValue + 1]; ZeroCounts(ByteCounts); for (i = 0; i < ByteIterations; i++) { ByteCounts[Rand.Byte()] += 1; } PrintCounts(ByteCounts); Console.WriteLine(); } Console.WriteLine("Bytes({0})", NumBytes); byte[] byteArr = Rand.Bytes(NumBytes); for (i = 0; i < NumBytes; i++) { Console.WriteLine(byteArr[i]); } Console.WriteLine(); Console.WriteLine("Uniform()"); for (i = 0; i < NumIterations; i++) { Console.WriteLine(Rand.Uniform()); } Console.WriteLine(); Console.WriteLine("Uniform(-3, -1)"); for (i = 0; i < NumIterations; i++) { Console.WriteLine(Rand.Uniform(-3, -1)); } Console.WriteLine(); Console.WriteLine("Uniform(-2, 2)"); for (i = 0; i < NumIterations; i++) { Console.WriteLine(Rand.Uniform(-2, 2)); } Console.WriteLine(); Console.WriteLine("Uniform(3, 5)"); for (i = 0; i < NumIterations; i++) { Console.WriteLine(Rand.Uniform(3, 5)); } Console.WriteLine(); Console.WriteLine("Gauss({0}, {1})", GaussMean, GaussDeviation); for (i = 0; i < NumIterations; i++) { Console.WriteLine(Rand.Gauss(GaussMean, GaussDeviation)); } Console.WriteLine(); double sum = 0; for (i = 0; i < MeanIterations; i++) { sum += Rand.Uniform(); } Console.WriteLine("Mean of {0} x Uniform(): {1:0.0000}", MeanIterations, sum / MeanIterations); Console.WriteLine(); sum = 0; for (i = 0; i < MeanIterations; i++) { sum += Rand.Gauss(0, 1); } Console.WriteLine("Mean of {0} x Gauss(0, 1): {1:0.0000}", MeanIterations, sum / MeanIterations); Console.WriteLine(); Console.WriteLine("Disk()"); PrintPoint(Rand.Disk()); Console.WriteLine(); Console.WriteLine("Circle()"); PrintPoint(Rand.Circle()); Console.WriteLine(); Console.WriteLine("Sphere3()"); PrintPoint(Rand.Sphere3()); Console.WriteLine(); Console.WriteLine("Sphere4()"); PrintPoint(Rand.Sphere4()); Console.WriteLine(); Console.WriteLine("Sphere({0}, {1})", SphereDim, SphereRadius); PrintPoint(Rand.Sphere(SphereDim, SphereRadius)); Console.WriteLine(); if (TestBool) { Console.WriteLine("Bool()"); int countTrue = 0; int countFalse = 0; for (i = 0; i < BoolIterations; i++) { if (Rand.Bool()) { countTrue++; } else { countFalse++; } } Console.WriteLine("True: {0}", countTrue); Console.WriteLine("False: {0}", countFalse); Console.WriteLine(); } Console.WriteLine("Index({0})", MaxIndex); ZeroCounts(IndexCounts); for (i = 0; i < IndexIterations; i++) { int idx = Rand.Index(MaxIndex); IndexCounts[idx] += 1; } PrintCounts(IndexCounts); Console.WriteLine(); Console.WriteLine("Index2({0}, ...)", MaxIndex); ZeroCounts(IndexCounts); for (i = 0; i < IndexIterations; i++) { int idx1, idx2; Rand.Index2(MaxIndex, out idx1, out idx2); Debug.Assert(idx1 != idx2); IndexCounts[idx1] += 1; IndexCounts[idx2] += 1; } PrintCounts(IndexCounts); Console.WriteLine(); RandomOps.Set RandSet = new RandomOps.Set(Rand, RandSetSize); Console.WriteLine("RandSet.Reset()"); RandSet.Reset(); Console.WriteLine(); Console.WriteLine("RandSet.Draw() with set of size {0} and {1} iterations", RandSetSize, SetIterations / 2); for (i = 0; i < SetIterations / 2; i++) { Console.WriteLine(RandSet.Draw()); } Console.WriteLine(); Console.WriteLine("RandSet.Reset()"); Console.WriteLine("RandSet.Draw() with set of size {0}", RandSetSize); RandSet.Reset(); for (i = 0; i < SetIterations; i++) { Console.WriteLine(RandSet.Draw()); } Console.WriteLine(); //RandSet.Draw(); // Assertion fails. Console.WriteLine("RandSet.ResetExclude({0})", RandSetExclude); Console.WriteLine("RandSet.Draw() with set of size {0}", RandSetSize); RandSet.ResetExclude(RandSetExclude); for (i = 0; i < SetIterations - 1; i++) { Console.WriteLine(RandSet.Draw()); } Console.WriteLine(); //RandSet.Draw(); // Assertion fails. ZeroCounts(SetCounts); Console.WriteLine("RandSet.Draw() with set of size {0}", RandSetSize); for (i = 0; i < SetIterations2; i++) { RandSet.Reset(); while (RandSet.Size > 0) { int idx = RandSet.Draw(); SetCounts[idx] += 1; } } PrintCounts(SetCounts); Console.WriteLine(); InitSetProbabilities(Rand); ZeroCounts(IndexDistributionCounts); Console.WriteLine("Rand.Index() with probability distribution"); for (i = 0; i < IndexDistributionIterations; i++) { int idx = Rand.Index(IndexProbabilities); IndexDistributionCounts[idx] += 1; } PrintDistributionCounts(); Console.WriteLine(); RandomOps.IndexDistribution IndexDistribution = new RandomOps.IndexDistribution(Rand, IndexProbabilities); ZeroCounts(IndexDistributionCounts); Console.WriteLine("IndexDistribution.DrawLinearSearch()"); for (i = 0; i < IndexDistributionIterations; i++) { int idx = IndexDistribution.DrawLinearSearch(); IndexDistributionCounts[idx] += 1; } PrintDistributionCounts(); Console.WriteLine(); ZeroCounts(IndexDistributionCounts); Console.WriteLine("IndexDistribution.DrawBinarySearch()"); for (i = 0; i < IndexDistributionIterations; i++) { int idx = IndexDistribution.DrawBinarySearch(); IndexDistributionCounts[idx] += 1; } PrintDistributionCounts(); Console.WriteLine(); }
/// <summary> /// Perform DE crossover. /// </summary> /// <param name="crossover">Crossover variant to be performed.</param> /// <param name="CR">Crossover probability.</param> /// <param name="n">Dimensionality for problem.</param> /// <param name="w">Differential weight (vector).</param> /// <param name="x">Current agent position.</param> /// <param name="y">Potentially new agent position.</param> /// <param name="g">Population's best known position.</param> /// <param name="agents">Entire population.</param> /// <param name="randomSet">Random-set used for drawing distinct agents.</param> public static void DoCrossover(Variant crossover, double CR, int n, double[] w, double[] x, ref double[] y, double[] g, double[][] agents, RandomOps.Set randomSet) { // Agents used in crossover. double[] a, b, c; switch (crossover) { case Variant.Best1Bin: { // The first agent used in crossover is g. a = g; // Pick random and distinct agent-indices. // Also distinct from agent x. int R1 = randomSet.Draw(); int R2 = randomSet.Draw(); b = agents[R1]; c = agents[R2]; } break; case Variant.Rand1Bin: default: { // Pick random and distinct agent-indices. // Also distinct from agent x. int R1 = randomSet.Draw(); int R2 = randomSet.Draw(); int R3 = randomSet.Draw(); // Refer to the randomly picked agents as a and b. a = agents[R1]; b = agents[R2]; c = agents[R3]; } break; } // Pick a random dimension. int R = Globals.Random.Index(n); // Compute potentially new position. for (int k = 0; k < n; k++) { if (k == R || Globals.Random.Bool(CR)) { y[k] = a[k] + w[k] * (b[k] - c[k]); } else { y[k] = x[k]; } } }
/// <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); // 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[] lowerBound = Problem.LowerBound; double[] upperBound = Problem.UpperBound; 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[] agentFitness = new double[numAgents]; double[] t = 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 gFitness = Problem.MaxFitness; double[] g = null; // Initialize all agents. // This counts as iterations below. for (j = 0; j < numAgents && Problem.RunCondition.Continue(j, gFitness); 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); // Compute fitness of initial position. agentFitness[j] = Problem.Fitness(x); // Update swarm's best known position. if (g == null || agentFitness[j] < gFitness) { g = x; gFitness = agentFitness[j]; } // Trace fitness of best found solution. Trace(j, gFitness); } for (i = numAgents; Problem.RunCondition.Continue(i, gFitness);) { 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.RunCondition.Continue(i, gFitness); 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]; // Store old position of agent x. x.CopyTo(t, 0); // Perform crossover. DECrossover.DoCrossover(_crossover, CR, n, w, ref x, g, agents, randomSet); // Enforce bounds before computing new fitness. Tools.Bound(ref x, lowerBound, upperBound); // Compute new fitness. double newFitness = Problem.Fitness(x, agentFitness[j]); // Update agent in case of fitness improvement. if (newFitness < agentFitness[j]) { // Update agent's fitness. Position is already updated. agentFitness[j] = newFitness; // Update swarm's best known position. if (newFitness < gFitness) { g = x; gFitness = newFitness; } } else // Fitness was not an improvement. { // Restore old position. t.CopyTo(x, 0); } // Trace fitness of best found solution. Trace(i, gFitness); } } // Return best-found solution and fitness. return new Result(g, gFitness, i); }
/// <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)); }
/// <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); }
/// <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); // 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[] lowerBound = Problem.LowerBound; double[] upperBound = Problem.UpperBound; 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[] agentFitness = new double[numAgents]; double[] t = 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 gFitness = Problem.MaxFitness; double[] g = null; // Initialize all agents. // This counts as iterations below. for (j = 0; j < numAgents && Problem.RunCondition.Continue(j, gFitness); 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); // Compute fitness of initial position. agentFitness[j] = Problem.Fitness(x); // Update swarm's best known position. if (g == null || agentFitness[j] < gFitness) { g = x; gFitness = agentFitness[j]; } // Trace fitness of best found solution. Trace(j, gFitness); } for (i = numAgents; Problem.RunCondition.Continue(i, gFitness);) { Debug.Assert(numAgents > 0); for (j = 0; j < numAgents && Problem.RunCondition.Continue(i, gFitness); 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]; // Store old position of agent x. x.CopyTo(t, 0); // 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, ref x, g, agents, randomSet); // Enforce bounds before computing new fitness. Tools.Bound(ref x, lowerBound, upperBound); // Compute new fitness. double newFitness = Problem.Fitness(x, agentFitness[j]); // Update agent in case of fitness improvement. if (newFitness < agentFitness[j]) { // Update agent's fitness. Position is already updated. agentFitness[j] = newFitness; // Update swarm's best known position. if (newFitness < gFitness) { g = x; gFitness = newFitness; } // JDE 'Self-adaptive' parameters. // Keep the new parameters because they led to fitness improvement. F[j] = newF; CR[j] = newCR; } else // Fitness was not an improvement. { // Restore old position. t.CopyTo(x, 0); } // Trace fitness of best found solution. Trace(i, gFitness); } } // Return best-found solution and fitness. return new Result(g, gFitness, i); }