ソースを参照

排程优化-退火合并优化

fangpy 7 ヶ月 前
コミット
1b8c08a20e

+ 3 - 0
rw-aps-server/src/main/java/com/rongwei/rwapsserver/aps/listener/TaskStartTimeListener.java

@@ -227,6 +227,9 @@ public class TaskStartTimeListener implements VariableListener<ApsSolution, Prod
                 preProcess = process.getPreviousProcesses().get(0);
                 if(process.getPreviousProcesses().size()>1){
                     for (ProductionProcesses previousProcess : process.getPreviousProcesses()) {
+                        if(previousProcess.getEndTime() == null){
+                            int a = 1;
+                        }
                         if(previousProcess.getEndTime().compareTo(preProcess.getEndTime())>0){
                             preProcess = previousProcess;
                         }

+ 2 - 1
rw-aps-server/src/main/java/com/rongwei/rwapsserver/aps/service/impl/ApsServiceImpl.java

@@ -328,7 +328,8 @@ public class ApsServiceImpl implements ApsService {
                 .withSolutionClass(ApsSolution.class)
                 .withEntityClasses(ProductionProcesses.class)
                 .withConstraintProviderClass(ApsConstraintProvider.class)
-                .withTerminationSpentLimit(Duration.ofSeconds(runPlanSeconds1))
+//                .withTerminationSpentLimit(Duration.ofSeconds(runPlanSeconds1))
+                .withTerminationConfig(new TerminationConfig().withUnimprovedSecondsSpentLimit(120L))
                 .withMoveThreadCount(cores)
         );
         Solver<ApsSolution> solver1 = solverFactory1.buildSolver();

+ 117 - 89
rw-aps-server/src/main/java/com/rongwei/rwapsserver/aps/service/impl/ProductionScheduleServiceImpl.java

@@ -189,6 +189,7 @@ public class ProductionScheduleServiceImpl implements ProductionScheduleService
                 }
             }
         }
+
         // 退火提前排序
         Map<String,List<String>> ordergrous = new HashMap<>();
         apsService.tuihuoApsSch(apsSolution,otherThproces,otherNotZzFirstProces,productionScheduleVo,ordergrous);
@@ -196,6 +197,7 @@ public class ProductionScheduleServiceImpl implements ProductionScheduleService
         /*if(1 == 1){
             return null;
         }*/
+
         // 去掉锁定工序
         List<ProductionProcesses> notLocks = new ArrayList<>();
         List<ProductionProcesses> hasLocks = new ArrayList<>();
@@ -222,96 +224,11 @@ public class ProductionScheduleServiceImpl implements ProductionScheduleService
         if(ordergrous != null && ordergrous.size()>0){
             for (CommonJavaBean cjb : orderMerges) {
                 List<String> v = cjb.getOrderids();
-                ApsSolution apsSolution1 = new ApsSolution();
-                apsSolution1.setStartTime(apsSolution.getStartTime());
-                List<ProductionProcesses> processesList1 = apsSolution.getProcessesList().stream().filter(m -> v.contains(m.getProduceOrder().get(0).getId())).collect(Collectors.toList());
-                apsSolution1.setProcessesList(processesList1);
-                apsSolution1.setEquipmentList(apsSolution.getEquipmentList());
-
-                runPlanSeconds = (apsSolution1.getProcessesList().size()/prospeed + 1)*60;
-                SolverFactory<ApsSolution> solverFactory = SolverFactory.create(new SolverConfig()
-                                .withEnvironmentMode(mode)
-                                .withSolutionClass(ApsSolution.class)
-                                .withEntityClasses(ProductionProcesses.class)
-                                .withConstraintProviderClass(ApsConstraintProvider.class)
-                                .withTerminationConfig(new TerminationConfig().withUnimprovedSecondsSpentLimit(120L))
-//                                .withTerminationSpentLimit(Duration.ofSeconds(runPlanSeconds))
-                                .withMoveThreadCount(cores)
-                );
-                Solver<ApsSolution> solver = solverFactory.buildSolver();
-                solver.addEventListener(new SolverEventListener<ApsSolution>() {
-                    public void bestSolutionChanged(BestSolutionChangedEvent<ApsSolution> event) {
-                        if(solver.isEveryProblemChangeProcessed()) {
-                            event.getNewBestSolution().setConstructionHeuristicEnd(true);
-                            if(event.getNewBestSolution().getStepBestProcessesList() == null){
-                                List<ProductionProcesses> processesList = new ArrayList<>();
-//                        List<ProductionProcesses> processesList = ObjectUtil.cloneByStream(event.getNewBestSolution().getProcessesList());
-                                for (ProductionProcesses productionProcesses : event.getNewBestSolution().getProcessesList()) {
-                                    ProductionProcesses newPro = new ProductionProcesses();
-                                    newPro.setId(productionProcesses.getId());
-                                    newPro.setEquipmentId(productionProcesses.getEquipmentId());
-                                    Equipment eq = new Equipment();
-                                    if(productionProcesses.getEquipment() != null){
-                                        eq.setId(productionProcesses.getEquipment().getId());
-                                    }
-                                    newPro.setEquipment(eq);
-                                    newPro.setProduceTime(productionProcesses.getProduceTime());
-                                    newPro.setStartTime(productionProcesses.getStartTime());
-                                    newPro.setEndTime(productionProcesses.getEndTime());
-                                    newPro.setVolumeWidth(productionProcesses.getVolumeWidth());
-                                    newPro.setSinglerollweight(productionProcesses.getSinglerollweight());
-                                    processesList.add(newPro);
-
-                                    productionProcesses.setApsStatus("LS");
-                                }
-                                event.getNewBestSolution().setStepBestProcessesList(processesList);
-                            }
-                            System.out.println("************"+event.getNewBestScore()+"************");
-                            log.info("************"+event.getNewBestScore()+"************");
-
-                            // 终止排程
-//                    solver.terminateEarly();
-                        }
-                    }
-                });
-                SolutionManager<ApsSolution, HardSoftScore> scoreManager = SolutionManager.create(solverFactory);
-
-                ApsSolution solvedBalance1 = solver.solve(apsSolution1);
-                log.info("**************排程评分分析***************");
-                ScoreExplanation<ApsSolution, HardSoftScore> explain = scoreManager.explain(solvedBalance1);
-                log.info(explain.toString());
-                log.info("**************排程评分分析***************");
-                productionScheduleRetVo.setScoreResult(scoreManager.explain(solvedBalance1).toString());
-                // 得分分析
-                softExplain(explain,solvedBalance1.getProcessesList());
-
-                solvedBalance.getProcessesList().addAll(solvedBalance1.getProcessesList());
-                thOrderids.addAll(v);
-
-                // 排程结束的数据手动添加到设备占用列表中
-                setEqRunTimes(solvedBalance1.getProcessesList());
-            }
-        }
-        // 剩余数据按照坯料计划分组排程
-        List<ProductionProcesses> otherPros = apsSolution.getProcessesList().stream().filter(n -> !thOrderids.contains(n.getProduceOrder().get(0).getId())).collect(Collectors.toList());
-        if(otherPros != null && otherPros.size()>0){
-            Map<String, List<ProductionProcesses>> equass = new HashMap<>();
-            for (ProductionProcesses otherPro : otherPros) {
-                if(equass.containsKey(otherPro.getProduceOrder().get(0).getId())){
-                    equass.get(otherPro.getProduceOrder().get(0).getId()).add(otherPro);
-                }else {
-                    List<ProductionProcesses> p1 = new ArrayList<>();
-                    p1.add(otherPro);
-                    equass.put(otherPro.getProduceOrder().get(0).getId(),p1);
-                }
-            }
-            if(equass != null && equass.size()>0){
-                for(Map.Entry<String, List<ProductionProcesses>> entry : equass.entrySet()){
-                    String k = entry.getKey();
-                    List<ProductionProcesses> processesList1 = entry.getValue();
-
+                try{
+                    log.info("分批排程开始,当前批次坯料计划ID:"+v);
                     ApsSolution apsSolution1 = new ApsSolution();
                     apsSolution1.setStartTime(apsSolution.getStartTime());
+                    List<ProductionProcesses> processesList1 = apsSolution.getProcessesList().stream().filter(m -> v.contains(m.getProduceOrder().get(0).getId())).collect(Collectors.toList());
                     apsSolution1.setProcessesList(processesList1);
                     apsSolution1.setEquipmentList(apsSolution.getEquipmentList());
 
@@ -322,7 +239,7 @@ public class ProductionScheduleServiceImpl implements ProductionScheduleService
                                     .withEntityClasses(ProductionProcesses.class)
                                     .withConstraintProviderClass(ApsConstraintProvider.class)
                                     .withTerminationConfig(new TerminationConfig().withUnimprovedSecondsSpentLimit(120L))
-//                                    .withTerminationSpentLimit(Duration.ofSeconds(runPlanSeconds))
+//                                .withTerminationSpentLimit(Duration.ofSeconds(runPlanSeconds))
                                     .withMoveThreadCount(cores)
                     );
                     Solver<ApsSolution> solver = solverFactory.buildSolver();
@@ -373,9 +290,114 @@ public class ProductionScheduleServiceImpl implements ProductionScheduleService
                     softExplain(explain,solvedBalance1.getProcessesList());
 
                     solvedBalance.getProcessesList().addAll(solvedBalance1.getProcessesList());
+                    thOrderids.addAll(v);
 
                     // 排程结束的数据手动添加到设备占用列表中
                     setEqRunTimes(solvedBalance1.getProcessesList());
+                }catch (Exception e){
+                    if(productionScheduleRetVo.getMsg() == null){
+                        productionScheduleRetVo.setMsg("排程异常,");
+                    }
+                    productionScheduleRetVo.setMsg(productionScheduleRetVo.getMsg()+"坯料计划ID:"+v);
+                    e.printStackTrace();
+                    log.error("分批排程异常,当前批次坯料计划ID:"+v,e);
+                }
+            }
+        }
+        // 剩余数据按照坯料计划分组排程
+        List<ProductionProcesses> otherPros = apsSolution.getProcessesList().stream().filter(n -> !thOrderids.contains(n.getProduceOrder().get(0).getId())).collect(Collectors.toList());
+        if(otherPros != null && otherPros.size()>0){
+            Map<String, List<ProductionProcesses>> equass = new HashMap<>();
+            for (ProductionProcesses otherPro : otherPros) {
+                if(equass.containsKey(otherPro.getProduceOrder().get(0).getId())){
+                    equass.get(otherPro.getProduceOrder().get(0).getId()).add(otherPro);
+                }else {
+                    List<ProductionProcesses> p1 = new ArrayList<>();
+                    p1.add(otherPro);
+                    equass.put(otherPro.getProduceOrder().get(0).getId(),p1);
+                }
+            }
+            if(equass != null && equass.size()>0){
+                for(Map.Entry<String, List<ProductionProcesses>> entry : equass.entrySet()){
+                    String k = entry.getKey();
+                    try{
+                        log.info("剩余分批排程开始,当前批次坯料计划ID:"+k);
+                        List<ProductionProcesses> processesList1 = entry.getValue();
+
+                        ApsSolution apsSolution1 = new ApsSolution();
+                        apsSolution1.setStartTime(apsSolution.getStartTime());
+                        apsSolution1.setProcessesList(processesList1);
+                        apsSolution1.setEquipmentList(apsSolution.getEquipmentList());
+
+                        runPlanSeconds = (apsSolution1.getProcessesList().size()/prospeed + 1)*60;
+                        SolverFactory<ApsSolution> solverFactory = SolverFactory.create(new SolverConfig()
+                                        .withEnvironmentMode(mode)
+                                        .withSolutionClass(ApsSolution.class)
+                                        .withEntityClasses(ProductionProcesses.class)
+                                        .withConstraintProviderClass(ApsConstraintProvider.class)
+                                        .withTerminationConfig(new TerminationConfig().withUnimprovedSecondsSpentLimit(120L))
+//                                    .withTerminationSpentLimit(Duration.ofSeconds(runPlanSeconds))
+                                        .withMoveThreadCount(cores)
+                        );
+                        Solver<ApsSolution> solver = solverFactory.buildSolver();
+                        solver.addEventListener(new SolverEventListener<ApsSolution>() {
+                            public void bestSolutionChanged(BestSolutionChangedEvent<ApsSolution> event) {
+                                if(solver.isEveryProblemChangeProcessed()) {
+                                    event.getNewBestSolution().setConstructionHeuristicEnd(true);
+                                    if(event.getNewBestSolution().getStepBestProcessesList() == null){
+                                        List<ProductionProcesses> processesList = new ArrayList<>();
+//                        List<ProductionProcesses> processesList = ObjectUtil.cloneByStream(event.getNewBestSolution().getProcessesList());
+                                        for (ProductionProcesses productionProcesses : event.getNewBestSolution().getProcessesList()) {
+                                            ProductionProcesses newPro = new ProductionProcesses();
+                                            newPro.setId(productionProcesses.getId());
+                                            newPro.setEquipmentId(productionProcesses.getEquipmentId());
+                                            Equipment eq = new Equipment();
+                                            if(productionProcesses.getEquipment() != null){
+                                                eq.setId(productionProcesses.getEquipment().getId());
+                                            }
+                                            newPro.setEquipment(eq);
+                                            newPro.setProduceTime(productionProcesses.getProduceTime());
+                                            newPro.setStartTime(productionProcesses.getStartTime());
+                                            newPro.setEndTime(productionProcesses.getEndTime());
+                                            newPro.setVolumeWidth(productionProcesses.getVolumeWidth());
+                                            newPro.setSinglerollweight(productionProcesses.getSinglerollweight());
+                                            processesList.add(newPro);
+
+                                            productionProcesses.setApsStatus("LS");
+                                        }
+                                        event.getNewBestSolution().setStepBestProcessesList(processesList);
+                                    }
+                                    System.out.println("************"+event.getNewBestScore()+"************");
+                                    log.info("************"+event.getNewBestScore()+"************");
+
+                                    // 终止排程
+//                    solver.terminateEarly();
+                                }
+                            }
+                        });
+                        SolutionManager<ApsSolution, HardSoftScore> scoreManager = SolutionManager.create(solverFactory);
+
+                        ApsSolution solvedBalance1 = solver.solve(apsSolution1);
+                        log.info("**************排程评分分析***************");
+                        ScoreExplanation<ApsSolution, HardSoftScore> explain = scoreManager.explain(solvedBalance1);
+                        log.info(explain.toString());
+                        log.info("**************排程评分分析***************");
+                        productionScheduleRetVo.setScoreResult(scoreManager.explain(solvedBalance1).toString());
+                        // 得分分析
+                        softExplain(explain,solvedBalance1.getProcessesList());
+
+                        solvedBalance.getProcessesList().addAll(solvedBalance1.getProcessesList());
+
+                        // 排程结束的数据手动添加到设备占用列表中
+                        setEqRunTimes(solvedBalance1.getProcessesList());
+                    }catch (Exception e){
+                        if(productionScheduleRetVo.getMsg() == null){
+                            productionScheduleRetVo.setMsg("排程异常,");
+                        }
+                        productionScheduleRetVo.setMsg(productionScheduleRetVo.getMsg()+"坯料计划ID:"+k);
+                        e.printStackTrace();
+                        log.error("剩余分批排程开始,当前批次坯料计划ID:"+k,e);
+                    }
                 }
             }
         }
@@ -684,6 +706,9 @@ public class ProductionScheduleServiceImpl implements ProductionScheduleService
                 for (String orderid : cjb.getOrderids()) {
                     List<ProductionProcesses> processes = apsSolution.getProcessesList().stream().filter(v ->
                             v.getProduceOrder().get(0).getId().equals(orderid)).collect(Collectors.toList());
+                    if(processes == null || processes.size() == 0){
+                        continue;
+                    }
                     ProductionProcesses productionProcesses = processes.get(0);
                     if(maxVolumeWidth == null){
                         maxVolumeWidth = productionProcesses.getVolumeWidth();
@@ -702,6 +727,9 @@ public class ProductionScheduleServiceImpl implements ProductionScheduleService
                 for (String orderid : cjb.getOrderids()) {
                     List<ProductionProcesses> processes = apsSolution.getProcessesList().stream().filter(v ->
                             v.getProduceOrder().get(0).getId().equals(orderid)).collect(Collectors.toList());
+                    if(processes == null || processes.size() == 0){
+                        continue;
+                    }
                     ProductionProcesses productionProcesses = processes.get(0);
                     if(maxVolumeWidth == null){
                         maxVolumeWidth = productionProcesses.getVolumeWidth();

+ 6 - 2
rw-aps-server/src/test/java/com/rongwei/rwapsserver/RwApsServerApplicationTests.java

@@ -37,7 +37,7 @@ class RwApsServerApplicationTests {
         test1(3);
     }
 
-    private void test1(Integer a){
+    private void test1(Integer inta){
         /*Integer b = a;
         for (int i = 0; i < 2; i++) {
             b = a+1;
@@ -54,7 +54,11 @@ class RwApsServerApplicationTests {
         System.out.println(dateTime);*/
 
 //        System.out.println(new BigDecimal("20").divide(new BigDecimal("0.352"),0, BigDecimal.ROUND_HALF_UP).intValue());
-        System.out.println(new BigDecimal("15").divide(new BigDecimal("57"),2, BigDecimal.ROUND_HALF_UP).setScale(0, RoundingMode.CEILING).intValue());
+//        System.out.println(new BigDecimal("15").divide(new BigDecimal("57"),2, BigDecimal.ROUND_HALF_UP).setScale(0, RoundingMode.CEILING).intValue());
+
+        List<String> a = new ArrayList<>();
+        a.add("12");a.add("34");a.add("56");
+        System.out.println("测试数据:"+a);
     }
 
 }