📄 『 高性能计算 』 - 神经网络遗传算法---绝对好东西.htm
字号:
-100 to 100 */ <BR> for ( i = (MAXPOP/2) ;
i < MAXPOP ; i++ ) <BR> for ( j =
0 ; j < SIZE ; j++ ) <BR>
pop[p][j] = ((random()&1048575) / 10000.0 - 50) * 2;
<BR> } <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| Calculate the scores of all the vectors
in all the populations | <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>calc_score() <BR>{ <BR> int p, i; <BR> for ( p =
0 ; p < POPS ; p++ ) <BR> for ( i = 0 ; i <
MAXPOP ; i++ ) <BR> { <BR>
apply( p, i ); <BR>
score[p] = check_performance(); <BR>
} <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| Sort the populations
| <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>sort_population() <BR>{ <BR> int p, i, j, k, best;
<BR> /* Use insert sort */ <BR> for ( p = 0 ; p <
POPS ; p++ ) <BR> for ( i = 0 ; i < (MAXPOP-1)
; i++ ) <BR> { <BR>
best = score[p]; <BR> for ( j
= (i+1) ; j < MAXPOP ; j++ ) <BR>
if ( score[p][j] > best ) <BR>
{ <BR>
best = score[p][j]; <BR>
k = j; <BR>
} <BR> if
( best > score[p] ) <BR>
pop_swap( p, i, k ); <BR> } <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| Show (on the standard output) the best
scores of all populations | <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>statistics( generation ) <BR> int generation;
<BR>{ <BR> int p; <BR> if ( generation % MIXGEN == 0
) <BR> printf("-----------------------------\n");
<BR> printf(" %4d) First are: ", generation);
<BR> for ( p = 0 ; p < POPS ; p++ ) printf("%3d
", score[p][0] ); <BR> printf(" (from %d)\n",total);
<BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| Generate the next generation in all
populations
| <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>make_next_generation( generation ) <BR> int
generation; <BR>{ <BR> int p, i, j, k1, k2, m;
<BR> float dev; <BR> for ( p = 0 ; p < POPS ; p++
) <BR> { <BR> /* keep best -
BESTPOP */ <BR> /* add another group,
randomly - (SELPOP-BESTPOP) */ <BR>
for ( i = BESTPOP ; i < SELPOP ; i++ ) <BR>
pop_swap( p, i, (irand( MAXPOP - i ) + i)
); <BR> /* create new individuals */
<BR> for ( i = SELPOP ; i < NEWPOP ; i++
) <BR> for ( j = 0 ; j < SIZE ;
j++ ) <BR> pop[p][j] =
((random()&1048575) / 100000.0 - 5) * 2; <BR>
/* SELPOP to MUT1 will be severe mutations */
<BR> for ( i = NEWPOP ; i < MUT1 ; i++ )
<BR> { <BR>
pop_copy( p, i, p, irand(NEWPOP) ); <BR>
dev = 1 + ((irand(2000) - 1000 )/
5000); <BR>
pop[p][irand(SIZE)] *= dev; <BR>
dev = 1 + ((irand(2000) - 1000 )/ 5000);
<BR> pop[p][irand(SIZE)] *=
dev; <BR> } <BR>
/* MUT2 to MAXPOP will be crossovers */ <BR>
for ( i = MUT1 ; i < MAXPOP ; i++ ) <BR>
{ <BR> /*
Every several generations (set by MIXGEN) there is a
cross-over <BR><BR>
between different populations. */ <BR>
pop_copy( p, i, (((generation%MIXGEN)==0) ?
irand(POPS) : p), iran <BR>d(NEWPOP) ); <BR>
j = irand(NEWPOP); <BR>
k1 = irand( SIZE - 1); <BR>
k2 = irand( SIZE - 1 - k1 ) + k1 + 1;
<BR> for ( m = k1 ; m <=
k2 ; m++ ) pop[p][m] = pop[p][j][m]; <BR>
/* Mutate slightly */ <BR>
dev = 1 + ((irand(2000) - 1000 )/ 50000);
<BR> pop[p][irand(SIZE)] *=
dev; <BR> } <BR> }
<BR> calc_score(); <BR> sort_population();
<BR> statistics( generation ); <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| Return the number of cases for which the
NN returns the correct value | <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>check_performance() <BR>{ <BR> vector x;
<BR> int j, count=0; <BR> for ( j = 0 ; j < total
; j++ ) <BR> { <BR> x =
test[j]; <BR> if ( net(x) == hits[j] )
<BR> count++; <BR> }
<BR> return count; <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| Get data (read input file)
| <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>int get_data() <BR>{ <BR> char* FileName =
"/tmp/nn-input"; <BR> FILE *fd; <BR> int i, posnum,
negnum; <BR> float x,y; <BR> /* opens the file
*/ <BR> if ( (fd = fopen(FileName,"r")) == NULL )
<BR> { <BR> printf
("no-input-file"); <BR> exit(10);
<BR> } <BR> /* Total number of input values
*/ <BR> total = 0; <BR> /* read the positive
examples */ <BR> fscanf( fd, "%d", &posnum);
<BR> if (posnum > LIMIT) <BR> { <BR>
printf("Error"); <BR>
exit(20); <BR> } <BR> for ( i = 0 ; i
< posnum ; i++ ) <BR> { <BR>
fscanf( fd, "%f %f", &x, &y); <BR>
test[ total ].p[0] = x / 1000; <BR>
test[ total ].p[1] = y / 1000; <BR>
hits[ total++ ] = 1; /* 1 for positive examples */
<BR> } <BR> /* read the negative examples */
<BR> fscanf( fd, "%d", &negnum); <BR> if
((negnum+total) > LIMIT) <BR> { <BR>
printf("Error"); <BR>
exit(21); <BR> } <BR> for ( i = 0 ; i
< negnum ; i++ ) <BR> { <BR>
fscanf( fd, "%f %f", &x, &y); <BR>
test[ total ].p[0] = x / 1000; <BR>
test[ total ].p[1] = y / 1000; <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -