/* * Necker Cube alternation simulation, syncronous algorithm * Author: ASAKAWA Shinichi * $Id: Necker-alt.c,v 1.1 2002/04/29 07:56:16 asakawa Exp $ * * Configuration of neurons: * 0 ------- 1 8 ------- 9 / /| /| | 2---------3 | 10 | 11 | | | | | | | | 4 | 5 |12---------13 | |/ |/ / 6---------7 14---------15 */ #include #include #include #include #define N_neuron 16 int FROM=0; int TO=10000; int EPOCH=1; double sigmoid_slope = -2.0; double Inhibition = -2.0; double Excitation = 1.0; double Noise_range = 10.0; /* * Definition of a neuron * Ninp strands for a number of input from other neurons. */ struct NEURON { double output; double state; int fire_cnt; double thres; int Ninp; double *inp_wgt; double (*f)(); struct NEURON **inp_neuron; }; typedef struct NEURON neuron; neuron Necker[N_neuron]; double linear(double value) { return value; } double sigmoid(double value) { return 1.0 / ( 1.0 + exp(sigmoid_slope * value) ); } double formal(double value) { if ( value > 0.0 ){ return 1.0; } else { return 0.0; } } /* return random number from 0 to 1 */ double drand(void) { return ((double)(rand() % RAND_MAX) / (double)RAND_MAX); } /* return random number from -1 to 1 */ double drand2(void) { return (drand() * 2.0 - 1.0); } void initialize_neuron(neuron *a, int N) { int i; a->thres = 0.0; a->state = drand(); a->fire_cnt = 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)); a->f = sigmoid; a->output = a->f(a->state); } void calc_neuron(neuron *a) { double wrk = 0.0; int i; for(i=0; i< a->Ninp; i++){ wrk += a->inp_wgt[i] * a->inp_neuron[i]->output; } wrk -= a->thres; a->state = wrk; a->output = a->f(a->state); } void Print_net(int iter) { printf("### Iteration : %d\n", iter); printf(" %3.1f ------ %3.1f %3.1f ----- %3.1f\n", Necker[0].output,Necker[1].output,Necker[8].output,Necker[9].output); printf(" / /| /| |\n"); printf(" %3.1f ------ %3.1f| %3.1f | %3.1f |\n", Necker[2].output,Necker[3].output,Necker[10].output,Necker[11].output); printf(" | | | | | |\n"); printf(" | %3.1f | %3.1f |%3.1f ----- %3.1f\n", Necker[4].output,Necker[5].output,Necker[12].output,Necker[13].output); printf(" | |/ |/ / \n"); printf(" %3.1f ------ %3.1f %3.1f ------ %3.1f \n\n", Necker[6].output,Necker[7].output,Necker[14].output,Necker[15].output); } int main_loop(void) { int i, iter = 0; while ( iter <= TO ){ for (i=0;i 0.8) { Necker[i].fire_cnt++; } if (Necker[i].fire_cnt > 10) { Necker[i].state = -30.0; Necker[i].output = 0.0; Necker[i].fire_cnt = -10; } if (Necker[i].fire_cnt < 0 ) { Necker[i].state = -30.0; Necker[i].output = 0.0; Necker[i].fire_cnt++; } else { Necker[i].state += Noise_range * drand(); } } for (i=0;i