博主比较懒,这也是为数不多的认真地用Java进行的仿真实验,一般不都是用Matlab嘛。。。

//package 传感器网络
//Setnet.java
package 传感器网络;
import java.util.*;
import org.ujmp.core.DenseMatrix;
import org.ujmp.core.Matrix;
import org.ujmp.core.doublematrix.DoubleMatrix2D;
public class Setnet {
	double radius;
	long number;
	double[] x,y;
	DoubleMatrix2D Setup;
	public DoubleMatrix2D Setup()
	{
		try{
		x=new double[(int)number];
		y=new double[(int)number];
		
		for(long i=number-1;i>=0;i--)
		{
		 x[(int) i]=Math.random();

		}
		for(long j=number-1;j>=0;j--)
		{
		 y[(int) j]=Math.random();
		}
		
		Setup=DenseMatrix.Factory.zeros(number, number);
		for(int l=0;l<=(int)number-1;l++)
		{
		for(int k=l+1;k<=(int)number-1;k++)
		{
	     double d;
	 
	    	 
	     d=Math.pow(Math.pow(x[l]-x[k], 2)+Math.pow(y[l]-y[k], 2),0.5);
	     //System.out.println(d);
	     
	    // else d=Math.pow(Math.pow(x[l]-x[k], 2)+Math.pow(y[l]-y[k], 2),0.5);

	     
	     
		 if(d<=radius)
		 {
			 //System.out.println(d);
			
			 Setup.setDouble(1, l, k);
			 Setup.setDouble(1, k, l);
			 
			
			 
		 }
		// System.out.println(d);
		// System.out.println(Setup);
		//System.out.println(radius);
		 
		 
		}
		
		}
		for(int l=0;l<=(int)number-1;l++)
		{
		for(int k=0;k<=(int)number-1;k++)
		{
			if(k==l)
			{
				Setup.setDouble(1, l, k);
			}
		}
		}
		
		//System.out.println(Setup);
		//System.out.println(radius);
		return Setup;
		
		}
	catch(Exception e)
	{
		e.printStackTrace();
	
	}
	finally{
			return Setup;
		}
	
	}

}
//NetWork.java
package 传感器网络;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

import org.ujmp.core.DenseMatrix;
import org.ujmp.core.Matrix;
import org.ujmp.core.calculation.Calculation.Ret;
import org.ujmp.core.doublematrix.DoubleMatrix2D;
public class NetWork {
	int count;
	DoubleMatrix2D dsm;
	boolean k=true;
	public double NetWork(long dimension,double radius)
	{	
     Setnet network=new Setnet();
     network.number=dimension;
     network.radius=radius;
     long i = 0;
     Matrix ds=Matrix.Factory.ones(dimension,dimension);
     Matrix d=Matrix.Factory.zeros(dimension,dimension);
     Matrix d1=Matrix.Factory.zeros(dimension,dimension);
     //double[][] dd=new double[(int) dimension][(int)dimension];
     //dd=ds.toDoubleArray();
     //System.out.println(radius);
     for(int i1=1000;i1>0;i1--)
     {
      //System.out.println(radius);
      //System.out.println(dimension);
      DoubleMatrix2D dsm=network.Setup();
      //double[][] d=new double[(int) dimension][(int)dimension];
     // d=dsm.toDoubleArray();
     /* for(int j=0;j<=d.length-1;j++)
      {
    	  for(int k=0;k<=d.length-1;k++)
    	  {
    	    if(d[j][k]==dd[j][k]&this.k==true)
    	    {
    	    	this.k=true;
    	    }
    	    else this.k=false;
    			  
    			
    	  }
      }*/
      //System.out.println(dsm);
      //System.out.println(ds);
      d=dsm;
      d1=dsm;
      i=0;
      while(i<dimension-1&dimension!=0)
      {
    	  dsm=(DoubleMatrix2D) dsm.mtimes(d1);
    	  d=d.plus(dsm);
    	  i++;
      }
         //System.out.println(d);
      System.out.println(d);
      if(!d.containsDouble(0)&dimension!=0)
      {
 		  count++;
 		  System.out.println(d);
 		  //System.out.println(count);
      }
      //System.out.println(count);
      }
      BigDecimal count1=new BigDecimal(count);
      BigDecimal b=new BigDecimal(1000);
      MathContext mc=new MathContext(1,RoundingMode.HALF_UP);
      BigDecimal calResult=count1.divide(b,mc);
      
     //System.out.println(count);
     return calResult.doubleValue();
    
     
}
}
//Graph.java
package 传感器网络;
import java.math.BigDecimal;
import java.util.Vector;

import org.jfree.chart.*;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.*;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

import org.ujmp.core.DenseMatrix;
import org.ujmp.core.Matrix;
public class Graph {
    private static final Object[] DefaultCategoryDataset = null;
	static double[] radius=new double[51];
    static long[] numbers=new long[21];
    static double[] ds=new double[1071];
    public Graph()
    {
    int k=0,l=0,q=0;
    BigDecimal b,c;
    NetWork net;
    net=new NetWork();
    c=BigDecimal.valueOf(0.02);
   	b=BigDecimal.valueOf(0.00);
    for(double i=0;i<=1;k++)
    {   
    	radius[k]=i;
    	b=b.add(c);
    	i=b.doubleValue();
 
    }
    
    for(long i=0;i<=100;l++)
    {
    	numbers[l]=i;
    	i+=5;
    }
    
	for(int n=0;n<l;n++)
	{
		System.out.println(numbers[n]);
		for(int m=0;m<k;m++)
		{   
			
			ds[q]=net.NetWork(numbers[n], radius[m]);
			net.count=0;
			//System.out.println(radius[m]);
		//	System.out.println(ds[q]);
			q++;
			
			
		}
		//System.out.println(radius[m]);

	}
	
	
	

	//DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	//JFreeChart chart=ChartFactory.createXYLineChart("Connectivity in G(n,R)", "Communication radius", "Pr[network is connected]", (XYDataset) dataset, PlotOrientation.HORIZONTAL, true, true, true);
	//chart.getPlot();
	
    }
	public static void main(String args[])
	{
       
		Graph gh=new Graph();
		Draw dw=new Draw("n=0",1);
		String[] s={"0","5","10","15","20","25","30","35","40","45","50","55","60","65","70","75","80","85","90","95","100"};
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
		for(int j=0;j<21;j++)
		{
		for(int i=0;i<51;i++)
		{
  
			dw.radius=radius[i];
			dw.ds=ds[j*51+i];
			dataset.addValue(dw.ds, s[j], String.valueOf(radius[i]));

		    		
           
			
		}
		
		}
		for(int j=0;j<51;j++)
		{
			for(int i=0;i<21;i++)
			{
				dataset1.addValue(ds[j*21+i], String.valueOf(radius[j]), s[i]);
			}
		}
		Draw dw2=new Draw("Connectivity in G(n,R)",dataset);
		Draw2 dw3=new Draw2("Connectivity in G(n,R)",dataset1);
        //System.out.println(Draw.createDataset(dw2.radius, dw2.ds));
        dw2.pack();
        RefineryUtilities.centerFrameOnScreen(dw2);
		dw2.setVisible(true);
        dw3.pack();
        RefineryUtilities.centerFrameOnScreen(dw3);
		dw3.setVisible(true);
	}

}
//DrawComponent.java
package 传感器网络;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;

class DrawComponent extends JComponent{
    private static final int DEAFULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 400;
    public double[] x,y=new double[100];
    public double radius;
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        //绘制矩形
        double leftx = 100;
        double topy = 100;
        double width = 400;
        double height = 200;
        Rectangle2D rect = new Rectangle2D.Double(leftx,topy ,width,height);
        g2.draw(rect);

        //绘制椭圆
        //Ellipse2D ellipse = new Ellipse2D.Double();
       // ellipse.setFrame(rect);
        //g2.draw(ellipse);
        //绘制直线
        g2.draw(new Line2D.Double(leftx,topy+height,leftx,topy));
        g2.draw(new Line2D.Double(leftx,topy+height,leftx + width ,topy + height));
        //画正圆
        for(int i=0;i<100;i++)
        {
        double centerx = x[i];
        double centery = y[i];
        double radius = this.radius;
        Ellipse2D circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(centerx, centery,centerx+radius,centery+radius);
        g2.draw(circle);
        }
    }
}
//DrawFrame.java
package 传感器网络;

import javax.swing.JFrame;

public class DrawFrame extends JFrame {
	
	    public DrawFrame(double[] x,double[] y,double radius){
	    	DrawComponent c=new DrawComponent();
	        c.x=x;
	        c.y=y;
	        c.radius=radius;
	        add(new DrawComponent());
	        pack();
	    
	}

}
//Draw.java
package 传感器网络;
import org.jfree.chart.ChartFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.*;


public class Draw extends ApplicationFrame{

	String[] s=new String[51];
	static double radius;
	static long numbers;
	static double ds;
	static DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
	public Draw(String title,DefaultCategoryDataset dataset) {
		super(title);
		JFreeChart chart = createChart(dataset);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(chartPanel);
		// TODO Auto-generated constructor stub
	}
public Draw(String title,int i)
{
	super(title);
}
/**
*Creates a sample dataset.
*
*@return The dataset.
*/
public static  CategoryDataset createDataset(double radius,int ds) 
{ 
Double d=radius;
String s=String.valueOf(d);
dataset.addValue(ds,"n", s);

return dataset;
}
/**
*Creates a sample chart.
*
*@param dataset a dataset.
*
*@return The chart.
*/
@SuppressWarnings("deprecation")
public static JFreeChart createChart(CategoryDataset dataset) {

// create the chart...
JFreeChart chart = ChartFactory.createLineChart( "Connectivity in G(n,R)",	// chart title
"Radius(m)",	// domain axis label
"Pr[network is connected]",	// range axis label
dataset,	// data
PlotOrientation.VERTICAL,// orientation ,	// include legend
true,
true,	// tooltips
false	// urls
);

chart.addSubtitle(new TextTitle("Connectivity in G(n,R)"));
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.WHITE); 
plot.setRangeGridlinePaint(Color.DARK_GRAY);

// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

// customise the renderer... LineAndShapeRenderer renderer
LineAndShapeRenderer renderer= (LineAndShapeRenderer) plot.getRenderer(); 
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true); 
renderer.setFillPaint(Color.white);

return chart;
}
/**
*Creates a panel for the demo (used by SuperDemo.java).
*
*@return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(dataset);



return new ChartPanel(chart);
}

}
//Draw2.java
package 传感器网络;


import org.jfree.chart.ChartFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.*;


public class Draw2 extends ApplicationFrame{

	String[] s=new String[51];
	static double radius;
	static long numbers;
	static int ds;
	static DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
	public Draw2(String title,DefaultCategoryDataset dataset) {
		super(title);
		JFreeChart chart = createChart(dataset);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(chartPanel);
		// TODO Auto-generated constructor stub
	}
public Draw2(String title,int i)
{
	super(title);
}
/**
*Creates a sample dataset.
*
*@return The dataset.
*/
public static  CategoryDataset createDataset(double radius,int ds) 
{ 
Double d=radius;
String s=String.valueOf(d);
dataset.addValue(ds,"n", s);

return dataset;
}
/**
*Creates a sample chart.
*
*@param dataset a dataset.
*
*@return The chart.
*/
@SuppressWarnings("deprecation")
public static JFreeChart createChart(CategoryDataset dataset) {

// create the chart...
JFreeChart chart = ChartFactory.createLineChart( "Connectivity in G(n,R)",	// chart title
"Number of nodes",	// domain axis label
"Pr[network is connected]",	// range axis label
dataset,	// data
PlotOrientation.VERTICAL,// orientation ,	// include legend
true,
true,	// tooltips
false	// urls
);

chart.addSubtitle(new TextTitle("Connectivity in G(n,R)"));
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.WHITE); 
plot.setRangeGridlinePaint(Color.DARK_GRAY);

// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

// customise the renderer... LineAndShapeRenderer renderer
LineAndShapeRenderer renderer= (LineAndShapeRenderer) plot.getRenderer(); 
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true); 
renderer.setFillPaint(Color.white);

return chart;
}
/**
*Creates a panel for the demo (used by SuperDemo.java).
*
*@return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(dataset);



return new ChartPanel(chart);
}

}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值