📄 gridmodel.java
字号:
double maxVal = 0 ;
double[] minArray = null ;
double[] maxArray = null ;
double[] array = new double[maxLength] ;
for(int i = 0 ; i< firstArray.length ; i++){
array[i] = Math.abs(firstArray[i] - secondArray[i]);
}
minVal = min(array);
maxVal = max(array);
for(int i = 0 ; i < cc.length ; i ++ ){
cc[i] = (minVal + dstinguishC * maxVal)/(array[i] + dstinguishC * maxVal );
}
correlation = avg(cc);
return correlation ;
}
public double[] getCorrelation(double[][] matrix ){
/**
* 拿到灰色关联度:
* 输入:矩阵matrix[0,1,...,n-1][0,1,...,n-1]
* 输出:数组correlation[0,1,...,n-2]
*/
//拿到灰色关联度,第一行作为初始行,第二行到第n行为转变行,则有共有n-1个关联度
if (matrix == null) return null;
double[] correlation = new double[matrix.length - 1] ;
for(int i = 0 ; i < matrix.length - 1; i++){
correlation[i] = getCorrelation(matrix[0], matrix[i+1]);
}
return correlation ;
}
public double getAbsoluteCorrelation(double[] firstArray, double[] secondArray){
/**
* 拿到绝对灰色关联度:
* 输入:数组firstArray[0,1,...,n-1],数组secondArray[0,1,...,n-1]
* 输出:绝对关联度
*/
if (firstArray == null || secondArray == null || (firstArray.length != secondArray.length)) return -1 ;
double absCorrelation = 0 ;
double sum0 = 0 ;
double sum1 = 0 ;
for(int i = 1 ;i < firstArray.length - 1 ; i++){
sum0 += firstArray[i] - firstArray[0];
sum1 += secondArray[i] - secondArray[0];
}
sum0 += (firstArray[firstArray.length-1] -firstArray[0])/2;
sum1 += (secondArray[secondArray.length-1] -secondArray[0])/2;
sum0 = Math.abs(sum0);
sum1 = Math.abs(sum1);
absCorrelation = (1 + sum0 + sum1 )/(1 + sum0 + sum1 + Math.abs(sum1-sum0));
System.out.println(format(absCorrelation));
return format(absCorrelation) ;
}
public double[] getAbsoluteCorrelation(double[][] matrix ){
/**
* 拿到绝对灰色关联度:
* 输入:矩阵matrix[0,1,...,n-1][0,1,...,n-1]
* 输出:数组correlation[0,1,...,n-2]
*/
//拿到灰色关联度,第一行作为初始行,第二行到第n行为转变行,则有共有n-1个绝对关联度
if (matrix == null) return null;
double[] absCorrelation = new double[matrix.length - 1] ;
for(int i = 0 ; i < matrix.length - 1; i++){
absCorrelation[i] = getAbsoluteCorrelation(matrix[0], matrix[i+1]);
format(absCorrelation[i]);
}
return absCorrelation ;
}
public double getRelativeResidual(double[] firstArray, double[] secondArray ){
/**
* 拿到相对残差:
* 输入:数组firstArray[0,1,...,n-1],数组secondArray[0,1,...,n-1]
* 输出:相对残差
*/
if(firstArray.length != secondArray.length){
return -1 ;
}
double[] residual = new double[firstArray.length];
for(int i = 0 ; i< firstArray.length ; i++ ){
residual[i] = Math.abs((firstArray[i] - secondArray[i])/firstArray[i]);
}
System.out.println(format(avg(residual)));
return format(avg(residual));
}
public void show(double[] array){
/**
* 输出数组array[0,1,2,...n-1]:
* 输入:数组array[0,1,2,...n-1]
* 输出:数组array[0,1,2,...n-1]
*/
if( array==null || array.length == 0) return;
StringBuffer strbf = new StringBuffer("[ ");
for(int i = 0 ;i < array.length ; i ++){
strbf .append(format(array[i]));
if(i < array.length -1)
strbf .append(", ");
}
System.out.println(strbf.toString() + " ]");
}
public void show(double[][] matrix){
/**
* 输出矩阵matrix[0,1,2,...n-1][0,1,2,...n-1]:
* 输入:数组matrix[0,1,2,...n-1][0,1,2,...n-1]
* 输出:数组matrix[0,1,2,...n-1][0,1,2,...n-1]
*/
if(matrix==null || matrix.length ==0) return ;
for(int i = 0 ;i < matrix.length ; i ++){
for( int j = 0 ; j < matrix.length ; j++)
System.out.print(matrix[i][j] + " ");
System.out.println();
}
}
public double format(double number){
/**
* 小数点后保留三位小数:
* 输入:double类型数字number
* 输出:保留两位小数的double类型数字
*/
try{
number = (int)(number*1000)/1000.0;
}catch(Exception e){
System.out.println("Data Format Error.");
}
return number ;
}
public double[] getSimulatePrediction(double paramA, double paramB,double[] inputArray){
/**
* 模型计算值(包含初始序列的估计值):
* 输入:最小二乘法得到系数paramA,paramB,以及数组inputArray[]
* 输出:数组outputArray[0,1,...,n-1],outputArray对应原始序列模型计算值
*/
if(inputArray==null|| inputArray.length==0){
return null ;
}
double[] outputArray = new double[this.PREDICTIONNUMBER];
outputArray[0] = inputArray[0];
for(int i= 1 ; i < outputArray.length ; i++){
outputArray[i] = (inputArray[0] - paramB/paramA)*Math.exp(-paramA*i) + paramB/paramA;
}
return iagoSequence(outputArray);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
*GridModel_1_1 gmFunction = new GridModel_1_1();
* double[] inputs = new double[args.length] ;
* //从系统输入操作参数
* for( int i = 0 ;i < args.length ; i++){
* inputs[i] = Double.parseDouble(args[i]);
* }
*/
double[] inputs = new double[]{27260,29547,32411,35388};
GridModel gmFunction = new GridModel(inputs);
//存储累加序列
double[] agoSequences = null ;
//存储紧邻均值生成序列
double[] avgCloseSequences = null ;
//利用最小二乘法求系数a,b ;
double[] coefficents = null ;
//存储原始模型的估计值
double[] inputsModel = null;
//存储累减操作后得到的模型序列
double[] iagoSequences = null ;
//存储模型预测值
double[] predictionSequences = null ;
//存储相对误差平均值
double relativeResidual = 0 ;
//存储绝对关联度
double absoluteCorrelation = 0 ;
System.out.println("原始序列:");
gmFunction.show(inputs);
System.out.println("累加序列:");
agoSequences = gmFunction.agoSequence(inputs);
System.out.println("紧邻均值生成序列:");
avgCloseSequences = gmFunction.avgCloseDeal(agoSequences);
System.out.println("利用最小二乘法求出的系数a,b");
coefficents = gmFunction.leastSquareMethod(avgCloseSequences,inputs);
System.out.println("原始模型的估计值:");
inputsModel = gmFunction.getModel(coefficents[0],coefficents[1],inputs);
System.out.println("累减序列:");
iagoSequences = gmFunction.iagoSequence(inputsModel);
System.out.println("误差平均值:");
relativeResidual = gmFunction.getRelativeResidual(inputs,iagoSequences);
System.out.println("绝对关联度 :");
absoluteCorrelation = gmFunction.getAbsoluteCorrelation(inputs,iagoSequences);
//输出预测值,包括初始序列。
System.out.println("预测值 :");
if(relativeResidual < RELATIVERESIDUAL && absoluteCorrelation > ABSOLTETCORRELATION ){
predictionSequences = gmFunction.getSimulatePrediction(coefficents[0],coefficents[1],iagoSequences);
}else{
System.out.println("精度不符合要求,请重新建立模型。");
}
/**
* 以下代码用于与向"F:/result.txt"中输入模型运算结果
*/
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("F:/result.txt"));
StringBuffer strbf = new StringBuffer("经过灰色系统GM(1,1)模型建模,所得的最终预测结果为:" + "[");
for(int i = 0 ;i < predictionSequences.length ; i ++){
strbf .append(gmFunction.format(predictionSequences[i]));
if(i < predictionSequences.length -1)
strbf .append(", ");
}
strbf.append("]");
bw.append(strbf);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(bw != null){
bw.close();
bw = null ;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -