From d5e9680fb31a68d6e056389edc9a0f30f7bf6e18 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Mon, 22 Jan 2024 14:26:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E6=BB=9A=E5=8E=9F=E7=85=A4=E9=85=8D?= =?UTF-8?q?=E7=85=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../coalBlend/entity/CoalBlendEntity.java | 569 +++++++++--------- .../coal/coalBlend/entity/CoalPercentVo.java | 10 + 3 files changed, 280 insertions(+), 301 deletions(-) diff --git a/pom.xml b/pom.xml index d74d2e44..d3548008 100644 --- a/pom.xml +++ b/pom.xml @@ -365,7 +365,7 @@ com.google.ortools ortools-java - 9.6.2534 + 9.8.3296 diff --git a/src/main/java/cn/lihongjie/coal/coalBlend/entity/CoalBlendEntity.java b/src/main/java/cn/lihongjie/coal/coalBlend/entity/CoalBlendEntity.java index 4efbfd6b..d34f2cb4 100644 --- a/src/main/java/cn/lihongjie/coal/coalBlend/entity/CoalBlendEntity.java +++ b/src/main/java/cn/lihongjie/coal/coalBlend/entity/CoalBlendEntity.java @@ -48,30 +48,30 @@ public class CoalBlendEntity extends OrgCommonEntity { @OneToMany(mappedBy = "coal", cascade = CascadeType.ALL, orphanRemoval = true) private List results; - @Comment("配煤类型") - private String blendType; - - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'coal.blendType'\n" - + " and i.code = blend_type)") - private String blendTypeName; - @Comment("煤类型") private String coalType; @Formula( "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'coalBlend.coalType'\n" - + " and i.code = coal_type)") + + "from t_dictionary d,\n" + + " t_dictionary_item i\n" + + "where d.id = i.dictionary_id\n" + + " and d.code = 'coalBlend.coalType'\n" + + " and i.code = coal_type)") private String coalTypeName; + @Comment("配煤类型") + private String blendType; + + @Formula( + "(select i.name\n" + + "from t_dictionary d,\n" + + " t_dictionary_item i\n" + + "where d.id = i.dictionary_id\n" + + " and d.code = 'coal.blendType'\n" + + " and i.code = blend_type)") + private String blendTypeName; + @Comment("结果个数") private Integer count = 20; @@ -294,83 +294,52 @@ public class CoalBlendEntity extends OrgCommonEntity { }); IntVar[] percentValArr = percentVal.toJavaArray(IntVar[]::new); - - boolean isJM = StringUtils.isEmpty(coalType) || StringUtils.equalsIgnoreCase(coalType, "0"); - boolean isYM = StringUtils.equalsIgnoreCase(coalType, "2"); - if (isJM) { - - // 各个煤累加和 100 - model.addEquality(LinearExpr.sum(percentValArr), 100); - } else { - if (isYM) { - // 原煤配煤比例小于等于 100 - model.addLessOrEqual(LinearExpr.sum(percentValArr), 100); - } - } + // 各个煤累加和 100 + model.addEquality(LinearExpr.sum(percentValArr), 100); addBaseConstrain(model, percentValArr); IntVar gcd = model.newIntVar(2, 100, "gcd"); Stream gcdVal = null; - if (isJM) { + if (Objects.equals(blendType, "2")) { - if (Objects.equals(blendType, "2")) { + gcdVal = + Stream.ofAll(coals) + .zipWithIndex( + (c, i) -> + model.newIntVar( + c.getMinPercent(), + c.getMaxPercent(), + "gcdX" + i)); + percentVal.zip(gcdVal).forEach(t -> model.addDivisionEquality(t._2, t._1, gcd)); + percentVal + .zip(gcdVal) + .forEach(t -> model.addModuloEquality(LinearExpr.constant(0), t._1, gcd)); - gcdVal = - Stream.ofAll(coals) - .zipWithIndex( - (c, i) -> - model.newIntVar( - c.getMinPercent(), - c.getMaxPercent(), - "gcdX" + i)); - percentVal.zip(gcdVal).forEach(t -> model.addDivisionEquality(t._2, t._1, gcd)); - percentVal - .zip(gcdVal) - .forEach(t -> model.addModuloEquality(LinearExpr.constant(0), t._1, gcd)); - - model.addLessOrEqual( - LinearExpr.sum(gcdVal.toJavaArray(IntVar[]::new)), type2PercentSum); - } - - if (StringUtils.isNotEmpty(targetParam)) { - if (targetOrder == -1) { - - model.maximize( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong(x -> (long) (x.getParam(targetParam) * 100)) - .toArray())); - - } else { - - model.minimize( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong(x -> (long) (x.getParam(targetParam) * 100)) - .toArray())); - } - } + model.addLessOrEqual( + LinearExpr.sum(gcdVal.toJavaArray(IntVar[]::new)), type2PercentSum); } - if (isYM){ + if (StringUtils.isNotEmpty(targetParam)) { + if (targetOrder == -1) { + model.maximize( + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong(x -> (long) (x.getParam(targetParam) * 100)) + .toArray())); - int coalSize = coals.size(); - long[] weightArr = new long[coalSize]; + } else { - for (int i = 0; i < coalSize; i++) { - - - weightArr[i] = (long) (Math.pow(100, coalSize - i)); - + model.minimize( + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong(x -> (long) (x.getParam(targetParam) * 100)) + .toArray())); } - - model.maximize(LinearExpr.weightedSum(percentValArr, weightArr)); } - stopWatch.stop(); stopWatch.start("CpSolver"); @@ -429,283 +398,283 @@ public class CoalBlendEntity extends OrgCommonEntity { if (param1.get()) vo.setParam1( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam1() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam1() + * 100)) + .toArray())) + / 10000.0); if (param2.get()) vo.setParam2( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam2() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam2() + * 100)) + .toArray())) + / 10000.0); if (param3.get()) vo.setParam3( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam3() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam3() + * 100)) + .toArray())) + / 10000.0); if (param4.get()) vo.setParam4( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam4() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam4() + * 100)) + .toArray())) + / 10000.0); if (param5.get()) vo.setParam5( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam5() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam5() + * 100)) + .toArray())) + / 10000.0); if (param6.get()) vo.setParam6( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam6() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam6() + * 100)) + .toArray())) + / 10000.0); if (param7.get()) vo.setParam7( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam7() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam7() + * 100)) + .toArray())) + / 10000.0); if (param8.get()) vo.setParam8( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam8() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam8() + * 100)) + .toArray())) + / 10000.0); if (param9.get()) vo.setParam9( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam9() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam9() + * 100)) + .toArray())) + / 10000.0); if (param10.get()) vo.setParam10( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam10() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam10() + * 100)) + .toArray())) + / 10000.0); if (param11.get()) vo.setParam11( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam11() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam11() + * 100)) + .toArray())) + / 10000.0); if (param12.get()) vo.setParam12( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam12() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam12() + * 100)) + .toArray())) + / 10000.0); if (param13.get()) vo.setParam13( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam13() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam13() + * 100)) + .toArray())) + / 10000.0); if (param14.get()) vo.setParam14( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam14() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam14() + * 100)) + .toArray())) + / 10000.0); if (param15.get()) vo.setParam15( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam15() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam15() + * 100)) + .toArray())) + / 10000.0); if (param16.get()) vo.setParam16( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam16() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam16() + * 100)) + .toArray())) + / 10000.0); if (param17.get()) vo.setParam17( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam17() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam17() + * 100)) + .toArray())) + / 10000.0); if (param18.get()) vo.setParam18( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam18() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam18() + * 100)) + .toArray())) + / 10000.0); if (param19.get()) vo.setParam19( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam19() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam19() + * 100)) + .toArray())) + / 10000.0); if (param20.get()) vo.setParam20( value( - LinearExpr.weightedSum( - percentValArr, - coals.stream() - .mapToLong( - x -> - (long) - (x - .getParam20() - * 100)) - .toArray())) - / 10000.0); + LinearExpr.weightedSum( + percentValArr, + coals.stream() + .mapToLong( + x -> + (long) + (x + .getParam20() + * 100)) + .toArray())) + / 10000.0); CoalBlendEntity.this.results.add(vo); diff --git a/src/main/java/cn/lihongjie/coal/coalBlend/entity/CoalPercentVo.java b/src/main/java/cn/lihongjie/coal/coalBlend/entity/CoalPercentVo.java index 643da31c..91a389eb 100644 --- a/src/main/java/cn/lihongjie/coal/coalBlend/entity/CoalPercentVo.java +++ b/src/main/java/cn/lihongjie/coal/coalBlend/entity/CoalPercentVo.java @@ -22,4 +22,14 @@ public class CoalPercentVo { @Comment("煤的铲车配煤比例") private Double type2Percent; + + + + @Comment("原煤比例") + private Double ymPercent; + public CoalPercentVo(String name, Double percent, Double type2Percent) { + this.name = name; + this.percent = percent; + this.type2Percent = type2Percent; + } }