📄 遗传算法c源码.txt
字号:
{
int p, i, j, k1, k2, m;
float dev;
for ( p = 0 ; p < POPS ; p++ )
{
/* keep best - BESTPOP */
/* add another group, randomly - (SELPOP-BESTPOP) */
for ( i = BESTPOP ; i < SELPOP ; i++ )
pop_swap( p, i, (irand( MAXPOP - i ) + i) );
/* create new individuals */
for ( i = SELPOP ; i < NEWPOP ; i++ )
for ( j = 0 ; j < SIZE ; j++ )
pop[p][j] = ((random()&1048575) / 100000.0 - 5) * 2;
/* SELPOP to MUT1 will be severe mutations */
for ( i = NEWPOP ; i < MUT1 ; i++ )
{
pop_copy( p, i, p, irand(NEWPOP) );
dev = 1 + ((irand(2000) - 1000 )/ 5000);
pop[p][irand(SIZE)] *= dev;
dev = 1 + ((irand(2000) - 1000 )/ 5000);
pop[p][irand(SIZE)] *= dev;
}
/* MUT2 to MAXPOP will be crossovers */
for ( i = MUT1 ; i < MAXPOP ; i++ )
{
/* Every several generations (set by MIXGEN) there is a cross-over
between different populations. */
pop_copy( p, i, (((generation%MIXGEN)==0) ? irand(POPS) : p), iran
d(NEWPOP) );
j = irand(NEWPOP);
k1 = irand( SIZE - 1);
k2 = irand( SIZE - 1 - k1 ) + k1 + 1;
for ( m = k1 ; m <= k2 ; m++ ) pop[p][m] = pop[p][j][m];
/* Mutate slightly */
dev = 1 + ((irand(2000) - 1000 )/ 50000);
pop[p][irand(SIZE)] *= dev;
}
}
calc_score();
sort_population();
statistics( generation );
}
/*-----------------------------------------------------------------------*\
| |
| Return the number of cases for which the NN returns the correct value |
| |
\*-----------------------------------------------------------------------*/
check_performance()
{
vector x;
int j, count=0;
for ( j = 0 ; j < total ; j++ )
{
x = test[j];
if ( net(x) == hits[j] )
count++;
}
return count;
}
/*-----------------------------------------------------------------------*\
| |
| Get data (read input file) |
| |
\*-----------------------------------------------------------------------*/
int get_data()
{
char* FileName = "/tmp/nn-input";
FILE *fd;
int i, posnum, negnum;
float x,y;
/* opens the file */
if ( (fd = fopen(FileName,"r")) == NULL )
{
printf ("no-input-file");
exit(10);
}
/* Total number of input values */
total = 0;
/* read the positive examples */
fscanf( fd, "%d", &posnum);
if (posnum > LIMIT)
{
printf("Error");
exit(20);
}
for ( i = 0 ; i < posnum ; i++ )
{
fscanf( fd, "%f %f", &x, &y);
test[ total ].p[0] = x / 1000;
test[ total ].p[1] = y / 1000;
hits[ total++ ] = 1; /* 1 for positive examples */
}
/* read the negative examples */
fscanf( fd, "%d", &negnum);
if ((negnum+total) > LIMIT)
{
printf("Error");
exit(21);
}
for ( i = 0 ; i < negnum ; i++ )
{
fscanf( fd, "%f %f", &x, &y);
test[ total ].p[0] = x / 1000;
test[ total ].p[1] = y / 1000;
hits[ total++ ] = 0; /* 0 for negative example */
}
fclose( fd );
return (0) ;
}
/*-----------------------------------------------------------------------*\
| |
| best_pop - Find the population with the best solution |
| |
\*-----------------------------------------------------------------------*/
int best_pop()
{
int i, p, best = 0;
for ( i = 0 ; i < POPS ; i++ )
if ( score[0] > best )
{
best = score[0];
p = i;
}
return(p);
}
/*-----------------------------------------------------------------------*\
| |
| charmap - draw a charmap showing the NN's behaviour |
| |
\*-----------------------------------------------------------------------*/
charmap( p )
int p;
{
int i, j, result;
vector x;
apply( p ,0 );
for ( i = 0 ; i < 350 ; i++ )
{
for ( j = 0 ; j < 350 ; j++ )
if ( (i%12==0) && (j%6==0) )
{
x.p[0] = j/1000.0;
x.p[1] = i/1000.0;
result = net( x );
printf("%c", (result==1 ? '+' : '.' ) );
}
if ( i%12==0 ) printf("\n");
}
}
/*-----------------------------------------------------------------------*\
| |
| make_output - create the output file |
| |
\*-----------------------------------------------------------------------*/
make_output(p)
int p;
{
int i, j, result, oldresult, start;
vector x;
char* FileName = "/tmp/nn-output";
FILE *fd;
printf("\n%s\n", (score[p][0]!=total ? "Failed." : "Success" ) );
apply( p, 0 );
printf("Writing output file...\n");
/* Open the file */
if ( (fd = fopen(FileName,"w")) == NULL )
{
printf ("Can't open output file");
exit(10);
}
/* line scheme */
for ( i = 0 ; i < 350 ; i++ ) /* Scan horizontally */
{
result = 0;
for ( j = 0 ; j < 350 ; j++ )
{
oldresult = result;
x.p[0] = j/1000.0;
x.p[1] = i/1000.0;
result = net( x );
if ( oldresult != result )
fprintf( fd, "%d %d ", j, i );
}
}
for ( j = 0 ; j < 350 ; j++ ) /* Scan vertically */
{
result = 0;
for ( i = 0 ; i < 350 ; i++ )
{
oldresult = result;
x.p[0] = j/1000.0;
x.p[1] = i/1000.0;
result = net( x );
if ( oldresult != result )
fprintf( fd, "%d %d ", j, i );
}
}
fclose( fd );
printf("Done!\n");
}
/*-----------------------------------------------------------------------*\
| |
| Main |
| |
\*-----------------------------------------------------------------------*/
main()
{
int generation, j, p, best, done = 0;
float px, py, px1, py1;
randomize();
get_data(); /* Read input from file */
make_initial_population();
calc_score();
sort_population();
/* Educate the net */
generation = 0;
while ( (done != 1 ) && ( generation++ < SESSIONS ) )
{
make_next_generation( generation );
p = best_pop();
/* Show a charmap every 50 generations */
if ( generation % 50 == 0 ) charmap(p);
if ( score[p][0] == total )
done = 1;
}
/* return results */
make_output(p);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -