0

basically here is my code, sorry if its untidy. you will see in the crossover function i want to use the rand. i get errors like "too many arguments for int rand()"

```
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
#pragma hdrstop
//-------------------------------------------------------------------
using namespace std;
void patpixselect(void);
void crossover(void);
void sort_chromosomes( float[16]);
float weights[16][60];
float patpix[9] ; // array for pixels in patterns
float error[16];
const float e = 2.718281828;
int CHR = 16;
int GENE = 60;
float OUTA;
float OUTB;
float OUTC;
float OUTD;
float OUTE;
float OUTX;
float OUTY;
float OUTZ;
float target1;
float target2;
float target3;
float err;
float toterr;
float ERRX;
float ERRY;
float ERRZ;
int pat = 0;
int main ()
{
srand(time(NULL));
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 60; j++)
{
weights[i][j] = -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
}
}
//---- test strings for fitness --------------------------
for(int i = 0; i < 16; i++)
{
toterr = 0;
for (pat = 1; pat < 4; pat++)
{
patpixselect();
cout << endl << "pattern: " << pat << endl;
cout << endl << patpix[0] << endl;
getch();
OUTA = 1/(1 + pow(e, -((weights[i][0] * patpix[0]) + (weights[i][1] * patpix[1])
+ (weights[i][2] * patpix[2] ) + (weights[i][3] * patpix[3]) + (weights[i][4] * patpix[4])
+ ( weights[i][5] * patpix[5]) + (weights[i][6] * patpix[6]) + (weights[i][7] * patpix[7])
+ (weights[i][8] * patpix[8]))));
cout << "OUTA " << OUTA << endl;
OUTB = 1/(1 + pow(e, -((weights[i][9] * patpix[0]) + (weights[i][10] * patpix[1])
+ (weights[i][11] * patpix[2] ) + (weights[i][12] * patpix[3]) + (weights[i][13] * patpix[4])
+ ( weights[i][14] * patpix[5]) + (weights[i][15] * patpix[6]) + (weights[i][16] * patpix[7])
+ (weights[i][17] * patpix[8]))));
cout << "OUTB " << OUTB << endl;
OUTC = 1/(1 + pow(e, -((weights[i][18] * patpix[0]) + (weights[i][19] * patpix[1])
+ (weights[i][20] * patpix[2] ) + (weights[i][21] * patpix[3]) + (weights[i][22] * patpix[4])
+ ( weights[i][23] * patpix[5]) + (weights[i][24] * patpix[6]) + (weights[i][25] * patpix[7])
+ (weights[i][26] * patpix[8]))));
cout << "OUTC " << OUTC << endl;
OUTD = 1/(1 + pow(e, -((weights[i][27] * patpix[0]) + (weights[i][28] * patpix[1])
+ (weights[i][29] * patpix[2] ) + (weights[i][30] * patpix[3]) + (weights[i][31] * patpix[4])
+ ( weights[i][32] * patpix[5]) + (weights[i][33] * patpix[6]) + (weights[i][34] * patpix[7])
+ (weights[i][35] * patpix[8]))));
cout << "OUTD " << OUTD << endl;
OUTE = 1/(1 + pow(e, -((weights[i][36] * patpix[0]) + (weights[i][37] * patpix[1])
+ (weights[i][38] * patpix[2] ) + (weights[i][39] * patpix[3]) + (weights[i][40] * patpix[4])
+ ( weights[i][41] * patpix[5]) + (weights[i][42] * patpix[6]) + (weights[i][43] * patpix[7])
+ (weights[i][44] * patpix[8]))));
cout << "OUTE " << OUTE << endl;
OUTX = 1/(1 + pow(e, -((weights[i][45] * OUTA) + (weights[i][46] * OUTB)
+ (weights[i][47] * OUTC ) + (weights[i][48] * OUTD) + (weights[i][49] * OUTE))));
cout << "OUTX " << OUTX << endl;
OUTY = 1/(1 + pow(e, -((weights[i][50] * OUTA) + (weights[i][51] * OUTB)
+ (weights[i][52] * OUTC ) + (weights[i][53] * OUTD) + (weights[i][54] * OUTE))));
cout << "OUTY " << OUTY << endl;
OUTZ = 1/(1 + pow(e, -((weights[i][55] * OUTA) + (weights[i][56] * OUTB)
+ (weights[i][57] * OUTC ) + (weights[i][58] * OUTD) + (weights[i][59] * OUTE))));
cout << "OUTZ " << OUTZ << endl;
// ------------- calculate errors -(fitness)--------------------------------------------------
cout << endl << endl << "target 1 is " << target1 << endl
<< "target 2 is " << target2 << endl << "target3 is " << target3;
ERRX = (OUTX)*(1 - OUTX)*(target1 - OUTX ); // error of out put x
ERRX = pow(ERRX,2);
ERRX = sqrt(ERRX);
ERRY = (OUTY)*(1 - OUTY)*(target2 - OUTY ); // err of output Y
ERRY = pow(ERRY,2);
ERRY = sqrt(ERRY);
ERRZ = (OUTZ)*(1 - OUTZ)*(target3 - OUTZ ); // err of output z
ERRZ = pow(ERRZ,2);
ERRZ = sqrt(ERRZ);
err = ( ERRX + ERRY + ERRZ ); // total error of 1 pattern for 1 string
cout << endl << "total error for 1 pattern for 1 string " << err << endl;
toterr += err; // --- error for all patterns for 1 string
cout << endl << "total error for all patterns for 1 string " << toterr << endl;
}
error[i] = toterr; // put each error for each string in array to match with each string.
cout << endl << "total error for string " << i << " is " << error[i] << endl ;
}
// check to see if things have
cout << endl << "errors before sorting" ;
for(int j = 0; j < 16; j++)
{
cout << endl << "error in string " << j << " is " << error[j];
}
sort_chromosomes( error );
getch();
cout << endl << "errors after sorting" ;
for(int j = 0; j < 16; j++)
{
cout << endl << "error in string " << j << " is " << error[j];
}
crossover();
getch();
return 0;
}
/******************************************************************************
*
* Name: patpixselect
* Parameters: none
* Returns: none
* Globals: none
* Description: database for which pixels for which pattern and targets for the
pattern
*****************************************************************************/
void patpixselect(void)
{
if (pat == 1)
{
patpix[0] = 1;
patpix[1] = 1;
patpix[2] = 1;
patpix[3] = 1;
patpix[4] = 1;
patpix[5] = 1;
patpix[6] = 1;
patpix[7] = 1;
patpix[8] = 1;
target1 = 1;
target2 = 0;
target3 = 0;
return ;
}
if (pat == 2)
{
patpix[0] = 2;
patpix[1] = 2;
patpix[2] = 2;
patpix[3] = 2;
patpix[4] = 2;
patpix[5] = 2;
patpix[6] = 2;
patpix[7] = 2;
patpix[8] = 2;
target1 = 0;
target2 = 1;
target3 = 0;
return ;
}
if (pat == 3)
{
patpix[0] = 3;
patpix[1] = 3;
patpix[2] = 3;
patpix[3] = 3;
patpix[4] = 3;
patpix[5] = 3;
patpix[6] = 3;
patpix[7] = 3;
patpix[8] = 3;
target1 = 0;
target2 = 0;
target3 = 1;
return ;
}
}
//-----------------------------------------------------------------------------
/******************************************************************************
*
* Name: sort_chromosomes()
* Parameters:
* Returns: none
* Globals: none
* Description: This function sorts all the strings into their order or best to worst
*
********************************************************************************/
void sort_chromosomes( float error[16] )
{
int c,j;
float temp;
float t;
for( c=0; c<CHR-1; c++)
{
for( j=c ; j< CHR-1; j++)
{
if( error[j] < error[j+1])
{
temp = error[j];
error[j]= error[j+1];
error[j+1]= temp;
for( int g=0; g< GENE; g++)
{
t = weights[j][g];
weights[j][g]= weights[j+1][g];
weights[j+1][g]= t;
}
}
}
}
for( c=0; c<CHR-1; c++)
{
for( j=c ; j< CHR-1; j++)
{
if( error[j] < error[j+1])
{
temp = error[j];
error[j]= error[j+1];
error[j+1]= temp;
for( int g=0; g< GENE; g++)
{
t= weights[j][g];
weights[j][g]= weights[j+1][g];
weights[j+1][g]= t;
}
}
}
}
}
/******************************************************************************
*
* Name: crossover()
* Parameters:
* Returns: none
* Globals: none
* Description: This function performs the genetic operator crossover using the first
* (best) half of the chromosomes. Each pair produces two off springs
* and there are four such pairs chosen at random from the first half of
* the generation. The offsprings are then overwritten on the bottom
* half of the chromosomes in initp.dat.
*
********************************************************************************/
void crossover(void)
{
int mate1[4];
int mate2[4];
int cp;
int chrnum;
int c,j;
float temp;
int t;
int g;
for(chrnum=0; chrnum<4; chrnum++)
{
mate1[chrnum] = rand(CHR/2); // gets mate one from random
do
{
mate2[chrnum] = rand(CHR/2); // gets mate 2 from random
}while( mate1[chrnum]== mate2[chrnum]); //as long as they aint the same
cp = rand( GENE); // cp = crossover point chosen at random
for( g=0; g < cp; g++ ) // take weights from 0 to cp from mate 1
{
// tells which string to put it in
weights[(CHR/2)+(chrnum*2)][g] = weights[mate1[chrnum]][g] ;
}
for( g = cp; g < GENE; g++ ) // take w from cp to end from 2 and put it
{
weights[(CHR/2)+(chrnum*2)][g] = weights[mate2[chrnum]][g] ;
}
for( g = 0; g < cp; g++ )
{
// puts it in the string after 1 above
weights[(CHR/2)+(chrnum*2)+1][g] = weights[mate2[chrnum]][g] ; // take first half from 2
}
for( g=cp; g < GENE; g++ )
{
weights[(CHR/2)+(chrnum*2)+1][g] = weights[mate1[chrnum]][g] ; //take 2nd half from 1
}
}
}
```