/// <summary> /// Initializes a new instance of the IndividualRectangles. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="init">Initilaization genes.</param> public IndividualRectangles(int width, int height, int numberOfShapes, bool init = true) : base(width, height, numberOfShapes * size) { geneSize = size; this.numberOfShapes = numberOfShapes; // Init if (init) { for (int i = 0; i < numberOfShapes; i++) { //left corner genes[(i * geneSize)] = FastRandom.GetInt(0, Width); genes[(i * geneSize) + 1] = FastRandom.GetInt(0, Height); // width genes[(i * geneSize) + 2] = FastRandom.GetInt(0, maxWidth); // height genes[(i * geneSize) + 3] = FastRandom.GetInt(0, maxHeight); // color genes[(i * geneSize) + 4] = FastRandom.GetDouble() * 255; genes[(i * geneSize) + 5] = FastRandom.GetDouble() * 255; genes[(i * geneSize) + 6] = FastRandom.GetDouble() * 255; } } }
/// <summary> /// Evolved one generation. /// </summary> /// <param name="population">Curr. pop</param> /// <returns>New generation</returns> protected override IPopulation EvolvedOneGeneration(IPopulation population) { List <IIndividual> newGeneration = new List <IIndividual>(); foreach (var orginal in population.Individuals) { // generate unique random numbers var randomValues = FastRandom.GetUniqueInts(3, 0, population.Size); int a = randomValues[0]; int b = randomValues[1]; int c = randomValues[2]; // choose random individuals (agents) from population IIndividual individual1 = population.Individuals[a]; IIndividual individual2 = population.Individuals[b]; IIndividual individual3 = population.Individuals[c]; int i = 0; int R = FastRandom.GetInt(0, population.Size); var candidate = population.CreateEmptyIndividual(); foreach (var orginalElement in orginal.GetGenes()) { double probXover = FastRandom.GetDouble(); if (probXover < XoverProbability || i == R) { // simple mutation double newElement = individual1.GetGene(i) + F * (individual2.GetGene(i) - individual3.GetGene(i)); candidate.ReplaceGene(i, newElement); } else { candidate.ReplaceGene(i, orginalElement); } i++; } var fit = fitness.Evaluate(candidate); if (fit < orginal.Fitness) { newGeneration.Add(candidate); } else { newGeneration.Add(orginal); } } CurrentGenerationsNumber++; population.Individuals = newGeneration; return(population); }
/// <summary> /// Initializes a new instance of the IndividualRriangles. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="init">Initilaization genes.</param> public IndividualTriangles(int width, int height, int numberOfShapes, bool init = true) : base(width, height, numberOfShapes * size) { geneSize = size; this.numberOfShapes = numberOfShapes; // Init if (init) { for (int i = 0; i < numberOfShapes; i++) { //1. vertex genes[(i * geneSize)] = FastRandom.GetInt(0, Width); genes[(i * geneSize) + 1] = FastRandom.GetInt(0, Height); // 2. vertex genes[(i * geneSize) + 2] = FastRandom.GetInt(0, Width); genes[(i * geneSize) + 3] = FastRandom.GetInt(0, Height); // 3. vertex genes[(i * geneSize) + 4] = FastRandom.GetInt(0, Width); genes[(i * geneSize) + 5] = FastRandom.GetInt(0, Height); // color genes[(i * geneSize) + 6] = FastRandom.GetDouble() * 255; genes[(i * geneSize) + 7] = FastRandom.GetDouble() * 255; genes[(i * geneSize) + 8] = FastRandom.GetDouble() * 255; } } }
/// <summary> /// Linear xover executor. /// </summary> /// <param name="population">Input population.</param> /// <param name="xover">Xover operator.</param> /// <param name="xoverProbability">Xover probability</param> /// <param name="parents">Parents list</param> /// <returns>Childten(individuals)</returns> public virtual IList <IIndividual> Cross(IPopulation population, IXover xover, float xoverProbability, IList <IIndividual> parents) { var size = population.Size; var offspring = new List <IIndividual>(size); for (int i = 0; i < size; i += xover.ChildrenNumber) { // selected parents from population var selectedParents = parents.Skip(i).Take(xover.ParentsNumber).ToList(); // If match the probability cross is made, otherwise the offspring is an exact copy of the parents. // Checks if the number of selected parents is equal which the crossover expect, because the in the end of the list we can // have some rest individual if (selectedParents.Count == xover.ParentsNumber && FastRandom.GetDouble() <= xoverProbability) { offspring.AddRange(xover.Cross(selectedParents)); } else { offspring.AddRange(selectedParents); } } return(offspring); }
/// <summary> /// Mutate the specified individual in population. /// </summary> /// <param name="individual">The individual to be mutated.</param> /// <param name="mut_probability">The mutation probability to mutate each individual.</param> public override void Mutate(IIndividual individual, float mutation_probabilty) { if (FastRandom.GetDouble() <= mutation_probabilty) { var indexes = FastRandom.GetUniqueInts(2, 0, individual.Length); SwapGenes(indexes[0], indexes[1], individual); } }
// <summary> /// Mutate the specified individual. /// </summary> /// <param name="individual">The individual to be mutated.</param> /// <param name="mutation_probabilty">The mutation probability to mutate each individual.</param> public override void Mutate(IIndividual individual, float mutation_probabilty) { for (int index = 0; index < individual.Length; index++) { if (FastRandom.GetDouble() <= mutation_probabilty) { individual.ReplaceGene(index, individual.GenerateGene(index)); } } }
/// <summary> /// Mutate one ind. /// </summary> /// <param name="ind">Individual</param> void Mutate(IIndividual ind) { // last gene is sigma parameter // var sigma = 1; var sigma = ind.GetGene(ind.Length - 1); for (int index = 0; index < ind.Length - 1; index++) { if (FastRandom.GetDouble() <= mutationProbability) { var g = ind.GetGene(index); var s = Normal.Sample(FastRandom.Instance, 0, sigma); ind.ReplaceGene(index, g + s); } } }
/// <summary> /// Cross the specified parents generating the children (Uniform cross). /// </summary> /// <param name="firstParent"> First parent</param> /// <param name="secondParent">Second parent</param> /// <returns>The offspring (children) of the parents.</returns> private IList<IIndividual> PerfomCross(IIndividual firstParent, IIndividual secondParent) { var firstChild = firstParent.CreateNew(); var secondChild = firstParent.CreateNew(); // uniform cross by pmixProb for (int i = 0; i < firstParent.Length; i++) { if (FastRandom.GetDouble() < mixProbability) { firstChild.ReplaceGene(i, firstParent.GetGene(i)); secondChild.ReplaceGene(i, secondParent.GetGene(i)); } else { firstChild.ReplaceGene(i, secondParent.GetGene(i)); secondChild.ReplaceGene(i, firstParent.GetGene(i)); } } return new List<IIndividual> { firstChild, secondChild }; }
/// <summary> /// Initializes a new instance of the IndividualCircles. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="init">Initilaization genes.</param> public IndividualCircles(int width, int height, int numberOfShapes, bool init = true) : base(width, height, numberOfShapes * size) { geneSize = size; this.numberOfShapes = numberOfShapes; // Init if (init) { for (int i = 0; i < numberOfShapes; i++) { genes[(i * geneSize)] = FastRandom.GetInt(0, Width); genes[(i * geneSize) + 1] = FastRandom.GetInt(0, Height); genes[(i * geneSize) + 2] = FastRandom.GetInt(0, maxRadius); genes[(i * geneSize) + 3] = FastRandom.GetDouble() * 255; genes[(i * geneSize) + 4] = FastRandom.GetDouble() * 255; genes[(i * geneSize) + 5] = FastRandom.GetDouble() * 255; } } }
public override double GenerateGene(int geneIndex) { var maxValue = GetMaxGeneValue(geneIndex); return(FastRandom.GetDouble() * maxValue); }