// Mutate an individual private static void mutate(Individual indiv) { // Loop through genes for (int i = 0; i < indiv.size(); i++) { if (Rnd.NextDouble() <= MutationRate) { // Create random gene byte gene = (byte)Math.Round(Rnd.NextDouble()); indiv.setGene(i, gene); } } }
// Crossover individuals private static Individual crossover(Individual indiv1, Individual indiv2) { Individual newSol = new Individual(); // Loop through genes for (int i = 0; i < indiv1.size(); i++) { // Crossover if (Rnd.NextDouble() <= UniformRate) { newSol.setGene(i, indiv1.getGene(i)); } else { newSol.setGene(i, indiv2.getGene(i)); } } return(newSol); }
// Crossover individuals private static Individual crossover(Individual indiv1, Individual indiv2) { Individual newSol = new Individual(); // Loop through genes for (int i = 0; i < indiv1.size(); i++) { // Crossover if (Rnd.NextDouble() <= UniformRate) { newSol.setGene(i, indiv1.getGene(i)); } else { newSol.setGene(i, indiv2.getGene(i)); } } return newSol; }