潘志宝
2025-02-20 fdcde13df2173fdbb0a2e9e942c2400ec2006c3c
预测,模拟调整 predictByModel
已修改6个文件
163 ■■■■■ 文件已修改
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModelHandler.java 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java 97 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleConstructor.java 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/dto/SampleInfo.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-model/iailab-module-model-biz/src/main/resources/application-dev.yaml 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModelHandler.java
@@ -23,4 +23,16 @@
     * @throws ModelInvokeException
     */
    PredictResultVO predictByModel(Date predictTime, MmPredictModelEntity predictModel,String itemName,String itemNo) throws ModelInvokeException;
    /**
     * 预测,模拟调整
     *
     * @param predictTime
     * @param predictModel
     * @param itemName
     * @param itemNo
     * @param deviation
     * @return
     */
    PredictResultVO predictByModel(Date predictTime, MmPredictModelEntity predictModel,String itemName,String itemNo, double[][] deviation) throws ModelInvokeException;
}
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java
@@ -151,6 +151,103 @@
    }
    /**
     * 预测,模拟调整
     *
     * @param predictTime
     * @param predictModel
     * @param itemName
     * @param itemNo
     * @return
     * @throws ModelInvokeException
     */
    @Override
    public synchronized PredictResultVO predictByModel(Date predictTime, MmPredictModelEntity predictModel,String itemName,String itemNo, double[][] deviation) throws ModelInvokeException {
        PredictResultVO result = new PredictResultVO();
        if (predictModel == null) {
            throw new ModelInvokeException("modelEntity is null");
        }
        String modelId = predictModel.getId();
        try {
            List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Predict.name(), modelId, predictTime, itemName, new HashMap<>());
            String modelPath = predictModel.getModelpath();
            if (modelPath == null) {
                log.info("模型路径不存在,modelId=" + modelId);
                return null;
            }
            IAILModel newModelBean = composeNewModelBean(predictModel);
            HashMap<String, Object> settings = getPredictSettingsByModelId(modelId);
            // 校验setting必须有pyFile,否则可能导致程序崩溃
            if (!settings.containsKey(MdkConstant.PY_FILE_KEY)) {
                throw new RuntimeException("模型设置参数缺少必要信息【" + MdkConstant.PY_FILE_KEY +  "】,请重新上传模型!");
            }
            if (settings == null) {
                log.error("模型setting不存在,modelId=" + modelId);
                return null;
            }
            int portLength = sampleDataList.size();
            Object[] param2Values = new Object[portLength + 2];
            for (int i = 0; i < portLength; i++) {
                param2Values[i] = sampleDataList.get(i).getMatrix();
            }
            param2Values[portLength] = newModelBean.getDataMap().get("models");
            param2Values[portLength + 1] = settings;
            log.info("####################### 模拟调整 "+ "【itemId:" + predictModel.getItemid() + ",itemName:" + itemName + ",itemNo:" + itemNo + "】 ##########################");
            log.info("参数: " + JSON.toJSONString(param2Values));
            //IAILMDK.run
            HashMap<String, Object> modelResult = DllUtils.run(newModelBean, param2Values, predictModel.getMpkprojectid());
            //打印结果
            log.info("预测模型计算完成:modelId=" + modelId + ",modelName=" + predictModel.getMethodname() + ",modelResult=" + JSON.toJSONString(modelResult));
            //判断模型结果
            if (!modelResult.containsKey(CommonConstant.MDK_STATUS_CODE) || !modelResult.containsKey(CommonConstant.MDK_RESULT) ||
                    !modelResult.get(CommonConstant.MDK_STATUS_CODE).toString().equals(CommonConstant.MDK_STATUS_100)) {
                throw new ModelResultErrorException("模型结果异常:" + modelResult);
            }
            modelResult = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT);
            List<MmItemOutputEntity> itemOutputList = mmItemOutputService.getByItemid(predictModel.getItemid());
            Map<MmItemOutputEntity, double[]> predictMatrixs = new HashMap<>();
            for (MmItemOutputEntity output : itemOutputList) {
                if (!modelResult.containsKey(output.getResultstr())) {
                    continue;
                }
                OutResultType outResultType = OutResultType.getEumByCode(output.getResultType());
                switch (outResultType) {
                    case D1:
                        double[] temp1 = (double[]) modelResult.get(output.getResultstr());
                        predictMatrixs.put(output, temp1);
                        break;
                    case D2:
                        double[][] temp2 = (double[][]) modelResult.get(output.getResultstr());
                        double[] tempColumn = new double[temp2.length];
                        for (int i = 0; i < tempColumn.length; i++) {
                            tempColumn[i] = temp2[i][output.getResultIndex()];
                        }
                        predictMatrixs.put(output, tempColumn);
                        break;
                    case D:
                        Double temp3 = (Double) modelResult.get(output.getResultstr());
                        predictMatrixs.put(output, new double[]{temp3});
                        break;
                    default:
                        break;
                }
            }
            result.setPredictMatrixs(predictMatrixs);
            result.setModelResult(modelResult);
            result.setPredictTime(predictTime);
        } catch (ModelResultErrorException ex) {
            log.error("模型结果异常", ex);
            throw ex;
        } catch (Exception ex) {
            log.error("调用发生异常,异常信息为:{0}", ex.getMessage());
            throw new ModelInvokeException(ex.getMessage());
        }
        return result;
    }
    /**
     * 构造IAILMDK.run()方法的newModelBean参数
     *
     * @param predictModel
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java
@@ -30,6 +30,7 @@
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@@ -76,6 +77,8 @@
        Map<String, ApiPointDTO> pointMap = sampleInfo.getPointMap();
        Map<String, ApiPlanItemDTO> planMap = sampleInfo.getPlanMap();
        Map<String, ApiIndItemDTO> indMap = sampleInfo.getIndMap();
        int deviationIndex = 0;
        //对每个爪分别进行计算
        for (ColumnItemPort entry : sampleInfo.getColumnInfo()) {
            //先依据爪内数据项的modelParamOrder进行排序——重写comparator匿名函数
@@ -94,11 +97,31 @@
                }
            }
            //找出对应的调整值
            double[] deviationItem = null;
            if (sampleInfo.getDeviation() != null && sampleInfo.getDeviation().length > 0) {
                deviationItem = sampleInfo.getDeviation()[deviationIndex];
            }
            deviationIndex ++;
            //对每一项依次进行数据查询,然后将查询出的值赋给matrix对应的位置
            for (int i = 0; i < entry.getColumnItemList().size(); i++) {
                try {
                    List<DataValueVO> dataEntityList = getData(entry.getColumnItemList().get(i), pointMap, planMap,indMap);
                    //补全数据
                    //设置调整值
                    if (deviationItem != null && deviationItem.length > 0) {
                        logger.info("设置调整值, i = " + i);
                        if (deviationItem[i] <= 0) {
                            continue;
                        }
                        for(int dataKey = 1; dataKey < dataEntityList.size(); dataKey ++) {
                            DataValueVO item = dataEntityList.get(dataKey);
                            item.setDataValue(item.getDataValue() + deviationItem[i]);
                        }
                    }
                    // 补全数据
                    ColumnItem columnItem = entry.getColumnItemList().get(i);
                    dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, columnItem.getParamType(),columnItem.getGranularity());
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleConstructor.java
@@ -44,4 +44,22 @@
    }
    public List<SampleData> constructSample(String typeA, String modelId, Date runTime,String itemName,
                                            Map<Integer, Integer> dynamicDataLength, double[][] deviation) throws ModelInvokeException {
        try {
            SampleInfoConstructor sampleInfoConstructor = sampleFactory.createSampleInfo(typeA, modelId);
            SampleInfo sampleInfo = sampleInfoConstructor.prepareSampleInfo(modelId, runTime, dynamicDataLength);
            sampleInfo.setDeviation(deviation);
            SampleDataConstructor sampleDataConstructor = sampleFactory.createSampleData(typeA);
            return sampleDataConstructor.prepareSampleData(sampleInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取模型的算法参数异常",e);
            throw new ModelInvokeException(MessageFormat.format("{0},Name:{1}",
                    ModelInvokeException.errorGetModelArithParam, itemName));
        }
    }
}
iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/dto/SampleInfo.java
@@ -33,7 +33,8 @@
    private Integer sampleCycle;
    private BigDecimal[][] deviation;
    // 调整值
    private double[][] deviation;
    // 所有测点信息,避免重复查询
    private Map<String, ApiPointDTO> pointMap;
    // 所有计划数据信息,避免重复查询
iailab-module-model/iailab-module-model-biz/src/main/resources/application-dev.yaml
@@ -55,7 +55,7 @@
influx-db:
  org: iailab
  token: NloIinwybvMwKlJ8SGOAqboXH72EhdQEsnnV7kwtstVu6sbt24LNJ0bVICepeAtl2pxpd1Hj8gDLj9m4hnB7Fw==
  url: http://127.0.0.1:8086
  username: dzd
  password: qwer1234
  token: _338h4Kbu2KQaes5QwAyOz9pTUueXoSF9XmPi8N9oTS1SrhTZVj4J9JfSraUyWA0PfWMZOlf9QWax-USkJQR_A==
  url: http://172.16.8.200:8086
  username: iailab
  password: iailab2019