高斯正态云模型

兵兵放大招让我们写云发生器,也是醉了。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

class GaussianModel{
    private double Ex;
    private double En;
    private double He;
    private int N;
    private double[] x = new double[20480];
    private double[] u = new double[20480];
    private double[] ctb = new double[20480];
    private double per067,per067_;
    private double per100,per100_;
    private double per200,per200_;
    private double per300,per300_;

    private Random random = new Random();
    public GaussianModel(double _Ex, double _En, double _He, int _N)
    {
        Ex = _Ex;
        En = _En;
        He = _He;
        N = _N;
        getCloudDrops();
    }
    final public void print_x_u()
    {
        System.out.println("\nData:");
        for (int i = 0 ; i < N ; ++i)
            System.out.println("("+x[i]+","+u[i]+")");
        System.out.println();
        return ;
    }

    private void getCloudDrops()
    {
        /*
         * 产生服从N(a,b)随机数
         * Math.sqrt(b)*random.nextGaussian()+a; 
        */
        for(int i = 0 ; i < N ; ++i)
        {
            double En_ = He*random.nextGaussian()+En;
            x[i] = En_*random.nextGaussian()+Ex;
            u[i] = Math.exp(-1*Math.pow(x[i]-Ex, 2.0)/(2*Math.pow(En_, 2.0)));
        }
    }

    public void get_Ctb() 
    {
        // ------------------ 贡献值算法
        // 知识表示中的不确定性P4;
        for(int i = 0 ; i < N ; ++i)
            ctb[i] = u[i] * x[i] * Math.sqrt(2 * Math.PI) * En;

        double sum = 0 ;
        double sum_ = 0 ;
        int count = 0 ;
        for (int i = 0 ; i < N ; ++i)
            sum += ctb[i];
//      骨干元素贡献值计算
        for(int i = 0 ; i < N ; ++i)
        {
            if(x[i] >= Ex - 0.67*En && x[i] <= Ex + 0.67*En)
            {
                sum_ += ctb[i];
                count ++ ;
            }
        }
        per067 = sum_/sum ;
        per067_ = ((double)count)/((double)N);
//      基本元素
        sum_ = 0 ;
        count = 0 ;
        for(int i = 0 ; i < N ; ++i)
        {
            if(x[i] >= Ex - En && x[i] <= Ex + En)
            {
                sum_ += ctb[i];
                count ++ ;
            }
        }
        per100 = sum_/sum;
        per100_ = ((double)count)/((double)N);
//      外围元素;
        sum_ = 0 ;
        count = 0 ;
        for(int i = 0 ; i < N ; ++i)
        {
            if(x[i] >= Ex - 2*En && x[i] <= Ex + 2*En)
            {
                sum_ += ctb[i];
                count ++ ;
            }
        }
        per200 = sum_/sum;
        per200_ = ((double)count)/((double)N);
//      弱外围元素;
        per300 = (sum - sum_)/sum;
        per300_ = ((double)(N-count))/((double)N);
        return ;
    }

    final public void print_Ctb()
    {
        System.out.println("骨干元素:[Ex-0.67En,Ex+0.67En]");
        System.out.println("样本比例:"+per067_*100+"%");
        System.out.println("贡献值:    "+per067*100+"%\n");

        System.out.println("基本元素:[Ex-En,Ex+En]");
        System.out.println("样本比例:"+per100_*100+"%");
        System.out.println("贡献值:    "+per100*100+"%\n");

        System.out.println("外围元素:[Ex-2En,Ex-En]+[Ex+En,Ex+2En]");
        System.out.println("样本比例:"+(per200_ - per100_)*100+"%");
        System.out.println("贡献值:    "+(per200 - per100)*100+"%\n");

        System.out.println("弱外围元素:[Ex-3En,Ex-2En]+[Ex+2En,Ex+3En]");
        System.out.println("样本比例:"+per300_*100+"%");
        System.out.println("贡献值:    "+per300*100+"%\n");
    }

    public void write_Excel(String _url) throws FileNotFoundException
    {
        /*
         * 数据写入到Excel中
         * */       
        String url = "E:/Code/JAVA_workspace/FieldCluster/src/";
        url += _url ;
        FileOutputStream out = new FileOutputStream(url);
        XSSFWorkbook wb = new XSSFWorkbook();

        //创建1页;
        XSSFSheet sheets = wb.createSheet("u(x)");
        for (int k = 0; k < N; k++) {
            XSSFRow rows = sheets.createRow(k);// 创建行数
            //写入单元格
            //int totalRows = sheets.getLastRowNum() + 1;
            rows.createCell(0).setCellValue(x[k]);
            rows.createCell(1).setCellValue(u[k]);
        }
        try {
            wb.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
           try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
           System.out.println("\n数据写入Excel成功!!!\n");
    }
}

public class CloudModel {
    private static double Ex;
    private static double En;
    private static double He;
    private static int N;
    private static Scanner cin = new Scanner(System.in);

    static class WriteExcel extends Thread{
        GaussianModel GM ;
        public void run()
        {
            try {
                GM.write_Excel("data.xlsx");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public void start(GaussianModel _GM)
        {
            super.start();
            GM = _GM;
        }
    }

    public static void main(String args[])
    {
        Ex = cin.nextDouble();
        En = cin.nextDouble();
        He = cin.nextDouble();
        N = cin.nextInt();
        GaussianModel GM = new GaussianModel(Ex,En,He,N);
        WriteExcel WE = new WriteExcel();
        WE.start(GM);
        GM.print_x_u();
        GM.get_Ctb();
        GM.print_Ctb();
    }
}

数据写入Excel结果:
输入Ex,En,He,N:
20
5
0.3
10240
在Excel中画出散点图如下:
这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值