package cn.w.ahp;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.ujmp.core.DenseMatrix;
import org.ujmp.core.Matrix;
import org.ujmp.core.calculation.Calculation;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 层次分析法工具
*/
public class AHPUtil {
private static final double[] RI_STANDARD = {0, 0, 0.58, 0.90, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49, 1.51};
public static final String CR = "CR";
public static final String OPTIMAL_SOLUTION = "最优解";
public static final double CR_CRITICAL_VALUE = 0.1;
private AHPUtil() {
}
/**
* 矩阵列向量归一化
*
* @param matrix 矩阵
* @return org.ujmp.core.Matrix
*/
public static Matrix normalizedColumn(Matrix matrix) {
DenseMatrix normalize = Matrix.Factory.zeros(matrix.getRowCount(), matrix.getColumnCount());
long columnCount = matrix.getColumnCount();
long rowCount = matrix.getRowCount();
for (long i = 0; i < columnCount; i++) {
Matrix columns = matrix.selectColumns(Calculation.Ret.LINK, i);
double valueSum = columns.getAbsoluteValueSum();
for (long j = 0; j < rowCount; j++) {
Matrix rows = columns.selectRows(Calculation.Ret.LINK, j);
normalize.setAsObject(rows.getMaxValue() / valueSum, j, i);
}
}
return normalize;
}
public static AHPAnalysisResult calculateMatrix(double[][] matrixArray) {
return calculateMatrix(DenseMatrix.Factory.importFromArray(matrixArray));
}
public static AHPAnalysisResult calculateMatrix(Matrix matrix) {
AHPAnalysisResult result = new AHPAnalysisResult();
// 矩阵的特征向量与特征值 0为特征向量,1为特征值
Matrix[] eig = matrix.eig();
// 矩阵的最大特征值
Matrix eigenvalue = eig[1];
Double eigMax = eigenvalue.abs(Calculation.Ret.NEW).getMaxValue();
result.setMaxEigenvalue(eigMax);
long maxIndex;
long columnCount = eigenvalue.getColumnCount();
for (maxIndex = 0; maxIndex < columnCount; maxIndex++) {
Matrix colMatrix = eigenvalue.selectColumns(Calculation.Ret.LINK, maxIndex);
if (colMatrix.containsDouble(eigMax)) {
break;
}
}
// 矩阵的特征向量
Matrix eigenvector = eig[0];
// 最大特征值的特征向量归一化
Matrix colMatrix = eigenvector.selectColumns(Calculation.Ret.NEW, maxIndex);
result.setEigenvector(colMatrix.toColumnVector(Calculation.Ret.LINK).toDoubleArray()[0]);
Matrix normalizedColumn = AHPUtil.normalizedColumn(colMatrix);
result.setWeight(normalizedColumn.toColumnVector(Calculation.Ret.LINK).abs(Calculation.Ret.NEW).toDoubleArray()[0]);
// CI
double ci = (eigMax - columnCount) / (columnCount - 1);
ci = ci <= 0 ? 0 : ci;
result.setCi(new BigDecimal(ci + "").doubleValue());
// RI
double ri = RI_STANDARD[(int) (columnCount - 1)];
result.setRi(ri);
// CR
double cr = ri != 0 ? ci / ri : 0;
result.setCr(new BigDecimal(cr + "").doubleValue());
if (cr > CR_CRITICAL_VALUE) {
throw new RuntimeException("层次单排序一致性检验未通过");
}
return result;
}
public static Map<String, Object> calcLevelOrdering(AHPAnalysisResult parent, List<AHPAnalysisResult> childList) {
Map<String, Object> map = MapUtil.newHashMap();
String[] title = childList.get(0).getTitle();
double[] parentWeight = parent.getWeight();
double[][] childWeight = new double[title.length][parentWeight.length];
AtomicInteger index = new AtomicInteger(0);
childList.stream().map(AHPAnalysisResult::getWeight).forEach(w -> {
for (int i = 0; i < title.length; i++) {
childWeight[i][index.get()] = i < w.length ? w[i] : 0;
}
index.incrementAndGet();
});
double max = 0;
String maxTitle = "";
for (int i = 0; i < title.length; i++) {
String t = title[i];
double w = 0;
for (int j = 0; j < childWeight[i].length; j++) {
w += parentWeight[j] * childWeight[i][j];
}
max = Math.max(max, w);
maxTitle = w == max ? t : maxTitle;
map.put(t, w);
}
map.put(OPTIMAL_SOLUTION, maxTitle);
double ci = 0;
double ri = 0;
for (int i = 0; i < parentWeight.length; i++) {
ci += parentWeight[i] * childList.get(i).getCi();
ri += parentWeight[i] * childList.get(i).getRi();
}
double cr = (ri != 0 ? ci / ri : 0) + parent.getCr();
map.put(CR, cr);
if (cr > CR_CRITICAL_VALUE) {
throw new RuntimeException("层次总排序一致性检验未通过");
}
return map;
}
public static LevelCalcResult calcCriterionWeight(LevelCalcResult calcResult, MatrixMetaData metaData) {
LevelCalcResult newCalcResult = new LevelCalcResult();
double[] calcWeight = calcResult.getWeight();
double[] criterionCi = metaData.getCi();
double[] criterionRi = metaData.getRi();
double[][] criterionWeight = transpositionWeight(metaData.getWeight());
double[] newCalcWeight = new double[criterionWeight.length];
double ci = 0;
double ri = 0;
for (int i = 0; i < criterionWeight.length; i++) {
double[] colWeight = criterionWeight[i];
for (int j = 0; j < colWeight.length; j++) {
newCalcWeight[i] += colWeight[j] * calcWeight[j];
}
}
for (int i = 0; i < criterionCi.length; i++) {
ci += calcResult.getCi() * criterionCi[i];
ri += calcResult.getRi() * criterionRi[i];
}
newCalcResult.setCi(ci);
newCalcResult.setRi(ri);
newCalcResult.setCr((ri != 0 ? ci / ri : 0) + calcResult.getCr());
newCalcResult.setWeight(newCalcWeight);
return newCalcResult;
}
public static Map<String, Object> calcTotalOrdering(String[] measure, LevelCalcResult calcResult, MatrixMetaData metaData) {
Map<String, Object> map = MapUtil.newHashMap();
double[][] measureWeight = transpositionWeight(metaData.getWeight());
double[] calcWeight = calcResult.getWeight();
double max = 0;
String maxTitle = "";
for (int i = 0; i < measureWeight.length; i++) {
double[] colWeight = measureWeight[i];
double calcMeasureWeight = 0;
for (int j = 0; j < colWeight.length; j++) {
calcMeasureWeight += colWeight[j] * calcWeight[j];
}
max = Math.max(max, calcMeasureWeight);
maxTitle = calcMeasureWeight == max ? measure[i] : maxTitle;
map.put(measure[i], calcMeasureWeight);
}
map.put(OPTIMAL_SOLUTION, maxTitle);
double ci = 0;
double ri = 0;
double[] measureCi = metaData.getCi();
double[] measureRi = metaData.getRi();
for (int i = 0; i < measureCi.length; i++) {
ci += calcResult.getCi() * measureCi[i];
ri += calcResult.getRi() * measureRi[i];
}
double cr = (ri != 0 ? ci / ri : 0) + calcResult.getCr();
if (cr > CR_CRITICAL_VALUE) {
throw new RuntimeException("层次总排序一致性检验未通过");
}
map.put(CR, cr);
没有合适的资源?快使用搜索试试~ 我知道了~
数学建模-层次分析法Java实现
共14个文件
java:9个
json:4个
xml:1个
需积分: 5 0 下载量 28 浏览量
2024-09-10
15:36:28
上传
评论
收藏 34KB ZIP 举报
温馨提示
数学建模-层次分析法Java简单代码实现
资源推荐
资源详情
资源评论
收起资源包目录
AHPDemo.zip (14个子文件)
pom.xml 1KB
src
test
java
main
resources
cn
w
ahp
MatrixTest4.json 847B
MatrixTest3.json 827B
MatrixTest2.json 575B
MatrixTest1.json 1KB
java
cn
w
ahp
AHPUtil.java 10KB
LevelCalcResult.java 992B
AHPAnalysisResult.java 3KB
test
MatrixTest1.java 2KB
MatrixTest4.java 2KB
MatrixTest2.java 2KB
MatrixTest3.java 2KB
MatrixMetaData.java 563B
AHPObject.java 1KB
共 14 条
- 1
资源评论
wxl302947229
- 粉丝: 78
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Java语言的供应链管理系统配置设计源码
- 基于Java的快捷酒店POS系统设计源码
- 基于Unity 3D导出的layaair项目JS实现打地鼠资源动态加载设计源码
- hp1018\1022驱动
- 基于Vue框架开发的Java与JavaScript整合的homestay民宿网站设计源码
- 基于Python与HTML的selenium前端网页性能测试框架设计源码
- Rust 实现一个基本的 TCP/IP 协议栈,包括数据链路层、网络层和传输层
- 基于C++语言的Serving服务端框架设计源码
- WeChat-微信Hook,DaenWxHook+千寻VX框架,DaenWxHook源代码+千寻VX框架源代码,绝好学习资料!
- 基于Java开发的国际象棋棋盘游戏设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功