📄 『 高性能计算 』 - 神经网络遗传算法---绝对好东西.htm
字号:
MUT1-NEWPOP = Number of mutations in the first muta <BR>tion
group */ <BR>#define MIXGEN 10 /* Number of
generations between population mixing */ <BR>typedef struct
<BR>{ <BR> float p[NUM]; <BR>} vector; <BR>/* NN related
*/ <BR>vector test[LIMIT], w1, w2, w3, w4, w5, w6; <BR>int
hits[LIMIT], total; <BR>float w7[6]; <BR>int b1, b2, b3, b4,
b5, b6, b7; <BR>/* GA related */ <BR>float
pop[POPS][MAXPOP][SIZE]; <BR>int score[POPS][MAXPOP];
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| Randomize
| <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>randomize() <BR>{ <BR> struct timeval tp;
<BR> struct timezone tzp; <BR> /* Use time of day to
feed the random number generator seed */
<BR> gettimeofday( &tp, &tzp); <BR> srandom(
tp.tv_sec ); <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| irand( range ) - return a random
integer in the range 0..(range-1) | <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>int irand( range ) <BR> int range; <BR>{
<BR> return( random() % range ); <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| scalar_mult - multiply two vectors
|
<BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>float scalar_mult( x, y ) vector x, y; <BR>{ <BR> int
i; <BR> float s = 0.0; <BR> for ( i = 0 ; i < NUM
; i++ ) s += ( x.p * y.p ); <BR> return s; <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| This function computes the NN's output
for a certain input vector. | <BR>| The NN
is constructed from 2 layers, first layer has 6 neurons,
| <BR>| second layer has 1
neuron.
| <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>int net( x ) vector x; <BR>{ <BR> /* First layer */
<BR> float a1 = atanpi( scalar_mult( w1, x ) + b1 ) /
1.6; /* atan transfer fu <BR>nction */ <BR> float
a2 = atanpi( scalar_mult( w2, x ) + b2 ) / 1.6; /* atan
transfer fu <BR>nction */ <BR> int a3 = (
scalar_mult( w3, x ) + b3 ) > 0;
/* hardlim transfer <BR>function */ <BR> int
a4 = ( scalar_mult( w4, x ) + b4 ) > 0;
/* hardlim transfer <BR>function */
<BR> float a5 = scalar_mult( w5, x ) + b5 ;
/* linear transfer
<BR>function */ <BR> float a6 = scalar_mult( w6, x ) + b6
; /*
linear transfer <BR>function */ <BR> /* Second layer */
<BR> float a7 = ( a1*w7[0] + a2*w7[1] + a3*w7[2] +
a4*w7[3] + <BR>
a5*w7[4] + a6*w7[5] + b7 ) > 0.0;
/* hardlim transfer <BR>function */
<BR> return(a7); <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| pop_swap( p, a, b ) - swap
two vectors and scores in the population p| <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>pop_swap( p, a, b ) <BR> int p, a, b; <BR>{
<BR> int t, i; <BR> /* Swap vector */ <BR> for
( i = 0 ; i < SIZE ; i++ ) <BR> { <BR>
t = pop[p][a]; <BR> pop[p][a]
= pop[p]; <BR> pop[p] = t; <BR>
} <BR> /* Swap score */ <BR> t = score[p][a];
<BR> score[p][a] = score[p]; <BR> score[p] = t;
<BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| apply( p, i ) - apply the i vector
of the population p on the NN | <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>apply( p, i ) <BR> int p, i; <BR>{
<BR> /* Get the weights and biases of the neurons from
the GA vector */ <BR> w1.p[0] = pop[p][0]; w1.p[1]
= pop[p][1]; b1 = pop[p][2]; <BR> w2.p[0] =
pop[p][3]; w2.p[1] = pop[p][4]; b2 = pop[p][5];
<BR> w3.p[0] = pop[p][6]; w3.p[1] = pop[p][7];
b3 = pop[p][8]; <BR> w4.p[0] = pop[p][9];
w4.p[1] = pop[p][10]; b4 = pop[p][11];
<BR> w5.p[0] = pop[p][12]; w5.p[1] = pop[p][13];
b5 = pop[p][14]; <BR> w6.p[0] = pop[p][15];
w6.p[1] = pop[p][16]; b6 = pop[p][17];
<BR> w7[0] = pop[p][18]; <BR> w7[1] = pop[p][19];
<BR> w7[2] = pop[p][20]; <BR> w7[3] = pop[p][21];
<BR> w7[4] = pop[p][22]; <BR> w7[5] = pop[p][23];
<BR> b7 = pop[p][24]; <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| pop_copy( p1, a, p2, b ) - copy the
vector b in the population p2 into | <BR>|
the vector a in the population p1.
| <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>pop_copy( p1, a, p2, b) <BR> int p1, a, p2,
b; <BR>{ <BR> int i; <BR> for ( i = 0 ; i < SIZE
; i++ ) <BR> pop[p1][a] = pop[p2]; <BR>}
<BR>/*-----------------------------------------------------------------------*\
<BR>|
| <BR>| Initialize the populations
| <BR>|
|
<BR>\*-----------------------------------------------------------------------*/
<BR>make_initial_population() <BR>{ <BR> int p, i, j;
<BR> for ( p = 0 ; p < POPS ; p++ ) <BR> {
<BR> /* Half population gets values from -1
to 1 */ <BR> for ( i = 0 ; i <
(MAXPOP/2) ; i++ ) <BR> for ( j = 0
; j < SIZE ; j++ ) <BR>
pop[p][j] = ((random()&1048575) / 1000000.0 - 0.5) *
2; <BR> /* Half population gets values from
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -