private static $_RI = array(
'1' => 0,
'2' => 0,
'3' => 0.51,
'4' => 0.89,
'5' => 1.12,
'6' => 1.25,
'7' => 1.35,
'8' => 1.42,
'9' => 1.46,
'10' => 1.49,
'11' => 1.52,
'12' => 1.54
);
/**
* 层次分析法
* @param $indexScore ---> 专家对每项指标所打的评分
* @return $guiyi ---> 各指标的权重
*/
public static function AHP($indexScore)
{
$arr_A = [];//矩阵A
$n = count($indexScore);
if ($n <= 1) {
return false;
}
foreach ($indexScore as $key => $val) {
$arr_A[$key] = [];
foreach ($indexScore as $v) {
$arr_A[$key][] = $val / $v;
}
}
$weight = [];
foreach ($arr_A as $val) {
$product = 1;
foreach ($val as $v) {
$product *= $v;
}
$weight[] = pow($product, 1 / $n);
}
$sum = array_sum($weight);
$guiyi = [];//特征向量 ---> 指标权重
if ($sum) {
foreach ($weight as $val) {
$guiyi[] = $val / $sum;
}
}
//$eigenvalue ---> 最大特征根
$eigenvalue = static::getEigenvalue($arr_A, $guiyi);
$_RI = static::$_RI[$n];
$_CI = ($eigenvalue - $n) / ($n - 1);
$_CR = $_CI / $_RI;
if ($_CR < 0.1) {
return $guiyi;
} else {
return '打分不合理';
}
}
/*求最大的特征值*/
public static function getEigenvalue($data1, $data2)
{
$eigenvalue = [];
foreach ($data1 as $key => $val) {
$sum = 0;
foreach ($val as $k => $v) {
$sum += $v * $data2[$k];
}
$eigenvalue[] = $sum;
}
return array_sum($eigenvalue);
}