I'm trying to build a population for my Genetic Algorithm Project using an array. The number of the population is 10 and it consists of 28 binary bit. Here is my code:
#include <iostream>
#include <conio.h>
#include <math.h>
#include <cstdlib>
usingnamespace std;
int main(){
float binary [10][28];
float population_bin [10][29];
float population_dec [10][29];
int row, column;
// Binary convertor//
for (row = 1; row <= 10; row ++){
for (column = 0; column <= 27; column ++){
binary [row][column] = pow (2, column);
}
}
//Binary Population//
for (row = 1; row <= 10; row ++){
for (column = 0; column <= 27; column ++){
population_bin [row][column] = rand ()% 2;
}
}
//Binary Conversion into Decimal//
for (row = 1; row <= 10; row ++){
for (column = 0; column <= 27; column ++){
population_dec [row][column] = binary [row][column] * population_bin [row][column];
}
}
//Population Decoder//
float A, B;
for (row = 1; row <= 10; row ++){
A = 0;
B = 0;
for (column = 0; column <= 27; column ++){
A += population_dec [row][column];
B = 20 + (A*0.0000744313004405472);
}
cout << "Population Number " << row << " is " << B << endl;
}
getch ();
return 0;
}
And the results are as follows:
Population Number 1 is 5746.61
Population Number 2 is 17959.6
Population Number 3 is 4246.73
Population Number 4 is 16603.9
Population Number 5 is 18836.4
Population Number 6 is 18333
Population Number 7 is 18365
Population Number 8 is 14957
Population Number 9 is 3034.82
Population Number 10 is nan