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

📄 gaussiantester.java

📁 一个基于PlaceLab的室内和室外的智能导航系统
💻 JAVA
字号:
/* * Created on Aug 2, 2004 * */package org.placelab.particlefilter;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import org.placelab.collections.UnsupportedOperationException;/** *  *  * The only thing you should want to change in here is 'paintComponent' in the DrawingPanel class. * */public class GaussianTester {		private static void createAndShowGUI() {	        //Create and set up the window.	        JFrame frame = new JFrame();	        Container contentPane = frame.getContentPane();	        contentPane.setLayout(new FlowLayout());	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();	        frame.setSize((int)Math.round(screenSize.width/1.5), (int)Math.round(screenSize.height/1.5));	        Dimension windowSize = frame.getSize();	        frame.setLocation(Math.max(0,(screenSize.width -windowSize.width)/2), 	                           Math.max(0,(screenSize.height-windowSize.height)/2));	        	        // Add a big panel for visualizing the gaussians	        final GaussianDrawingPanel mainPanel = new GaussianDrawingPanel(windowSize.width, windowSize.height);	        mainPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));	        contentPane.setLayout(new GridLayout());	       	        JButton gbutton = new JButton("GaussianSample");	  	        gbutton.addActionListener(new ActionListener() {	        	public void actionPerformed(ActionEvent e) {	        		mainPanel.gaussianSample();	        		mainPanel.repaint();	        	}	        });	        JButton ubutton = new JButton("UniformSample");	  	        ubutton.addActionListener(new ActionListener() {	        	public void actionPerformed(ActionEvent e) {	        		mainPanel.uniformSample();	        		mainPanel.repaint();	        	}	        });	        mainPanel.add(gbutton);	        mainPanel.add(ubutton);	        contentPane.add(mainPanel);	        	        //Display the window.	        //frame.pack();	        frame.setVisible(true);	    }	    public static void main(String[] args) {	        //Schedule a job for the event-dispatching thread:	        //creating and showing this application's GUI.	        javax.swing.SwingUtilities.invokeLater(new Runnable() {	            public void run() {	                createAndShowGUI();	            }	        });	    }		/**	 * 	 */	public GaussianTester() {		super();	}	}class GaussianDrawingPanel extends JPanel {	Dimension mySize;	public int axisBuffer = 50;		//	 define the bounds within which our gaussians are drawn	public double X_MIN = -150;	public double X_MAX = 0;	public double Y_MIN = 0;	public double Y_MAX = .1;			// keep these global to make things easy	MeanEstimateForFixedVarianceGaussian meanEstimate;	Gaussian groundTruth;	double publicSample;	double sampleWeight;	double oldMean, oldStdev;		public GaussianDrawingPanel(Dimension d) {		this.setSize(d);		mySize = d;		publicSample = 1000;				//get a distribution over the mean of a gaussian over a set of samples 		testMeanEstimateForFixedVarianceGaussian();	}	public GaussianDrawingPanel(int width, int height) {		this(new Dimension(width, height));	}		protected void paintComponent(Graphics g) {		// white background		g.setColor(new Color(255, 255, 255));		g.fillRect(0, 0, mySize.width, mySize.height);				// draw up 2 gaussians, the result of multiplying them, and the result of convolving them.		//staticMultiplyConvolveTest(g);						// draw up the groundTruth gaussian we're sampling from, and the estimate of the distributin		// over its mean		/*		drawGaussian(groundTruth, g, new Color(200, 200, 200), "True");		drawGaussian(meanEstimate, g, new Color(255, 0, 0), "Mean Distribution");		drawGaussian(new Gaussian(oldMean, oldStdev), g, new Color(255, 200, 200), "Old Mean Dist");		double si = 1.0-sampleWeight;		g.setColor(new Color((int)Math.round(255*si), (int)Math.round(255*si), 255));		g.fillOval(this.xValToPixel(publicSample)-5, this.yValToPixel(0)-5, 10, 10);		*/				// draw up a prior, a single sample with graduated weighting, and how each sample affects the posterior		staticWeightedSampleTest(g);				// draw on the axes at the end so they don't get covered up		drawAxes(g, new Color(0, 0, 0));			}		/* drawGaussian	 * ************	 * For each pixel in the x-direction:	 * 	--calculates the x-value corresponding to that pixel location	 * 	--calculates the y-value of the gaussian at that point	 *  --calculates the pixel location of that y-value	 *  --draws a dot at the appropriate (x,y) pixel location	 * Then writes the gaussian's label above its peak.	 */	protected void drawGaussian(Gaussian gaussian, Graphics g, Color color, String label) {		g.setColor(color);				int oldX = 0;		int oldY = mySize.height;		int pixelY;		for(int pixelX = 0; pixelX < mySize.width; pixelX++) {			pixelY = yValToPixel(gaussian.getHeightAt(pixelToXVal(pixelX)));			g.drawLine(oldX, oldY, pixelX, pixelY);			oldX = pixelX;			oldY = pixelY;		}						// draw the label above the gaussian's peak		int peakXpixel = xValToPixel(gaussian.getMean()); 		int peakYpixel = yValToPixel(gaussian.getHeightAt(gaussian.getMean()));		g.drawString(label, peakXpixel-10, peakYpixel-10);			}		protected void drawGaussian(Gaussian gaussian, Graphics g, Color color) {		drawGaussian(gaussian, g, color, "");	}		private double pixelToXVal(int pixelX) {		double xRange = X_MAX - X_MIN;		double xPerPixel = -1.0*xRange/mySize.width;		double x = pixelX*xPerPixel; 		return x;	}		private double pixelToYVal(int pixelY) {		double yRange = Y_MAX - Y_MIN;		double yPerPixel = yRange/(mySize.height-axisBuffer);		double yFromTop = pixelY * yPerPixel;		double y = yRange - yFromTop;		return y;	}		private int xValToPixel(double xVal) {		double xRange = X_MAX - X_MIN;		double pixelPerX = (mySize.width/xRange); 		double pixelX = mySize.width-(xVal-X_MIN)*pixelPerX;		return (int)Math.round(pixelX);	}		private int yValToPixel(double yVal) {		double yRange = Y_MAX - Y_MIN;		double pixelPerY = (mySize.height-axisBuffer)/yRange;		double yFromTop = yRange - yVal;		double pixelsFromTop = yFromTop*pixelPerY;		return (int)Math.round(pixelsFromTop);	}		private void drawAxes(Graphics gc, Color c) {		gc.setColor(c);				// draw the x-axis		gc.drawLine(0, mySize.height-axisBuffer, mySize.width, mySize.height-axisBuffer);		int bottomY = mySize.height-axisBuffer+5;		int topY = mySize.height-axisBuffer-5;		for(int tick=0; tick<mySize.width; tick+=100) {			gc.drawLine(tick, bottomY, tick, topY);			double d = pixelToXVal(tick);			d *=1000;			d = Math.round(d);			d /=1000;			Double D = new Double(d);			gc.drawString(D.toString(), tick, bottomY+8);		}				// draw the y axis		int middle = mySize.width/2;		gc.drawLine(middle, 0, middle, mySize.height-axisBuffer);		for(int tick=0; tick<mySize.height-axisBuffer; tick+=100) {			gc.drawLine(middle-5, tick, middle+5, tick);			double d = pixelToYVal(tick);			d *= 1000;			d = Math.round(d);			d /= 1000;			Double D = new Double(d);						gc.drawString(D.toString(), middle+8, tick+5);		}	}		public void gaussianSample() {				// first we generate a sample from the true distribution		double p = Math.random();		sampleWeight = Math.random();		//System.out.println("p: "+p);		double total = 0.0;		double increment = .001;		double r=0.0;		for(r=X_MIN; r<X_MAX; r+=increment) {			total += groundTruth.getHeightAt(r)*increment;			if (total >= p) break;		}		if (r >= X_MAX) throw new UnsupportedOperationException("Julie messed up in GaussianTester!");				// now we update the gaussian by the sample		publicSample = r;		//System.out.println("publicSample: "+publicSample);		oldMean = meanEstimate.getMean();		oldStdev = meanEstimate.getSigma();		meanEstimate.updateWithSample(publicSample);	}		public void uniformSample() {		double xrange = X_MAX-X_MIN;		double x = (xrange*Math.random())+X_MIN;		//		 now we update the gaussian by the sample		publicSample = x;		sampleWeight = Math.random();		//System.out.println("publicSample: "+publicSample);		oldMean = meanEstimate.getMean();		oldStdev = meanEstimate.getSigma();		meanEstimate.updateWithSample(publicSample);				}		public void testMeanEstimateForFixedVarianceGaussian() {		double trueMean = -50.0;		double trueStdev = 5.0;		groundTruth = new Gaussian(trueMean, trueStdev);		meanEstimate = new MeanEstimateForFixedVarianceGaussian(-70.0, 50, 50.0); 	}		public void staticMultiplyConvolveTest(Graphics g) {						// draw a simple gaussian		Gaussian g1 = new Gaussian(-70.0, 10.0);		drawGaussian(g1, g, new Color(255, 0, 0), "G1");				// draw another gaussian		Gaussian g2 = new Gaussian(-30.0, 20);		drawGaussian(g2, g, new Color(0, 0, 255), "G2");				// draw the multiplication of the two		Gaussian mult = new Gaussian(g2);		mult.multiplyBy(g1);		drawGaussian(mult, g, new Color(255, 0, 255), "Multiplied");				// draw the convolution of the two		Gaussian conv = new Gaussian(g2);		conv.smoothBy(g1);		drawGaussian(conv, g, new Color(0, 255, 0), "2 Conv 1");				// draw the other convolution		Gaussian conv2 = new Gaussian(g1);		conv2.smoothBy(g2);		drawGaussian(conv2, g, new Color(0, 255, 0), "1 Conv 2");		}		public void staticWeightedSampleTest(Graphics g) {		// draw a simple prior		MeanEstimateForFixedVarianceGaussian prior = new MeanEstimateForFixedVarianceGaussian(-90.0, 15, 5);		MeanEstimateForFixedVarianceGaussian posterior;		drawGaussian(prior, g, new Color(255, 0, 0));				// draw the location of the sample		double sample = -60.0;		g.setColor(new Color(0, 255, 0));		g.fillRect(this.xValToPixel(sample)-5, this.yValToPixel(0)-5, 10, 10);				//draw the posterior for each gradation of weighting of the sample		double weight, iw;		Color c;		int j;		double increment = .1;		for(weight = increment; weight<=1.0; weight+= increment) {			posterior = new MeanEstimateForFixedVarianceGaussian(prior);			posterior.updateWithSample(sample, weight);			iw = 1.0-weight;			j = (int)Math.round(iw*255);			c = new Color(j, j, 255);			drawGaussian(posterior, g, c);		}	}	}

⌨️ 快捷键说明

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