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); }
public override float Evaluate(Individual individual) { int foodScore = 0; int poisonScore = 0; foreach (Board b in boards) { b.ResetBoard(); ANNWeightPhenotype phenotype = (ANNWeightPhenotype)individual.Phenotype; ann.SetWeights(phenotype.Weights); for (int i = 0; i < timeSteps; i++) { b.player.Move(b.player.GetMove()); } foodScore += b.player.foodScore; poisonScore += b.player.poisonScore; } return((float)(foodScore * foodWeight + poisonScore * poisonWeight)); }