⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsom1.java

📁 wecome to sumathi computer centre is the centre for
💻 JAVA
字号:
/* * @(#)Bsom1.java	1.0   Nov 20 96  Akio Utsugi */import java.applet.Applet;import java.awt.*;import java.lang.Math;import java.lang.Double;import java.net.URL;import java.util.Vector;import java.io.*;/** * Bsom1 is an applet for the application of Bayesian Self-Organizing Maps. * The following parameters are used: * 'data' is the number of data points. (def. 100) * 'unit' is the number of inner units (weight points). (def. 20) * 'amin' is the power of 10 for minimum alpha. (def. 0.) * 'amax' is the power of 10 for maximum alpha. (def. 5.) * 'bmin' is the power of 10 for minimum beta. (def. 0.) * 'bmax' is the power of 10 for maximum beta. (def. 5.) * 'init_weight' is the size of initial random weights. (def. 0.1) * 'input' is the name of a data file. * (xd, yd) is the position of origin of mathematical space in display. (def. 210, 150) * 'scale' is the scale factor from mathematical space to display. (def. 70) * 'dstep' is the size of a cell for density calculation. (def. 10) * @version 1.0   20 Nov 1996 * @author <a href="http://www.aist.go.jp/NIBH/~b0616/"> Akio Utsugi </a> * This applet uses Matrix.class(1.3), Slider.class(reformed version) * and class files made from BsomDemo1.java(1.3). */public class Bsom1 extends BsomDemo1{    BsomDrawArea bsomDrawArea;        TextField inputText;    TextField dstepText;    String input_file;    Button outputButton;            public void init()    {	String p;	Vector buf = null;			/* Get parameters. */		p = getParameter("data");	if(p == null){	    p = "100";	}	n_data = Integer.valueOf(p).intValue();		p = getParameter("unit");	if(p == null){	    p = "20";	}	n_unit = Integer.valueOf(p).intValue();		p = getParameter("amin");	if(p == null){	    p = "0.";	}	alpha_min_power = Double.valueOf(p).doubleValue();		p = getParameter("amax");	if(p == null){	    p = "5.";	}	alpha_max_power = Double.valueOf(p).doubleValue();		p = getParameter("bmin");	if(p == null){	    p = "0.";	}	beta_min_power = Double.valueOf(p).doubleValue();		p = getParameter("bmax");	if(p == null){	    p = "5.";	}	beta_max_power = Double.valueOf(p).doubleValue();		initial_alpha = Math.pow(10., alpha_max_power);	initial_beta = Math.pow(10., beta_min_power);		p = getParameter("xd");	if(p == null){	    p = "210";	}	xd = Integer.valueOf(p).intValue();		p = getParameter("yd");	if(p == null){	    p = "150";	}	yd = Integer.valueOf(p).intValue();		p = getParameter("scale");	if(p == null){	    p = "70";	}	scale = (double)Integer.valueOf(p).intValue();		p = getParameter("dstep");	if(p == null){	    p = "10";	}	dstep = Integer.valueOf(p).intValue();			p = getParameter("init_weight");	if(p == null){	    p = "0.1";	}	initial_weight_level = Double.valueOf(p).doubleValue();		input_file = getParameter("input");		if(input_file != null){	    buf = loadData(input_file);	    	}	if(buf != null){	    n_data = buf.size()/2;	}	else{	    showStatus("Input file is required.");	    stop();	}		/* Layout */		setLayout(new BorderLayout());		// Draw area	bsomDrawArea = new BsomDrawArea(this, xd, yd, scale);	drawArea = bsomDrawArea;	bsomDrawArea.n_data = n_data;	bsomDrawArea.n_unit = n_unit;	bsomDrawArea.dstep = dstep;		bsomDrawArea.initial_weight_level = initial_weight_level;		bsomDrawArea.alpha = initial_alpha;	bsomDrawArea.beta = initial_beta;		bsomDrawArea.buf = buf;		add("Center", bsomDrawArea);		// Control panel	Panel controlPanel = new Panel();		// Button panel	Panel buttonPanel = new Panel();		buttonPanel.setLayout(new GridLayout(4, 1));		learnButton = new Checkbox("learn");	autoButton = new Checkbox("auto");	densityButton = new Checkbox("density");	initButton = new Button("init");		buttonPanel.add(learnButton);	buttonPanel.add(autoButton);	buttonPanel.add(densityButton);	buttonPanel.add(initButton);		controlPanel.add(buttonPanel);			// Weight panel	Panel weightPanel = new Panel();	weightPanel.setLayout(new GridLayout(2, 1));		// Alpha slider panel	Panel alphaSliderPanel = new Panel();	alphaSliderPanel.setLayout(new BorderLayout());	alphaSlider = new AlphaSlider(bsomDrawArea, alpha_min_power, alpha_max_power);	Label alabel = new Label("alpha");	alphaSliderPanel.add("Center", alphaSlider);	alphaSliderPanel.add("South", alabel);	weightPanel.add(alphaSliderPanel);			// Beta slider panel	Panel betaSliderPanel = new Panel();	betaSliderPanel.setLayout(new BorderLayout());	betaSlider = new BetaSlider(bsomDrawArea, beta_min_power, beta_max_power);	Label blabel = new Label("beta");	betaSliderPanel.add("Center", betaSlider);	betaSliderPanel.add("South", blabel);	weightPanel.add(betaSliderPanel);		controlPanel.add(weightPanel);		// IO Panel	Panel IOPanel = new Panel();	IOPanel.setLayout(new GridLayout(4,1));		Panel inputPanel = new Panel();	inputPanel.setLayout(new GridLayout(1, 2));	Label inputLabel = new Label("Input File");	inputText = new TextField(input_file, 10);	inputPanel.add(inputLabel);	inputPanel.add(inputText);	IOPanel.add(inputPanel);		Panel unitNumberPanel = new Panel();	unitNumberPanel.setLayout(new GridLayout(1, 2));	unitNumberText = new TextField(""+n_unit, 2);	Label unitNumberLabel = new Label("#unit");	unitNumberPanel.add(unitNumberLabel);	unitNumberPanel.add(unitNumberText);	IOPanel.add(unitNumberPanel);		Panel dstepPanel = new Panel();	dstepPanel.setLayout(new GridLayout(1, 2));	dstepText = new TextField(""+dstep, 3);	Label dstepLabel = new Label("dstep");	dstepPanel.add(dstepLabel);	dstepPanel.add(dstepText);	IOPanel.add(dstepPanel);		Panel outputPanel = new Panel();	outputPanel.setLayout(new GridLayout(1,2));	outputButton = new Button("output");	Label dmy = new Label(" ");	outputPanel.add(outputButton);	outputPanel.add(dmy);	IOPanel.add(outputPanel);		controlPanel.add(IOPanel);		add("South", controlPanel);			// Set initial values of sliders.	alphaSlider.setValueOfSlider(initial_alpha);	betaSlider.setValueOfSlider(initial_beta);		bsomDrawArea.init();	       	repaint();	    }            public void start()    {		if(!bsomDrawArea.density_mode){	    bsomDrawArea.start();	}    }        public void stop()    {	bsomDrawArea.stop();    }        public synchronized boolean action(Event evt, Object what)    {        if(super.action(evt, what))            return true;		if(evt.target == inputText){	    reloadData((String)what);	    return true;	}		if(evt.target == dstepText){	    dstep = Integer.valueOf((String)what).intValue();	    bsomDrawArea.dstep = dstep;	    return true;	}		if(evt.target == outputButton){	    outputWeight();	    return true;	}	return false;    }            Vector loadData(String input_file)    {        InputStream is = null;        Vector buf = null;        StreamTokenizer st = null;	        try{	    is = new URL(getDocumentBase(), input_file).openStream();	}	catch(Exception e){	    showStatus("The file can't be opened.");	                try{		if(is != null){		    is.close();		}	    } catch(Exception e2){	    }	    return buf;	}			try{	    st = new StreamTokenizer(is);	    st.eolIsSignificant(true);	    st.commentChar('#');	    	    buf = new Vector();	    	    while(true){		if(st.nextToken() == StreamTokenizer.TT_NUMBER){		    Double val1 = new Double(st.nval);		    buf.addElement((Object)val1);		    if(st.nextToken() == StreamTokenizer.TT_NUMBER){			Double val2 = new Double(st.nval);			buf.addElement((Object)val2);			if(st.nextToken() != StreamTokenizer.TT_EOL)			    break;		    }		    else			break;		    		}		else if(st.ttype != StreamTokenizer.TT_EOL)		    break;			    }	    	    is.close();	}	catch(Exception e){	}		if(st.ttype != StreamTokenizer.TT_EOF){	    buf.removeAllElements();	    buf = null;	    showStatus("Format Error.");	}	else	    showStatus("New data are loaded.");		return buf;    }        public void reloadData(String input_file)    {        Vector buf = null;	        if(!input_file.equals("")){            buf = loadData(input_file);        }        else{            buf = null;        }        if(buf == null)            return;		        bsomDrawArea.alpha = initial_alpha;        bsomDrawArea.beta = initial_beta;        bsomDrawArea.buf = buf;        bsomDrawArea.auto_learn = false;        bsomDrawArea.learn = false;        bsomDrawArea.initData();        bsomDrawArea.W.init();        bsomDrawArea.draw_new_data = true;	        autoButton.setState(false);        learnButton.setState(false);        alphaSlider.setValueOfSlider(initial_alpha);        betaSlider.setValueOfSlider(initial_beta);        repaint();    }        public void outputWeight()    {        int i;        double x, y;        Weight W = bsomDrawArea.W;        UserData UX = (UserData)bsomDrawArea.X;        double dscale = UX.dscale;        double mx = UX.mx;        double my = UX.my;	        for(i=0; i<W.row; i++){            x = W.value[i][0]/dscale + mx;            y = W.value[i][1]/dscale + my;            System.out.println(x+" "+y);        }        System.out.println(" ");    }}class UserData extends Data{        double dscale;  // Scale of data    double mx;    double my;        UserData(int n, double scale, int radius, int x, int y, Vector input)    {	super(n, scale, radius, x, y, 1.,1.,1.,1.);		int i;		// Obtain means.	mx = 0.;	my = 0.;		for(i = 0; i < n; i++){	    value[i][0] = (double)((Double)(input.elementAt(2*i))).doubleValue();	    value[i][1] = (double)((Double)(input.elementAt(2*i+1))).doubleValue();	    mx += value[i][0];	    my += value[i][1];	}		mx /= n;	my /= n;		// Move the means to zeros and then get the range.	double dmax = 0;		for(i = 0; i < n; i++){	    value[i][0] -= mx;	    value[i][1] -= my;	    if(Math.abs(value[i][0]) > dmax){	        dmax = Math.abs(value[i][0]);	    }	    if(Math.abs(value[i][1]) > dmax){	        dmax = Math.abs(value[i][1]);	    }	}		// Resize the data to go into a 2*2 box.	dscale = 2./dmax;		for(i = 0; i < n ; i++){	    	    value[i][0] *= dscale;	    value[i][1] *= dscale;	}		    }}class BsomDrawArea extends DrawArea{    Vector buf;        BsomDrawArea(Bsom1 demo, int xd, int yd, double scale)    {        super(demo,xd,yd,scale);    }            void initData()    {	n_data = buf.size()/2;	X = new UserData(n_data, scale, 0, xd, yd, buf);	    }                /* Double-buffering is used for drawing objects. */        public boolean draw_new_data;    Graphics datagraphics;    Image datascreen;        public synchronized void update(Graphics g)    {		Dimension d = size();		if((offscreen == null) || (d.width != offscreensize.width) ||	   (d.height != offscreensize.height)){	    offscreen = createImage(d.width, d.height);	    offscreensize = d;	    offgraphics = offscreen.getGraphics();	    draw_new_data = true;	}	if(draw_new_data){	    datascreen = createImage(d.width, d.height);	    datagraphics = datascreen.getGraphics();	    datagraphics.setColor(getBackground());	    datagraphics.fillRect(0, 0, d.width, d.height);	    datagraphics.setColor(Color.black);	    X.draw(datagraphics);	    draw_new_data = false;	}		if(!density_mode){	    offgraphics.drawImage(datascreen, 0, 0, null);	}	else{	    g.drawString("Now, density is calculated.", 0, 10);	    drawDensity(offgraphics);	    offgraphics.setColor(Color.red);	    	    UserData UX = (UserData)X;	    double s = UX.dscale * UX.dscale;	    	    offgraphics.drawString("a="+alpha*s, 0, 10);	    offgraphics.drawString("b="+beta*s, 0, 20);	    offgraphics.setColor(Color.green);	    X.draw(offgraphics);	}		offgraphics.setColor(Color.blue);	W.draw(offgraphics);	W.drawCurve(offgraphics);	paint(g);	    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -