# In Kohonen Feature Map, neurons are organizing themselves according to certain input values. 
# (c) Chekanov

from jhplot  import  *
from jhpro.nnet import *
from java.util import Random

kfm=KohonenFeatureMap()

inputSize=10   # number of random points in 3D 
mapSizeX=9   # map size is 9x9
mapSizeY=9
maxCycle=1000000
im = InputMatrix(inputSize, 3);
kfm.setMaxLearningCycles(maxCycle);
kfm.createMapLayer(mapSizeX, mapSizeY) # create a map in Y-Y
kfm.setStopArea(0.02)                  # stop learning here
kfm.setInitActivationArea(100)
kfm.setInitLearningRate(0.6)

from jarray import array
inputX=[]
inputY=[]
inputZ=[]
rand = Random()
for i in range(inputSize):
     inputX.append(int(rand.nextGaussian()*100))
     inputY.append(int(rand.nextGaussian()*100))
     inputZ.append(int(rand.nextGaussian()*100)) 

px=   array(inputX, 'i')  
py=   array(inputY, 'i') 
pz=   array(inputZ, 'i')
im.setInputValues(px,py,pz);
kfm.connectLayers(im);
  
i=0
while kfm.finishedLearning() == False:
    kfm.learn()
    if (i%50 ==0): print "print  rate=",kfm.getLearningRate(), "Activation area=",kfm.getActivationArea(), "Elapsed time=",kfm.getElapsedTime()
    i=i+1
