/* * constant input value version */ #include #include #include #define FROM 0.0 #define TO 100.0 #define STEP 0.01 /* Definition of a neuron * IMPORTANT!!!! Among of the member of the following structure, * Ninp strands for a number of input from neurons. */ struct NEURON { double output, old_output; double thres; int Ninp; double *inp_wgt; struct NEURON **inp_neuron; }; typedef struct NEURON neuron; /* Each neuron can be defined as global variables */ neuron a, b, to_a_neuron, to_b_neuron; void initialize_neuron(neuron *a, int N) { int i; a->thres = 0.0; a->output = 0.0; a->Ninp = N; a->inp_wgt = (double *)malloc(sizeof(double) * (N+1)); for (i=0; i<=N; i++) { a->inp_wgt[i] = 0.0; } a->inp_neuron = (neuron **)malloc(sizeof(neuron *) * (N+1)); } double output_f(double value) { return value; } void calc_neuron(neuron *a) { double wrk; int i; wrk = 0.0; for(i=0; i< a->Ninp; i++){ wrk += a->inp_wgt[i] * a->inp_neuron[i]->old_output; } wrk -= a->thres; a->output += output_f(wrk * STEP); } void update_neuron(neuron *a) { a->old_output = a->output; } int main_loop(void) { double t = FROM; while ( t <= TO ){ printf("%5.2f %7.4f %7.4f\n", t, a.output, b.output); calc_neuron(&a); calc_neuron(&b); update_neuron(&a); update_neuron(&b); t += STEP; } return 0; } int main(int argc, char **argv) { if ( argc != 5 ) { fprintf(stderr,"Usage: %s input_a input_b init_a init_b\n", argv[0]); exit( 0 ); } initialize_neuron(&a, 2); initialize_neuron(&b, 2); initialize_neuron(&to_a_neuron, 0); initialize_neuron(&to_b_neuron, 0); a.inp_neuron[0] = &to_a_neuron; b.inp_neuron[0] = &to_b_neuron; a.inp_neuron[1] = &b; b.inp_neuron[1] = &a; to_a_neuron.output = to_a_neuron.old_output = atof(argv[1]); to_b_neuron.output = to_b_neuron.old_output = atof(argv[2]); a.output= atof(argv[3]); b.output= atof(argv[4]); a.inp_wgt[0] = b.inp_wgt[0] = 1.0; a.inp_wgt[1] = 1.0; b.inp_wgt[1] = -1.0; a.thres = b.thres = 0.0; return main_loop(); }