| | |
| | | package com.zt.life.modules.mainPart.TestScheme.service; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.zt.common.service.BaseService; |
| | | import com.zt.life.modules.mainPart.TestScheme.dao.TestSchemeDao; |
| | | import com.zt.life.modules.mainPart.TestScheme.dto.ConditionDto; |
| | | import com.zt.life.modules.mainPart.TestScheme.dto.ResultDto; |
| | | import com.zt.life.modules.mainPart.TestScheme.model.TestScheme; |
| | | import org.apache.commons.math3.distribution.ChiSquaredDistribution; |
| | | import org.springframework.stereotype.Service; |
| | | import com.zt.common.db.query.QueryFilter; |
| | | |
| | |
| | | } |
| | | |
| | | public List<ResultDto> getResult(ConditionDto dto) { |
| | | this.insertNum(); |
| | | List<ResultDto> result = new ArrayList<>(); |
| | | List<TestScheme> list = baseDao.getList(); |
| | | if (dto.getTjlx().equals("按风险名义值查询")) { |
| | | result = this.getCondition1(dto, list); |
| | | } else if (dto.getTjlx().equals("不限定生产方风险查询")) { |
| | | result = this.getCondition2(dto, list); |
| | | } else if (dto.getTjlx().equals("不限定规定值查询")) { |
| | | result = this.getCondition3(dto, list); |
| | | switch (dto.getTjlx()) { |
| | | case "按风险名义值查询": |
| | | result = this.getCondition1(dto); |
| | | break; |
| | | case "不限定生产方风险查询": |
| | | result = this.getCondition2(dto); |
| | | break; |
| | | case "不限定检验上限查询": |
| | | result = this.getCondition3(dto); |
| | | break; |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private List<ResultDto> getCondition1(ConditionDto dto, List<TestScheme> list) { |
| | | List<ResultDto> result = new ArrayList<>(); |
| | | int C = 0; |
| | | double oldvalue = 0; |
| | | for (int i = 1; i <= 10; i++) { |
| | | Integer finalI = i; |
| | | TestScheme data1 = list.stream().filter(k -> k.getPa().equals(dto.getUserRisk()) && k.getPc() == (2 * finalI + 2)).findFirst().get();// |
| | | TestScheme data2 = list.stream().filter(k -> k.getPa().equals(BigDecimal.ONE.subtract(dto.getUserRisk())) && k.getPc() == (2 * finalI + 2)).findFirst().get();// |
| | | if (data1 != null && data2 != null) { |
| | | Double value = data2.getValue() / data1.getValue(); |
| | | double strate = dto.getMinAccepValue() / dto.getSpecifiedValue(); |
| | | private void insertNum() { |
| | | baseDao.insertNum(); |
| | | } |
| | | |
| | | if (value >= strate) { |
| | | C = i; |
| | | if (strate - oldvalue < value - strate) |
| | | C = i - 1; |
| | | break; |
| | | private List<ResultDto> getCondition1(ConditionDto dto) { |
| | | List<ResultDto> result = new ArrayList<>(); |
| | | int C = 1; |
| | | double oldvalue = 0; |
| | | double strate = dto.getMinAccepValue() / dto.getSpecifiedValue(); |
| | | for (int i = 1; i <= 10000; i++) { |
| | | Double data1 = getUpperQuantile(1 - dto.getProductionRisk(), 2 * i + 2); |
| | | Double data2 = getUpperQuantile(dto.getUserRisk(), 2 * i + 2);//算法计算 |
| | | if (data1 != null && data2 != null) { |
| | | double value = data1 / data2; |
| | | System.out.println(value); |
| | | if (dto.getUserRisk() < 1 - dto.getProductionRisk()) { |
| | | if (oldvalue != 0) { |
| | | if (value > strate) { |
| | | C = i; |
| | | if (strate - oldvalue < value - strate) { |
| | | C = i - 1; |
| | | } |
| | | break; |
| | | } |
| | | } else { |
| | | if (value > strate) { |
| | | C = i; |
| | | break; |
| | | } |
| | | } |
| | | } else { |
| | | if (oldvalue != 0) { |
| | | if (value < strate) { |
| | | C = i; |
| | | if (oldvalue - strate < strate - value) { |
| | | C = i - 1; |
| | | } |
| | | break; |
| | | } |
| | | } else { |
| | | if (value < strate) { |
| | | C = i; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | oldvalue = value; |
| | | } |
| | |
| | | double T = 0; //总试验时间 |
| | | double TST1 = 0; //T/θ1 |
| | | double TST0 = 0; //T/θ0 |
| | | Integer finalC = C; |
| | | Stream<TestScheme> testSchemeStream = list.stream().filter(k -> k.getPa().equals(dto.getUserRisk()) && k.getPc() == (2 * finalC + 2)); |
| | | TestScheme data3 = testSchemeStream.findFirst().get(); |
| | | Double data3 = getUpperQuantile(dto.getUserRisk(), 2 * C + 2); |
| | | if (data3 != null) { |
| | | TST1 = data3.getValue() / 2; //T/θ1 |
| | | TST1 = data3 / 2; //T/θ1 |
| | | //TST1 = Convert.ToDouble(TST1.ToString("G2")); //取两位有效数字 |
| | | if (TST1 < 10) { |
| | | TST1 = TST1 + 0.1; |
| | |
| | | double e = 2.71828182845904523536; //自然数e |
| | | |
| | | for (int r = 0; r <= C; r++) { |
| | | productionRisk = productionRisk + Math.pow(TST0, r) * Math.pow(e, -TST0) / getjc(r); |
| | | double a = Math.pow(TST0, r) * Math.pow(e, -TST0) / getjc(r); |
| | | /* if (r>35){ |
| | | System.out.println(111); |
| | | }*/ |
| | | productionRisk = productionRisk + a; |
| | | userRisk = userRisk + Math.pow(TST1, r) * Math.pow(e, -TST1) / getjc(r); |
| | | } |
| | | ResultDto resultDto = new ResultDto(); |
| | | productionRisk = 1 - productionRisk; |
| | | |
| | | ResultDto resultDto = new ResultDto(); |
| | | resultDto.setAcceptNumber(C); |
| | | resultDto.setTotalTestTime(String.valueOf(Math.round(T))); |
| | | resultDto.setProductionRiskReal(new Formatter().format("%.2f", productionRisk * 100).toString()); |
| | |
| | | return result; |
| | | } |
| | | |
| | | public List<ResultDto> getCondition2(ConditionDto dto, List<TestScheme> list) { |
| | | public List<ResultDto> getCondition2(ConditionDto dto) { |
| | | List<ResultDto> result = new ArrayList<>(); |
| | | for (Integer i = 0; i < dto.getShowFailureTime(); i++) { |
| | | Integer finalI = i; |
| | | List<TestScheme> data = list.stream().filter(k -> k.getPa().equals(dto.getUserRisk()) && k.getPc() == (2 * finalI + 2)).collect(Collectors.toList());// |
| | | ResultDto resultDto = new ResultDto(); |
| | | double T = 0; |
| | | double TST1 = 0; // T/θ1 |
| | | double TST0 = 0;// T/θ0 |
| | | for (TestScheme item : data) { |
| | | TST1 = item.getValue() / 2; |
| | | T = TST1 * dto.getMinAccepValue(); |
| | | TST0 = T / dto.getSpecifiedValue(); // T/θ0 |
| | | } |
| | | for (int i = 0; i < dto.getShowFailureTime(); i++) { |
| | | double T; |
| | | double TST1; // T/θ1 |
| | | double TST0;// T/θ0 |
| | | |
| | | Double data2 = getUpperQuantile(dto.getUserRisk(), 2 * i + 2); |
| | | TST1 = data2 / 2; |
| | | T = TST1 * dto.getMinAccepValue(); |
| | | TST0 = T / dto.getSpecifiedValue(); // T/θ0 |
| | | |
| | | |
| | | double productionRisk = 0; |
| | | double userRisk = 0; |
| | | double e = 2.71828182845904523536; |
| | |
| | | } |
| | | productionRisk = 1 - productionRisk; |
| | | |
| | | ResultDto resultDto = new ResultDto(); |
| | | resultDto.setAcceptNumber(i + 1); |
| | | resultDto.setTotalTestTime(String.valueOf(Math.round(T))); |
| | | resultDto.setProductionRiskReal(new Formatter().format("%.2f", productionRisk * 100).toString()); |
| | |
| | | return result; |
| | | } |
| | | |
| | | private List<ResultDto> getCondition3(ConditionDto dto, List<TestScheme> list) { |
| | | private List<ResultDto> getCondition3(ConditionDto dto) { |
| | | List<ResultDto> result = new ArrayList<>(); |
| | | for (int i = 0; i < dto.getShowFailureTime(); i++) { |
| | | Integer finalI = i; |
| | | List<TestScheme> data = list.stream().filter(k -> k.getPa().equals(dto.getUserRisk()) && k.getPc() == (2 * finalI + 2)).collect(Collectors.toList());// |
| | | List<TestScheme> data2 = list.stream().filter(k -> k.getPa().equals(BigDecimal.ONE.subtract(dto.getProductionRisk())) && k.getPc() == (2 * finalI + 2)).collect(Collectors.toList());// |
| | | double T = 0; |
| | | double TST1 = 0; // T/θ1 |
| | | double TST0 = 0;// T/θ0 |
| | | double ST0 = 0; // θ0 |
| | | for (TestScheme item : data) { |
| | | TST1 = item.getValue() / 2; |
| | | T = TST1 * dto.getMinAccepValue(); |
| | | for (TestScheme item2 : data2) { |
| | | ST0 = item.getValue() * dto.getMinAccepValue() / item2.getValue(); |
| | | } |
| | | TST0 = T / ST0; |
| | | } |
| | | double T; |
| | | double TST1; // T/θ1 |
| | | double TST0;// T/θ0 |
| | | double ST0; // θ0 |
| | | |
| | | Double data1 = getUpperQuantile(dto.getUserRisk(), 2 * i + 2); |
| | | Double data2 = getUpperQuantile(1 - dto.getProductionRisk(), 2 * i + 2); |
| | | TST1 = data1 / 2; |
| | | T = TST1 * dto.getMinAccepValue(); |
| | | ST0 = data1 * dto.getMinAccepValue() / data2; |
| | | TST0 = T / ST0; |
| | | |
| | | |
| | | double productionRisk = 0; |
| | | double userRisk = 0; |
| | |
| | | productionRisk = productionRisk + Math.pow(TST0, r) * Math.pow(e, -TST0) / getjc(r); |
| | | userRisk = userRisk + Math.pow(TST1, r) * Math.pow(e, -TST1) / getjc(r); |
| | | } |
| | | productionRisk = 1 - productionRisk; |
| | | |
| | | ResultDto resultDto = new ResultDto(); |
| | | resultDto.setAcceptNumber(i + 1); |
| | | resultDto.setTotalTestTime(String.valueOf(Math.round(T))); |
| | | resultDto.setNumber(String.valueOf(Math.round(ST0))); |
| | | resultDto.setSpecifiedValue(String.valueOf(Math.round(ST0)));//检验上限 |
| | | resultDto.setProductionRiskReal(new Formatter().format("%.2f", productionRisk * 100).toString()); |
| | | resultDto.setUserRiskReal(new Formatter().format("%.2f", userRisk * 100).toString()); |
| | | result.add(resultDto); |
| | |
| | | } |
| | | |
| | | //计算阶乘 |
| | | int getjc(int a) { |
| | | int result = 1; |
| | | Double getjc(int a) { |
| | | double result = 1.0; |
| | | for (int i = a; i > 0; i--) |
| | | result = result * i; |
| | | return result; |
| | | } |
| | | |
| | | //卡方分布的上侧α分位数算法 |
| | | public Double getUpperQuantile(Double alpha, Integer degreesOfFreedom) { |
| | | ChiSquaredDistribution chiSquaredDistribution = new ChiSquaredDistribution(degreesOfFreedom); |
| | | Double upperQuantile = chiSquaredDistribution.inverseCumulativeProbability(1 - alpha); |
| | | String str = String.format("%.4f", upperQuantile); |
| | | upperQuantile = Convert.toDouble(str); |
| | | return upperQuantile; |
| | | } |
| | | } |