//kohon.java // applet framework // for demonstrating a square kohonen Neural network // (c) 1998 Robert W. Harrison import java.lang.*; import java.applet.*; import java.awt.*; import java.util.*; public class k2 extends Applet implements Runnable{ Thread lz = null; public boolean handleEvent(Event e) { if( e.id == Event.WINDOW_DESTROY)// this is needed for graceful close { System.exit(0); } return super.handleEvent(e); } public void start() { if(lz == null ) lz = new Thread(this); lz.start(); } public void stop() { lz.stop(); lz = null; // force gc } public static int NPoints; public void init() { String temp; Integer itemp; temp = getParameter("npoints"); if( temp == null){ NPoints = 25;} else{ itemp = new Integer(temp); NPoints = itemp.intValue();} // if( NPoints > 100) NPoints = 100; } public void run() { Graphics g; rrandom rnx = new rrandom((long)10134); rrandom rny = new rrandom((long)31459265); int i,j,imin,jmax,ii,imax; float x[] = new float[NPoints],y[] = new float[NPoints],r,xr,yr; float xo[] = new float[NPoints],yo[] = new float[NPoints]; float dx,dy; float rmin; float damp,kern; Color red,green; g= this.getGraphics(); red = new Color(255,0,0); green = new Color(0,255,0); jmax = NPoints; if( jmax > 100) jmax = 100; imax = NPoints/4; damp = (float)1.0; for( i=0; i< NPoints; i++) { x[i] = rnx.nextFloat();y[i] = rny.nextFloat();} for(;;)// forever { for(j=0; j< NPoints; j++) { xo[j] = x[j]; yo[j] = y[j];} damp = damp + (float)0.1; // update the points for( j=0; j< jmax; j++){ xr = rnx.nextFloat(); yr = rny.nextFloat(); rmin =(xr-x[0])*(xr-x[0]) + (yr-y[0])*(yr-y[0]); imin = 0; for( i=1; i< NPoints; i++) { r = (xr-x[i])*(xr-x[i]) + (yr-y[i])*(yr-y[i]); if( r < rmin) { rmin = r; imin = i;} } // now move the closest and his neighbors if( damp*4 > imax) damp = imax/4; for( i=0; i< imax; i++) { // kern = (float)0.5/(damp*i + (float)1.); kern = (float)0.5*(imax-i*damp)/(imax); // if( kern < (float)0.0) kern = (float)0.0; if( kern < (float)0.0) break; ii = imin -i; if( ii > -1 ){ dx = xr-x[ii]; dy = yr-y[ii]; x[ii] += kern*dx; y[ii] += kern*dy; } ii = imin +i; if( ii