/* maxnet */ #include (stdio.h) #include (stdlib.h) float ft(float x) { #define s 1.05 float y; if (x < 0) return 0 ; else { y = s * x ; if (y < 1) return y ; else return 1.0 ; } } #define nunits 10 #define epsilon 0.001 float units[nunits], psp[nunits], e, resid ; int i, j ; void main(void) { randomize() ; /* initialize first unit to maximum */ units[0] = 0.95 ; for (i=1; i < nunits; i++) units[i] = 0.9 * random(RAND_MAX)/RAND_MAX; for (i=0; i < nunits; i++) printf ( "%5.3f ", units[i] ) ; printf ("\n" ) ; e = 1 / (nunits+0.0) ; /* main loop */ do { for (i=0; i < nunits; i++) { psp[i] = 0 ; for (j=0;j < nunits;j++) if (j==i) psp[i] += units[j] ; else psp[i] -= e * units[j] ; } for (i=0; i < nunits; i++) units[i] = ft(psp[i]) ; for (i=0; i < nunits; i++) printf ( "%5.3f ", units[i] ) ; printf ("\n" ) ; for (resid=0.0,i=1; i < nunits; i++) resid += units[i] ; } while (resid>epsilon) ; }
Back to lecture