Genetic programming

Can anyone help to make this code run ,it is working upto create_binary and fitness. i need to link the chrom::selection with other ie i need to select a binary string based on the fitness calculated in chrom::fitness. psedo code for selection was written here,the output required is a new binary string with more fitness.



class chrom
{
private:
unsigned first;
unsigned second;
unsigned third;

public:
chrom(unsigned f=0,unsigned s=0,unsigned t=0);
chrom(void);
~chrom(void);
void create_binary();
void fitness();
void selection();

};

#include <iostream>
#include<conio.h>
#include<vector>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>


#define GEN 5
using namespace std;

chrom::chrom(void)
{
}

chrom::chrom(unsigned f,unsigned s,unsigned t)
{
first=f;
second=s;
third=t;
}
chrom::~chrom(void)
{
}



void chrom::create_binary()
{
long binary_number ;
vector <int> binary;

while ( first >= 1 )
{
binary_number =first % 2;
first /= 2;
binary.push_back(binary_number);

}

for ( int i = (int) binary.size()-1; i >= 0; i-- )
//cout << binary[i] << "";


while ( second >= 1 )
{
binary_number =second % 2;
second /= 2;
binary.push_back(binary_number);

}


for ( int i = (int) binary.size()-1; i >= 0; i-- )
//cout << binary[i] << "";

while ( third >= 1 )
{
binary_number =third % 2;
third /= 2;
binary.push_back(binary_number);

}

cout << endl << endl << endl;
cout << "Chromesomes are ";

for ( int i = (int) binary.size()-1; i >= 0; i-- )
cout << binary[i] << "";

}

void chrom::fitness()
{
int i;
int binary[i];
int x;
int y;
int z;
int fitness;

x=binary[13]*8+binary[12]*4+binary[11]*2+binary[10]*1;

y=binary[9]*64+binary[8]*32+binary[7]*16+binary[6]*8+binary[5]*4+binary[4]*2+binary[3]*1;

z=binary[2]*4+binary[1]*2+binary[0]*1;


fitness=(x*x)+(y*z)-(y*y);
cout << "Fitness of Chromesomes are ";
cout<<fitness<<"\n";

}



void chrom::selection()

{

int team_A,team_B,team_C,team_D;
int winner_e,winner_f;
int fitness;
unsigned int iseed;
iseed=time(NULL);
srand(iseed);

team_A=(int)(100.0*rand()/RAND_MAX+1);
team_B=(int)(100.0*rand()/RAND_MAX+1);
cout<<"Team A is = "<<team_A<< "\n";
cout<<"Team B is = "<<team_B<< "\n";

if(team_A>team_B)
winner_e=team_A;
else
winner_e=team_B;

cout<<"The winner of first group: "<<winner_e<<"\n";


team_C=(int)(100.0*rand()/RAND_MAX+1);
team_D=(int)(100.0*rand()/RAND_MAX+1);
cout<<"Team C is = "<<team_C<< "\n";
cout<<"Team D is = "<<team_D<< "\n";

if(team_C>team_D)
winner_f=team_C;
else
winner_f=team_D;

cout<<"The winner of first group: "<<winner_f<<"\n";


}

const int POP=4;
int main(void)
{
//int generation;
//for(generation=0;generation<GEN;generation++)
chrom number[POP]=
{
chrom(1,1,1),
chrom(3,6,8),
chrom(4,5,6),
chrom(1,2,3)
};

for(int i=0;i<POP;i++)
number[i].create_binary();
cout<<"\n";
for(int i=0;i<POP;i++)
number[i].fitness();


}
Topic archived. No new replies allowed.