前几天看了看层次分析法,这是一个比较经典的算法,一般在评价和数据融合方面应用比较多,网上也有很多针对这个算法改进也是比较多的,大多数只是给这个方法加了点模糊运算。其实在现在的系统中单用它做评价或是评估的话,显得有点单调。但是单单应用它给出指标的权重,然后再融合和评价用用其它算法,如基于神经网络、基于证据理论、基于云模型、基于空间坐标等等算法,这样感觉上就比较完整。
下面就给出层次分析法的两个实现,其实这个算法主要是实现矩阵的最大特征值和对应的特征向量。高等代数与线性代数中都给出矩阵的特征值和特征向量的定义,但是用定义求对于阶数较高的时候,运算量较大。为此一般采用近视运算。常用的就是和法和幂法。幂法网上已经给出实现了,我就针对幂法的实现改编一下,给出和法的实现。
1.和法
import java.math.BigDecimal;
import java.util.Arrays;
public class AHPCompute2 {
/**
* @param args
*/
public static void main(String[] args) {
/** a为N*N矩阵 */
double[][] a = new double[][] { { 1, 1.8, 2.2, 1 }, { 0.6, 1, 3, 1.7 },
{ 0.4, 0.3, 1, 0.5 }, { 1, 0.5, 2, 1 } };
int N = a[0].length;
double[] weight = new double[N];
AHPCompute2 instance = AHPCompute2.getInstance();
instance.weight(a, weight, N);
System.out.println(Arrays.toString(weight));
}
// 单例
private static final AHPCompute2 acw = new AHPCompute2();
// 平均随机一致性指针
private double[] RI = { 0.00, 0.00, 0.58, 0.90, 1.12, 1.21, 1.32, 1.41,
1.45, 1.49 };
// 随机一致性比率
private double CR = 0.0;
// 最大特征值
private double lamta = 0.0;
/**
* 私有构造
*/
private AHPCompute2() {
}
/**
* 返回单例
*
* @return
*/
public static AHPCompute2 getInstance() {
return acw;
}
/**
* 计算权重
*
* @param a
* @param weight
* @param N
*/
public void weight(double[][] a, double[] weight, int N) {
double[] w1 = new double[N];
double[] w2 = new double[N];
double sum = 0.0;
//按行求和
for (int j = 0; j < N; j++) {
double t = 0.0;
for (int l = 0; l < N; l++)
t += a[l][j];
w1[j] = t;
}
//按行归一化,然后按列求和,最后得到特征向量w2
for (int k = 0; k < N; k++) {
sum = 0;
for (int i = 0; i < N; i++) {
sum = sum + a[k][i] / w1[i];
}
w2[k] = sum / N;
}
lamta = 0.0;
//求矩阵和特征向量的积,然后求出特征值
for (int k = 0; k < N; k++) {
sum = 0;
for (int i = 0; i < N; i++) {
sum = sum + a[k][i] * w2[i];
}
w1[k] = sum;
lamta = lamta + w1[k] / w2[k];
}
// 计算矩阵最大特征值lamta,CI,RI
lamta = lamta / N;
double CI = (lamta - N) / (N - 1);
if (RI[N - 1] != 0) {
CR = CI / RI[N - 1];
}
// 四舍五入处理
lamta = round(lamta, 3);
CI = round(CI, 3);
CR = round(CR, 3);
for (int i = 0; i < N; i++) {
w1[i] = round(w1[i], 4);
w2[i] = round(w2[i], 4);
}
// 控制台打印输出
System.out.println("lamta=" + lamta);
System.out.println("CI=" + CI);
System.out.println("CR=" + CR);
// 控制台打印权重
System.out.println("");
System.out.println("w1[]=");
for (int i = 0; i < N; i++) {
System.out.print(w1[i] + " ");
}
System.out.println("");
System.out.println("w2[]=");
for (int i = 0; i < N; i++) {
weight[i] = w2[i];
System.out.print(weight[i] + " ");
}
System.out.println("");
}
/**
* 四舍五入
*
* @param v
* @param scale
* @return
*/
public double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 返回随机一致性比率
*
* @return
*/
public double getCR() {
return CR;
}
}
2.幂法
public class AHPComputeWeight {
/**
* @param args
*/
public static void main(String[] args) {
/** a为N*N矩阵 */
double[][] a = new double[][] { { 1 ,1.8, 2.2, 1 },
{ 0.6, 1, 3, 1.7 },
{ 0.4 ,0.3, 1 ,0.5 }, { 1 ,0.5, 2, 1 }
};
int N = a[0].length;
double[] weight = new double[N];
AHPComputeWeight instance = AHPComputeWeight.getInstance();
instance.weight(a, weight, N);
System.out.println(Arrays.toString(weight));
}
// 单例
private static final AHPComputeWeight acw = new AHPComputeWeight();
// 平均随机一致性指针
private double[] RI = { 0.00, 0.00, 0.58, 0.90, 1.12, 1.21, 1.32, 1.41,
1.45, 1.49 };
// 随机一致性比率
private double CR = 0.0;
// 最大特征值
private double lamta = 0.0;
/**
* 私有构造
*/
private AHPComputeWeight() {
}
/**
* 返回单例
*
* @return
*/
public static AHPComputeWeight getInstance() {
return acw;
}
/**
* 计算权重
*
* @param a
* @param weight
* @param N
*/
public void weight(double[][] a, double[] weight, int N) {
// 初始向量Wk
double[] w0 = new double[N];
for (int i = 0; i < N; i++) {
w0[i] = 1.0 / N;
}
// 一般向量W(k+1)
double[] w1 = new double[N];
// W(k+1)的归一化向量
double[] w2 = new double[N];
double sum = 1.0;
double d = 1.0;
// 误差
double delt = 0.00001;
while (d > delt) {
d = 0.0;
sum = 0;
// 获取向量
//int index = 0;
for (int j = 0; j < N; j++) {
double t = 0.0;
for (int l = 0; l < N; l++)
t += a[j][l] * w0[l];
// w1[j] = a[j][0] * w0[0] + a[j][1] * w0[1] + a[j][2] * w0[2];
w1[j] = t;
sum += w1[j];
}
// 向量归一化
for (int k = 0; k < N; k++) {
w2[k] = w1[k] / sum;
// 最大差值
d = Math.max(Math.abs(w2[k] - w0[k]), d);
// 用于下次迭代使用
w0[k] = w2[k];
}
}
// 计算矩阵最大特征值lamta,CI,RI
lamta = 0.0;
for (int k = 0; k < N; k++) {
lamta += w1[k] / (N * w0[k]);
}
double CI = (lamta - N) / (N - 1);
if (RI[N - 1] != 0) {
CR = CI / RI[N - 1];
}
// 四舍五入处理
lamta = round(lamta, 3);
CI = round(CI, 3);
CR = round(CR, 3);
for (int i = 0; i < N; i++) {
w0[i] = round(w0[i], 4);
w1[i] = round(w1[i], 4);
w2[i] = round(w2[i], 4);
}
// 控制台打印输出
System.out.println("lamta=" + lamta);
System.out.println("CI=" + CI);
System.out.println("CR=" + CR);
// 控制台打印权重
System.out.println("w0[]=");
for (int i = 0; i < N; i++) {
System.out.print(w0[i] + " ");
}
System.out.println("");
System.out.println("w1[]=");
for (int i = 0; i < N; i++) {
System.out.print(w1[i] + " ");
}
System.out.println("");
System.out.println("w2[]=");
for (int i = 0; i < N; i++) {
weight[i] = w2[i];
System.out.print(w2[i] + " ");
}
System.out.println("");
}
/**
* 四舍五入
*
* @param v
* @param scale
* @return
*/
public double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 返回随机一致性比率
*
* @return
*/
public double getCR() {
return CR;
}
}