/// <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> /// 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> /// Initializes a new instance of the IndividualBitmap. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="init">Initilaization genes.</param> public IndividualBitmap(int width, int height, bool init = true, Color[] initColors = null) : base(width, height, width * height * 3) { // Init if (init) { if (initColors == null) { for (int i = 0; i < Length; i++) { ReplaceGene(i, GenerateGene(i)); } } else { int indexR = 0; int indexG = 1; int indexB = 2; for (int i = 0; i < width * height; i++) { Color c = initColors[FastRandom.GetInt(0, initColors.Length)]; genes[indexR] = c.R; genes[indexG] = c.G; genes[indexB] = c.B; indexR += 3; indexG += 3; indexB += 3; } } } }
/// <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 IndividualBitmap. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="init">Initilaization genes.</param> public IndividualLines(int width, int height, int numberOfLines, bool init = true) : base(width, height, numberOfLines * 4) { this.numberOfLines = numberOfLines; // Init if (init) { int indexX1 = 0; int indexY1 = 1; int indexX2 = 2; int indexY2 = 3; for (int i = 0; i < numberOfLines; i++) { var sX = FastRandom.GetInt(0, width); var sY = FastRandom.GetInt(0, height); var shifts = FastRandom.GetInts(2, 0, 30); genes[indexX1] = sX; genes[indexY1] = sY; genes[indexX2] = sX + shifts[0]; genes[indexY2] = sY + shifts[1]; indexX1 += 4; indexY1 += 4; indexX2 += 4; indexY2 += 4; } } }
/// <summary> /// Mutate the specified individual. /// Shifted line /// </summary> /// <param name="individual">The individual.</param> /// <param name="mutation_probabilty">The probability to mutate each indiviudal.</param> public override void Mutate(IIndividual individual, float mutation_probabilty) { var indexes = FastRandom.GetInts(mutateRate, 0, individual.Length); foreach (var index in indexes) { var oldGene = individual.GetGene(index); // line endPoints shift var currShift = FastRandom.GetInt(-shift, shift); // sets new gene individual.ReplaceGene(index, oldGene + currShift); } }
/// <summary> /// Generates the gene. /// </summary> /// <param name="geneIndex">Index of the gene.</param> /// <returns>The new Gene.</returns> public override double GenerateGene(int geneIndex) { var colorIndex = FastRandom.GetInt(0, colorsDomain.Length); var color = colorsDomain[colorIndex]; if (geneIndex % 3 == 0) { return(color.R); } if (geneIndex % 3 == 1) { return(color.G); } return(color.B); }
/// <summary> /// Cross the specified parents generating the children. /// </summary> /// <param name="parents">The parents</param> /// <returns>The offspring (children) of the parents.</returns> public override IList <IIndividual> Cross(IList <IIndividual> parents) { // same length if (parents[0].Length != parents[1].Length) { return(parents); } int swapPoint = FastRandom.GetInt(1, parents[0].Length - 2); var firstChild = CreateChild(parents[0], parents[1], swapPoint); var secondChild = CreateChild(parents[1], parents[0], swapPoint); 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; } } }