public virtual void AddOffspring(Animal offSpring) { if (Offspring == null) { Offspring = new HashedSet <Animal>(); } Offspring.Add(offSpring); }
public void Mutate() { Func <int, double> fitness = x => Sq(x); var population = Population .Create(fitness) .AddIndividuals(new[] { 10, 11, 12, 3, 2, 1, 13, 14, 15, 4, 5, 6 }); var offspring = new Offspring <int>( population, new int [] { 30, 31, 32, 33, 30, 31 }, new int [] { 100 }) .Mutate(3, (a, r) => a + 100); offspring.Children .Should().Equal(100, 130, 131, 132); }
public void SelectElite() { Func <int, double> fitness = x => Sq(x); var population = Population .Create(fitness) .AddIndividuals(new[] { 10, 11, 12, 3, 2, 1, 13, 14, 15, 4, 5, 6 }); var offspring = new Offspring <int>( population, new int [] { 30, 31, 32, 33, 30, 31 }, new int [] { 20, 21 }) .SelectElite(4); offspring.Children .Should().BeEquivalentTo(1, 2, 3, 4, 20, 21); }
void IParticle2D.OnDestroy() { if (Offspring != null) { Node container = GetNodeOrNull(OffspringSpawnPath); if (container == null) { GD.Print(Name + ": [Error] Invalid offspring spawn path."); } else { Node offspring = Offspring.Instance(); container.AddChild(offspring); if (offspring is Node2D n2d) { n2d.GlobalPosition = GlobalPosition; } offspring.CallOnSpawn(); } } }
/// <summary> /// Usage: three options /// - NSGAIIAdaptive /// - NSGAIIAdaptive problemName /// - NSGAIIAdaptive problemName paretoFrontFile /// </summary> /// <param name="args">Command line arguments.</param> public static void Main(string[] args) { Problem problem; // The problem to solve Algorithm algorithm; // The algorithm to use Operator selection; // Selection operator Dictionary <string, object> parameters; // Operator parameters QualityIndicator indicators; // Object to get quality indicators // Logger object and file to store log messages var logger = Logger.Log; var appenders = logger.Logger.Repository.GetAppenders(); var fileAppender = appenders[0] as log4net.Appender.FileAppender; fileAppender.File = "NSGAIIAdaptive.log"; fileAppender.ActivateOptions(); indicators = null; if (args.Length == 1) { object[] param = { "Real" }; problem = ProblemFactory.GetProblem(args[0], param); } else if (args.Length == 2) { object[] param = { "Real" }; problem = ProblemFactory.GetProblem(args[0], param); indicators = new QualityIndicator(problem, args[1]); } else { // Default problem problem = new Kursawe("Real", 3); //problem = new Kursawe("BinaryReal", 3); //problem = new Water("Real"); //problem = new ZDT1("ArrayReal", 100); //problem = new ConstrEx("Real"); //problem = new DTLZ1("Real"); //problem = new OKA2("Real") ; } problem = new LZ09_F3("Real"); algorithm = new JMetalCSharp.Metaheuristics.NSGAII.NSGAIIAdaptive(problem); //algorithm = new ssNSGAIIAdaptive(problem); // Algorithm parameters algorithm.SetInputParameter("populationSize", 100); algorithm.SetInputParameter("maxEvaluations", 150000); // Selection Operator parameters = null; selection = SelectionFactory.GetSelectionOperator("BinaryTournament2", parameters); // Add the operators to the algorithm algorithm.AddOperator("selection", selection); // Add the indicator object to the algorithm algorithm.SetInputParameter("indicators", indicators); Offspring[] getOffspring = new Offspring[3]; double CR, F; CR = 1.0; F = 0.5; getOffspring[0] = new DifferentialEvolutionOffspring(CR, F); getOffspring[1] = new SBXCrossoverOffspring(1.0, 20); //getOffspring[1] = new BLXAlphaCrossoverOffspring(1.0, 0.5); getOffspring[2] = new PolynomialMutationOffspring(1.0 / problem.NumberOfVariables, 20); //getOffspring[2] = new NonUniformMutationOffspring(1.0/problem.getNumberOfVariables(), 0.5, 150000); algorithm.SetInputParameter("offspringsCreators", getOffspring); // Execute the Algorithm long initTime = Environment.TickCount; SolutionSet population = algorithm.Execute(); long estimatedTime = Environment.TickCount - initTime; // Result messages logger.Info("Total execution time: " + estimatedTime + "ms"); logger.Info("Variables values have been writen to file VAR"); population.PrintVariablesToFile("VAR"); logger.Info("Objectives values have been writen to file FUN"); population.PrintObjectivesToFile("FUN"); Console.WriteLine("Time: " + estimatedTime); Console.ReadLine(); if (indicators != null) { logger.Info("Quality indicators"); logger.Info("Hypervolume: " + indicators.GetHypervolume(population)); logger.Info("GD : " + indicators.GetGD(population)); logger.Info("IGD : " + indicators.GetIGD(population)); logger.Info("Spread : " + indicators.GetSpread(population)); logger.Info("Epsilon : " + indicators.GetEpsilon(population)); } }
private void GenerationLoop() { #region Generate and evaluate lambda offspring for (int k = 0; k < lambda; k++) { _population[k] = new Offspring { // arx(:,k) = xmean + sigma * B * (D .* randn(N,1)); % m + sig * Normal(0,C) Variable = LinearAlgebra.Addition(xmean, LinearAlgebra.Multiply(B, LinearAlgebra.ArrayPiecewiseMultiplication(D, LinearAlgebra.Randn(N)), sigma)) }; LinearAlgebra.Normalize(ref _population[k].Variable); //Invoke the objective function call and the array of decision variables... _population[k].Fitness = _objFun(_population[k].Variable); CountEval++; } #endregion #region Sort by fitness and compute weighted mean into xmean _population = _population.ToList().OrderBy(p => p.Fitness).ToArray(); // minimization xold = xmean; double[,] arx = new double[N, mu]; for (int i = 0; i < N; i++) for (int j = 0; j < mu; j++) arx[i, j] = _population[j].Variable[i]; xmean = LinearAlgebra.Multiply(arx, weights); //xmean = arx(:,arindex(1:mu)) * weights; // recombination, new mean value #endregion #region Cumulation: Update evolution paths double[] xdiff = LinearAlgebra.Minus(xmean, xold); // ps = (1-cs) * ps + sqrt(cs*(2-cs)*mueff) * invsqrtC * (xmean-xold) / sigma; ps = LinearAlgebra.Addition(LinearAlgebra.Scalar(1 - cs, ps), LinearAlgebra.Multiply(invsqrtC, xdiff, Math.Sqrt(cs*(2 - cs)*mueff)/sigma)); //hsig = sum(ps.^2)/(1-(1-cs)^(2*CountEval/lambda))/N < 2 + 4/(N+1); bool hsig = LinearAlgebra.Power(ps, 2).Sum()/(1 - Math.Pow(1 - cs, 2.0*CountEval/lambda))/N < 2 + 4.0/(N + 1); // pc = (1-cc) * pc + hsig * sqrt(cc*(2-cc)*mueff) * (xmean-xold) / sigma; pc = LinearAlgebra.Scalar(1 - cc, pc); if (hsig) pc = LinearAlgebra.Addition(pc, LinearAlgebra.Scalar(Math.Sqrt(cc*(2 - cc)*mueff)/sigma, xdiff)); #endregion #region Adapt covariance matrix C //artmp = (1/sigma) * (arx(:,arindex(1:mu)) - repmat(xold,1,mu)); % mu difference vectors double[,] artmp = new double[N, mu]; for (int i = 0; i < N; i++) for (int j = 0; j < mu; j++) artmp[i, j] = (_population[j].Variable[i] - xold[i])/sigma; // C = (1-c1-cmu) * C ... % regard old matrix // + c1 * (pc * pc' ... % plus rank one update // + (1-hsig) * cc*(2-cc) * C) ... % minor correction if hsig==0 // + cmu * artmp * diag(weights) * artmp'; % plus rank mu update var regardOldMatrix = LinearAlgebra.Scalar(1 - c1 - cmu, C); double[,] rank1Update = LinearAlgebra.Multiply(pc, pc, c1); if (!hsig) // minor correction if hsig==0 rank1Update = LinearAlgebra.Addition(rank1Update, LinearAlgebra.Scalar(c1*cc*(2 - cc), C)); var rankMuUpdate = LinearAlgebra.Multiply(artmp, LinearAlgebra.Diag(weights), artmp, true, cmu); C = LinearAlgebra.Addition(regardOldMatrix, LinearAlgebra.Addition(rank1Update, rankMuUpdate)); #endregion #region Adapt step size sigma sigma = sigma*Math.Exp((cs/damps)*(LinearAlgebra.Norm2(ps)/chiN - 1)); #endregion #region Update B and D from C // to achieve O(N^2) if (!(CountEval - _eigenEval > lambda/(c1 + cmu)/N/10)) return; _eigenEval = CountEval; LinearAlgebra.EnforceSymmestry(ref C); //[B,D] = eig(C); // eigen decomposition, B==normalized eigenvectors alglib.smatrixevd(C, N, 1, true, out D, out B); D = LinearAlgebra.Power(D, 0.5); //D = sqrt(diag(D)); // D contains standard deviations now invsqrtC = LinearAlgebra.InvertSqrtMatrix(B, D); #endregion }