Here we go practically all finished, Marvel in the shoddily coded and bodged genetic algorithm "ish" .
Code:
#include <iostream>
#include <stdio.h>
#include <string>
#define RAND_MAX 100
#define srand 12
#include <ctime>
#define PopSize 10
using namespace std;
using std::string;
using std::cout;
using std::endl;
using std::cin;
void GetUserInput(string &Input)
{
cout << "Please enter Amino Acid Sequence \"H,P\": " << endl;
cin >> Input;
cin.get();
}
void GetSetupString(string theInput, int (Output)[10])
// To declare an Array as a reference from Main, syntax is differant then other
// references, ie: Type (&ArryName)[Size], try finding that in any tutorial :)
{
int index;
for (index = 0; index < theInput.length(); ++index)
{
switch (theInput[index])
{
case 'H':
Output[index] = 1;
break;
case 'P':
Output[index] = 0;
break;
default:
break;
}
cout << Output[index] ;
}
cout << endl;
cin.get();
}
void GetPopulation(int (Output)[10], int (Pop)[PopSize][12])
{
int index;
int j;
for ( index = 0; index < PopSize; index++)
{
for ( j = 0; j < 10; j++)
{
Pop[index][j] = Output[j];
cout << Pop[index][j];
}
cin.get();
cout << endl;
}
}
void GetFitness(int (Pop)[PopSize][12])
{
// Fitness is kept in the 11th element which is only touched upon in this
// function and left alone in all other functions
int index;
int j;
int fitness = 0;
int Par = 0;
for (index = 0; index < PopSize; index++)
{
Pop[index][11] = 0;
fitness = 0;
for (j = 0; j < 10; j++)
{
if (Pop[index][j] == 1)
{
++fitness;
}
}
Pop[index][11] = fitness;
++Par;
cout << "Fitness of Parent:'"<< Par <<"'" << ".."<< Pop[index][11]<< endl;
}
}
void GetSortFitness(int (Pop)[PopSize][12])
{
// Using a bastardization of Bubble sort, the two for loopa make sure that
// the highest numbers reach the end of the array
int i;
int PopSort[12];
int index;
int j;
for (i = 0; i < PopSize; i++)
{
for (index = 0; index < PopSize-1; index++)
{
if (Pop[index][11] > Pop[index+1][11])
{
for ( j = 0; j < 12; j++)
{
PopSort[j] = Pop[index+1][j];
Pop[index+1][j] = Pop[index][j];
Pop[index][j] = PopSort[j];
}
cout << " Loop Initiated" << endl;
}
cout << "Loop Not initiated" /*<< " " << Pop[index][11]*/ <<endl;
}
//cout << "Sort Loop" << " " << Pop[index][11] <<endl;
}
}
int GetMutation (int (Pop)[PopSize][12])
{
//Don't forget to seed rand() each time you run the program,
int index;
int j;
int Num = 0;
for ( index = 0; index < PopSize; index++)
{
for ( j = 0; j < 10; j++)
{
if (rand()%100 > 90)
{
if ( rand()%100 < 50)
{
Pop[index][j] = 0;
}
else
{
Pop[index][j] = 1;
}
Num++;
}
else
{
//do nothing
}
cout << Pop[index][j] ;
}
cout << endl;//cin.get();
}
cin.get();
}
void GetCrossover(int (Pop)[PopSize][12])
{
int NewPop[PopSize][10];
int index;
int j;
int Inc;
int Off = 0;
for (index = 0; index < PopSize; index++)
{
for (j = 0; j < 10; j++)
{
NewPop[index][j] = Pop[index][j];
}
}
for (index = 0; index < 9; index++)
{
Inc = index;
char Check = 'X'; // Reinitialise Check to 'Y' for each loop
for (j = 0; j < 10; j++)
{
if ( Check = 'X')
{
// If Not 'X' the perform this block
if (rand()%100 > 70)
{
Inc = index;
Check = 'Y';
Pop[index][j] = NewPop[++Inc][j];
}
else
{
Pop[index][j] = NewPop[index][j];
}
}
else
// If Check = 'X' the perform this block
{
Pop[index][j] = NewPop[Inc][j];
}
cout << Pop[index][j];
}
cout << endl;
cout << "Offspring:" << ++Off << endl;
cin.get();
}
for (index = 10; index <11; index++)
{
Inc = 0;
for ( j = 0; j < 10; j++)
{
Pop[index][j] = NewPop[Inc][j];
cout << Pop[index][j];
}
cout << endl;
cout << "Best Parent Kept"<< endl;
cin.get();
}
}
int main ()
{
string Input;
int Output[10];
int Pop[PopSize][12];
//int size = Input.size();
int Next;
GetUserInput(Input);
GetSetupString(Input, Output);
GetPopulation(Output, Pop);
do
{
int Next = 0;
// GetPopulation(Output, Pop);
GetFitness(Pop);
GetSortFitness(Pop);
GetFitness(Pop);
GetMutation(Pop);
GetCrossover(Pop);
++Next;
}
while (Next !=10);
}
And yes there are pointless pieces of code in it and no i'am not changing it, i'am going to re write a new one probably from scratch, and probably using "strings" Anon .
Well hope yoiu enjoyed the journey as much as i did. Appreciated all the help over the number of threads created.
Regards Wolfe