public override Phenotype Develop(Genotype genotype) { BinaryGenotype binaryGenotype = (BinaryGenotype)genotype; ANNWeightPhenotype annWeightPhenotype = new ANNWeightPhenotype(); double max = Math.Pow(2, NumBitsPerWeight); double[] weights = new double[NumWeights]; int weight = 0; int weightIndex = 0; for (int i = 0; i < binaryGenotype.BitVector.Length; i++) { // Convert bits to an int weight += binaryGenotype.BitVector[i] << (i % NumBitsPerWeight); if (i % NumBitsPerWeight == NumBitsPerWeight - 1) { // Calculating a weight between 0 and 1 weights[weightIndex] = (double)(weight - max / 2) / max; weight = 0; weightIndex++; } } annWeightPhenotype.Weights = weights; return(annWeightPhenotype); }
override public Genotype Clone() { BinaryGenotype newGenotype = new BinaryGenotype(NumBits); BitVector.CopyTo(newGenotype.BitVector, 0); return(newGenotype); }
public override void Crossover(Genotype parent1Genotype, Genotype parent2Genotype, Genotype child1Genotype, Genotype child2Genotype) { float selectedValue = (float)random.NextDouble(); if (selectedValue < CrossoverRate) { // Crossover // Choose random crossover point BinaryGenotype binaryParent1 = (BinaryGenotype)parent1Genotype; BinaryGenotype binaryParent2 = (BinaryGenotype)parent2Genotype; BinaryGenotype binaryChild1 = (BinaryGenotype)child1Genotype; BinaryGenotype binaryChild2 = (BinaryGenotype)child2Genotype; int numBits = binaryParent1.NumBits; int crossoverPoint = random.Next(numBits); // Child 1 gets for (int i = 0; i < numBits; i++) { if (i < crossoverPoint) { binaryChild2.BitVector[i] = binaryParent1.BitVector[i]; } else { binaryChild1.BitVector[i] = binaryParent2.BitVector[i]; } } } return; }
public override Phenotype Develop(Genotype genotype) { BinaryGenotype binaryGenotype = (BinaryGenotype)genotype; BinaryPhenotype binaryPhenotype = new BinaryPhenotype(); binaryPhenotype.BitVector = (int[])binaryGenotype.BitVector.Clone(); binaryPhenotype.NumBits = binaryGenotype.NumBits; return(binaryPhenotype); }
public override void Mutate(Genotype genotype) { BinaryGenotype binaryGenotype = (BinaryGenotype)genotype; for (int i = 0; i < binaryGenotype.NumBits; i++) { float selectedValue = (float)random.NextDouble(); if (selectedValue < MutationRate) { binaryGenotype.BitVector[i] ^= 1; } } return; }
public override Phenotype Develop(Genotype genotype) { SymbolPhenotype symbolPhenotype = new SymbolPhenotype(); BinaryGenotype binaryGenotype = (BinaryGenotype)genotype; int symbol = 0; for (int i = 0; i < binaryGenotype.NumBits; i++) { symbol += binaryGenotype.BitVector[i] << (i % SymbolSize); if (i % SymbolSize == SymbolSize - 1) { symbolPhenotype.symbols.Add(symbol % S); symbol = 0; } } return(symbolPhenotype); }