public Individual[] Operate(Individual[] parents) { Individual[] offspring = new Individual[parents.Length]; for (int i = 0; i < parents.Length / 2; i++) { IntegerIndividual p1 = (IntegerIndividual)parents[2 * i]; IntegerIndividual p2 = (IntegerIndividual)parents[2 * i + 1]; IntegerIndividual o1 = (IntegerIndividual)p1.Clone(); IntegerIndividual o2 = (IntegerIndividual)p2.Clone(); if (Rnd.NextDouble() < XOverProbability) { int point = Rnd.Next(p1.Genes.Length); for (int j = point; j < p1.Genes.Length; j++) { int tmp = o1.Genes[j]; o1.Genes[j] = o2.Genes[j]; o2.Genes[j] = tmp; } } offspring[2 * i] = o1; offspring[2 * i + 1] = o2; } return(offspring); }
public Individual[] Operate(Individual[] parents) { Individual[] offspring = new Individual[parents.Length]; for (int i = 0; i < parents.Length; i++) { IntegerIndividual p1 = (IntegerIndividual)parents[i]; IntegerIndividual o1 = (IntegerIndividual)p1.Clone(); if (Rnd.NextDouble() < MutationProbability) { for (int j = 0; j < o1.Genes.Length; j++) { if (Rnd.NextDouble() < GeneChangeProbability) { o1.Genes[j] = Rnd.Next(o1.Max - o1.Min) + o1.Min; } } } offspring[i] = o1; } return(offspring); }
public Individual[] Operate(Individual[] parents) { Individual[] offspring = new Individual[parents.Length]; for (int i = 0; i < parents.Length; i++) { IntegerIndividual p1 = (IntegerIndividual)parents[i]; IntegerIndividual o1 = (IntegerIndividual)p1.Clone(); if (Rnd.NextDouble() < MutationProbability) { int i1 = 0, i2 = 0; int size = o1.Genes.Length; while (i1 == i2) { i1 = Rnd.Next(size); i2 = Rnd.Next(size); } int tmp = o1.Genes[i1]; o1.Genes[i1] = o1.Genes[i2]; o1.Genes[i2] = tmp; } offspring[i] = o1; } return(offspring); }
public void Populate(ArrayList individuals) { for (int i = 1; i < 101; i++) { IntegerIndividual theIndividual = new IntegerIndividual(); theIndividual.value = i; individuals.Add(theIndividual); } }
public Individual[] Operate(Individual[] parents) { Individual[] offspring = new Individual[parents.Length]; for (int i = 0; i < parents.Length; i++) { IntegerIndividual p1 = (IntegerIndividual)parents[i]; IntegerIndividual o1 = (IntegerIndividual)p1.Clone(); if (Rnd.NextDouble() < MutationProbability) { int iH = -1, iL = -1; double heavyBinSum = 0, lightBinSum = double.PositiveInfinity; for (int j = 0; j < Bins; j++) { double sum = getTotalWeight(o1, j); if (heavyBinSum < sum) { heavyBinSum = sum; iH = j; } else if (lightBinSum > sum) { lightBinSum = sum; iL = j; } } while (heavyBinSum > lightBinSum) { for (int j = 0; j < o1.Genes.Length; j++) { if ((int)o1.Genes[j] == iH) { o1.Genes[j] = iL; heavyBinSum -= Weights[j]; lightBinSum += Weights[j]; break; } } } } offspring[i] = o1; } return(offspring); }
public void Operate(IGeneration source, ArrayList destination) { //Are the individuals in source already ordered by fitness? If so: for (int i = 0; i < 50; i++) { IntegerIndividual theIndividual = (IntegerIndividual)source.Get(i).Individual; IntegerIndividual operatedIndividual1 = new IntegerIndividual(); IntegerIndividual operatedIndividual2 = new IntegerIndividual(); operatedIndividual1.value = theIndividual.value + 1; operatedIndividual2.value = theIndividual.value + 2; destination.Add(operatedIndividual1); destination.Add(operatedIndividual2); } }
double getTotalWeight(IntegerIndividual individual, int j) { double sum = 0; for (int i = 0; i < individual.Genes.Length; i++) { if (individual.Genes[i] == j) { sum += Weights[i]; } } return(sum); }