/*
 This is a proprietary part of DataMelt package.
 License: the Creative Commons Attribution-Share Alike License; either version 3.0 of the License, 
 or any later version.
 http://creativecommons.org/licenses/by-sa/3.0/
 

Redistributions of the source code, or parts of the source codes, must retain the copyright notice, this list of conditions and the following disclaimer (all at the top of each source code) and requires the written permission from DataMelt.

Redistribution in binary form of all or parts of a class must reproduce the copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution and requires the written permission of ScaVis

Details about license agreement or commercial use are: http://jwork.org/scavis/ */ package jhpro.nnet; import java.applet.Applet; import java.awt.*; public class Example extends Applet implements Runnable { public void init() { if(parametersFrame == null) parametersFrame = new ParametersFrame(600, 10); if(kfmThread == null) { kfmThread = new Thread(this, "kfmThread"); kfmThread.setPriority(10); } setup(); } public void setup() { setLayout(new BorderLayout()); drawCanvas = new Canvas(); statusPanel = new Panel(); statusPanel.setFont(statusFont); statusPanel.setBackground(Color.lightGray); statusPanel.setLayout(new GridLayout(4, 4, 4, 0)); statusLabel = new Label[8]; for(int i = 0; i < 8; i++) { statusLabel[i] = new Label("", 0); statusLabel[i].setFont(statusTextFont); } statusPanel.add(new Label("input values:", 2)); statusPanel.add(statusLabel[0]); statusPanel.add(new Label("learning cycle:", 2)); statusPanel.add(statusLabel[1]); statusPanel.add(new Label("input layer:", 2)); statusPanel.add(statusLabel[2]); statusPanel.add(new Label("learning rate:", 2)); statusPanel.add(statusLabel[3]); statusPanel.add(new Label("map size:", 2)); statusPanel.add(statusLabel[4]); statusPanel.add(new Label("activation area:", 2)); statusPanel.add(statusLabel[5]); statusPanel.add(new Label("weights:", 2)); statusPanel.add(statusLabel[6]); statusPanel.add(new Label("elapsed time:", 2)); statusPanel.add(statusLabel[7]); buttonsPanel = new Panel(); buttonsPanel.setFont(statusFont); buttonsPanel.setLayout(new GridLayout(1, 6, 2, 4)); startButton = new Button("Start learning"); stopButton = new Button("Stop learning"); resetButton = new Button("Reset"); parametersButton = new Button("Parameters"); exitButton = new Button("Exit"); buttonsPanel.add(startButton); buttonsPanel.add(stopButton); buttonsPanel.add(resetButton); buttonsPanel.add(parametersButton); buttonsPanel.add(exitButton); Panel panel = new Panel(); panel.setLayout(new BorderLayout()); panel.setBackground(Color.lightGray); panel.add("South", buttonsPanel); panel.add("Center", statusPanel); add("South", panel); add("Center", drawCanvas); show(); itLearns = false; startButton.enable(); stopButton.disable(); } public void start() { if(itLearns) { if(kfmThread == null) { kfmThread = new Thread(this, "kfmThread"); kfmThread.setPriority(10); } kfmThread.start(); kfm.resetTime(); return; } else { setNetParameters(); return; } } public void stop() { if(kfmThread != null) { kfmThread.stop(); kfmThread = null; } } public void destroy() { if(parametersFrame != null) { parametersFrame.dispose(); parametersFrame = null; } if(kfmThread != null) { kfmThread.stop(); kfmThread = null; } } public void setNetParameters() { d = drawCanvas.size(); offscreen = drawCanvas.createImage(d.width + 1, d.height + 1); kfm = new KohonenFeatureMap(); boundingBox = new Point3D[8]; boundingBoxDraw = new Point[8]; inputSize = parametersFrame.getInputValues(); im = new InputMatrix(inputSize, 3); inputX = new int[inputSize]; inputY = new int[inputSize]; inputZ = new int[inputSize]; inputs = new Point3D[inputSize]; inputsDraw = new Point[inputSize]; inputDimension = parametersFrame.getDim(); xSize = parametersFrame.getRangeX(); ySize = parametersFrame.getRangeY(); zSize = parametersFrame.getRangeZ(); td = new ThreeD(d.width + 1, d.height + 1, xSize, ySize, zSize); mapSizeX = parametersFrame.getMapSizeX(); mapSizeY = parametersFrame.getMapSizeY(); weights = new Point3D[mapSizeX * mapSizeY]; weightsDraw = new Point[mapSizeX * mapSizeY]; if(mapSizeY == 0) kfm.createMapLayer(mapSizeX, mapSizeX); else kfm.createMapLayer(mapSizeX, mapSizeY); kfm.setInitLearningRate(parametersFrame.getInitRate()); kfm.setInitActivationArea(parametersFrame.getInitArea()); maxCycle = parametersFrame.getStopCycle(); kfm.setMaxLearningCycles(maxCycle); stopArea = parametersFrame.getStopArea(); kfm.setStopArea(stopArea); displayStep = parametersFrame.getDisplayStep(); kfm.setDisplayStep(displayStep); for(int i = 0; i < inputSize; i++) { inputX[i] = (int)(Math.random() * (double)(xSize << 1) - (double)xSize); inputY[i] = (int)(Math.random() * (double)(ySize << 1) - (double)ySize); inputZ[i] = (int)(Math.random() * (double)(zSize << 1) - (double)zSize); inputs[i] = new Point3D(inputX[i], inputY[i], inputZ[i]); } im.setInputValues(inputX, inputY, inputZ); kfm.connectLayers(im); statusLabel[0].setText(String.valueOf(inputs.length)); statusLabel[2].setText(String.valueOf(inputDimension + " neurons")); statusLabel[4].setText(mapSizeX + "x" + mapSizeY + " neurons"); statusLabel[6].setText(String.valueOf(mapSizeX * mapSizeY * inputDimension)); boundingBox[0] = new Point3D(xSize, ySize, zSize); boundingBox[1] = new Point3D(xSize, ySize, -zSize); boundingBox[2] = new Point3D(-xSize, ySize, zSize); boundingBox[3] = new Point3D(-xSize, ySize, -zSize); boundingBox[4] = new Point3D(xSize, -ySize, zSize); boundingBox[5] = new Point3D(xSize, -ySize, -zSize); boundingBox[6] = new Point3D(-xSize, -ySize, zSize); boundingBox[7] = new Point3D(-xSize, -ySize, -zSize); td.setAngles(0, 0, 0); td.setAngleSteps(0, 0, 0); setWeights(kfm.getWeightValues()); updateStatus(kfm.getLearningCycle(), kfm.getLearningRate(), kfm.getActivationArea(), kfm.getElapsedTime()); draw(); td.setAngleSteps(parametersFrame.getSpeedX(), parametersFrame.getSpeedY(), parametersFrame.getSpeedZ()); } public void run() { while(Thread.currentThread() == kfmThread) { while(!kfm.finishedLearning()) { kfm.learn(); if(kfm.displayNow()) try { setWeights(kfm.getWeightValues()); draw(); updateStatus(kfm.getLearningCycle(), kfm.getLearningRate(), kfm.getActivationArea(), kfm.getElapsedTime()); Thread.sleep(25L); } catch(InterruptedException _ex) { } } startButton.disable(); stopButton.disable(); updateStatus(kfm.getLearningCycle(), kfm.getLearningRate(), kfm.getActivationArea(), kfm.getElapsedTime()); for(double d1 = kfm.getActivationArea(); d1 > 0.0D;) try { draw(); Thread.sleep(25L); } catch(InterruptedException _ex) { } } } public void updateStatus(int i, double d1, double d2, String s) { setFont(statusTextFont); statusLabel[1].setText(String.valueOf(i)); statusLabel[3].setText(String.valueOf(d1)); statusLabel[5].setText(String.valueOf(d2)); statusLabel[7].setText(s + " sec"); } public void setWeights(float af[][]) { for(int i = 0; i < weights.length; i++) weights[i] = new Point3D((int)af[0][i], (int)af[1][i], (int)af[2][i]); } public final synchronized void draw() { td.changeAngles(); boundingBoxDraw = td.rotate3Dto2D(boundingBox); inputsDraw = td.rotate3Dto2D(inputs); weightsDraw = td.rotate3Dto2D(weights); Graphics g = offscreen.getGraphics(); g.setColor(sandColor); g.fillRect(0, 0, d.width + 1, d.height + 1); g.setColor(Color.blue); for(int i = 0; i < inputsDraw.length; i++) g.fillRect(inputsDraw[i].x - 2, inputsDraw[i].y - 2, 4, 4); g.setColor(boxColor); for(int j = 0; j < 7; j += 2) g.drawLine(boundingBoxDraw[j].x, boundingBoxDraw[j].y, boundingBoxDraw[j + 1].x, boundingBoxDraw[j + 1].y); for(int k = 0; k < 4; k++) g.drawLine(boundingBoxDraw[k].x, boundingBoxDraw[k].y, boundingBoxDraw[k + 4].x, boundingBoxDraw[k + 4].y); g.drawLine(boundingBoxDraw[0].x, boundingBoxDraw[0].y, boundingBoxDraw[2].x, boundingBoxDraw[2].y); g.drawLine(boundingBoxDraw[1].x, boundingBoxDraw[1].y, boundingBoxDraw[3].x, boundingBoxDraw[3].y); g.drawLine(boundingBoxDraw[4].x, boundingBoxDraw[4].y, boundingBoxDraw[6].x, boundingBoxDraw[6].y); g.drawLine(boundingBoxDraw[5].x, boundingBoxDraw[5].y, boundingBoxDraw[7].x, boundingBoxDraw[7].y); g.setColor(Color.darkGray); for(int l = 1; l < weightsDraw.length; l++) if(l % mapSizeY != 0) g.drawLine(weightsDraw[l - 1].x, weightsDraw[l - 1].y, weightsDraw[l].x, weightsDraw[l].y); for(int i1 = 0; i1 < mapSizeY; i1++) { for(int j1 = 1; j1 < mapSizeX; j1++) { int k1 = (j1 - 1) * mapSizeY + i1; int l1 = j1 * mapSizeY + i1; g.drawLine(weightsDraw[k1].x, weightsDraw[k1].y, weightsDraw[l1].x, weightsDraw[l1].y); } } g = drawCanvas.getGraphics(); g.drawImage(offscreen, 0, 0, drawCanvas); } public boolean handleEvent(Event event) { if(event.id == 201) { if(parametersFrame != null) { parametersFrame.dispose(); parametersFrame = null; } stop(); System.exit(0); } return super.handleEvent(event); } public boolean action(Event event, Object obj) { if(event.target == startButton) { itLearns = true; startButton.disable(); stopButton.enable(); start(); return false; } if(event.target == resetButton) { itLearns = false; if(kfmThread != null) { kfmThread.stop(); kfmThread = null; } startButton.enable(); stopButton.disable(); setNetParameters(); return false; } if(event.target == stopButton) { startButton.disable(); stopButton.disable(); maxCycle = kfm.getLearningCycle(); return false; } if(event.target == parametersButton) { if(parametersFrame == null) parametersFrame = new ParametersFrame(600, 10); else if(!parametersFrame.isShowing()) parametersFrame.show(); return false; } if(event.target == exitButton) { parametersFrame.dispose(); parametersFrame = null; if(kfmThread != null) { kfmThread.stop(); kfmThread = null; } stop(); System.exit(0); } if(event.id == 201) { if(parametersFrame != null) { parametersFrame.dispose(); parametersFrame = null; } stop(); System.exit(0); } return false; } public String getAppletInfo() { String s = ""; s += "\nName: 3D Kohonen Feature Map"; s += "\n\nDescription:\nThis applet demonstrates a Kohonen Feature Map Neural Net"; s += "\nwith a 2- or 3-dimensional input neuron layer."; s += "\nIt uses specific classes for neural net components which I"; s += "\nimplemented for my diploma at the Fachhochschule Regensburg."; s += "\n\nAuthor: Jochen Froehlich ( jochen.froehlich@stud.fh-regensburg.de )"; s += "\n\nLast modified: 18-Nov-1996"; s += "\n\nCopyright 1996 by Jochen Froehlich"; s += "\nAll rights reserved"; return s; } public String[][] getParameterInfo() { String as[][] = { { "", "", "" } }; return as; } public Example() { } final Color sandColor = new Color(192, 192, 192); final Color boxColor = new Color(160, 160, 160); final Font statusFont = new Font("Helvetica", 0, 12); final Font statusTextFont = new Font("Helvetica", 1, 12); final Font headingFont = new Font("Helvetica", 1, 14); Thread kfmThread; KohonenFeatureMap kfm; ParametersFrame parametersFrame; InputMatrix im; Canvas drawCanvas; Panel statusPanel; Panel buttonsPanel; ThreeD td; Label statusLabel[]; Button startButton; Button stopButton; Button resetButton; Button parametersButton; Button infoButton; Button exitButton; Image offscreen; Dimension d; int xSize; int ySize; int zSize; int mapSizeX; int mapSizeY; int inputSize; int inputDimension; int inputX[]; int inputY[]; int inputZ[]; int maxCycle; int displayStep; double stopArea; Point3D boundingBox[]; Point3D inputs[]; Point3D weights[]; Point boundingBoxDraw[]; Point inputsDraw[]; Point weightsDraw[]; boolean itLearns; }

Ads help maintain this website.