PROGRAM hopfield DIM T(50,50) CALL memorize(T(,),N) DO CALL recall(T(,),N) LOOP END SUB memorize(T(,),N) DIM V(50) RANDOMIZE INPUT prompt "number of stored memories = ": M ! N corresponds to number of neurons INPUT prompt "size of memories = ": N PRINT "enter M strings of N 0's and 1's" FOR memory = 1 to M LINE INPUT s$ CALL convert(s$,N,V()) FOR i = 1 to N FOR j = 1 to N IF i <> j then LET T(i,j) = T(i,j) + (2*V(i) - 1)*(2*V(j) - 1) END IF NEXT j NEXT i NEXT memory PRINT END SUB SUB recall(T(,),N) DIM V(50) PRINT "enter a string of N 0's and 1's" LINE INPUT s$ CALL convert(s$,N,V()) DO FOR k = 1 to N LET i = int(N*rnd) + 1 ! choose neuron at random LET sum = 0 FOR j = 1 to N IF i <> j then LET sum = sum + T(i,j)*V(j) NEXT j IF sum > 0 then LET V(i) = 1 ! above threshold ELSE LET V(i) = 0 ! below threshold END IF NEXT k FOR i = 1 to N PRINT using "#": V(i); NEXT i PRINT PAUSE 1 LOOP until key input GET KEY kk END SUB SUB convert(s$,N,V()) ! convert string to array of 0's and 1's FOR i = 1 to N LET c$ = s$[i:i] IF c$ = "0" then LET V(i) = 0 ELSE LET V(i) = 1 END IF NEXT i END SUB