/** * 专家打分 */ public double[][] addScore(double[][] score) { ExpertScore expertScore = new ExpertScore(); int rows = score.length;//行 int cols = score[0].length;//列 // 计算每列的元素和 double[] colSums = new double[cols]; for (int j = 0; j < cols; j++) { double sum = 0; for (int i = 0; i < rows; i++) { sum += score[i][j] /10; } colSums[j] = sum; } // 对矩阵进行列归一化操作 for (int j = 0; j < cols; j++) { for (int i = 0; i < rows; i++) { score[i][j] = (score[i][j]/10) /colSums[j]; } } return score; }
/** * 指标权重配置 */ @Override @Transactional(rollbackFor = Exception.class) public JSONObject create(JSONObject json) throws JsonProcessingException { WeightResult weightResult = new WeightResult(); // 创建 ObjectMapper 对象 ObjectMapper objectMapper = new ObjectMapper(); // 将 JSON 字符串解析为一个 JsonNode 对象 JsonNode jsonNode = objectMapper.readTree(String.valueOf(json)); // 通过 JsonNode 对象获取 score 数组和 weight 数组 JsonNode scoreNode = jsonNode.get("score"); JsonNode weightNode = jsonNode.get("weight"); // 将 score 数组和 weight 数组转换为 Java 数组 double[][] score = objectMapper.convertValue(scoreNode, double[][].class); double[][] weight = objectMapper.convertValue(weightNode, double[][].class); IndicatorWeight indicatorWeight = new IndicatorWeight(); double[][] matrix = weight; double[][] scores = score; int rows = matrix.length;//行 int cols = matrix[0].length;//列 // 计算每列的元素和 double[] colSums = new double[cols]; for (int j = 0; j < cols; j++) { double sum = 0; for (int i = 0; i < rows; i++) { sum += matrix[i][j]; } colSums[j] = sum; } // 对矩阵进行列归一化操作 for (int j = 0; j < cols; j++) { for (int i = 0; i < rows; i++) { matrix[i][j] /= colSums[j]; } } // 输出归一化后的矩阵 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } //将归一化后的列相加 double[] we = new double[cols]; for (int j = 0; j < cols; j++) { for (int i = 0; i < rows; i++) { we[j] += matrix[i][j]; } we[j] /= 6; //return we; System.out.println("we:" + we[j]); } indicatorWeightRepository.save(indicatorWeight); // 计算结果数组 double[] result = new double[scores.length]; for (int j = 0; j < scores.length; j++) { double sum = 0.0; for (int i = 0; i < scores[0].length; i++) { sum += (score[j][i] /10) * we[i]; } result[j] = sum; System.out.println("result:"+ result[j]); /* weightResult.setWDisPhen(result[j]); weightResult.setWMechanismProposed(result[j]); weightResult.setWVerifyTechnologies(result[j]); weightResult.setWSolveScientificProblems(result[j]); weightResult.setWOriginality(result[j]); weightResult.setWUrgency(result[j]); weightResultRepository.save(weightResult);*/ } JSONObject jsonObject = new JSONObject(); jsonObject.put("weight", we); jsonObject.put("score",result); return jsonObject; }