package inport; import inport.ConversionUtils.*; import java.io.*; import java.util.ArrayList; public class Testing { public static Solver getTask_1_Solver() { return new Solver("conversion_1.mzn", Task::portToMiniZinc_1, MZnResultsResolver::resolveMiniZincResults); } public static Solver getTask_2_Solver() { return new Solver("conversion_2.mzn", Task::portToMiniZinc_2, MZnResultsResolver::resolveMiniZincResults); } public static Solver getTaskWithPartialCargoOp_Solver() { return new Solver("conversion_2_with_partial_cargo_operations.mzn", Task::portToMiniZinc_2, MZnResultsResolver::resolveMiniZincResults); } public static Solver getTaskWithGreedyConstraints_Solver() { return new Solver("conversion_2_greedy.mzn", Task::portToMiniZincGreedy, MZnResultsResolver::resolveMiniZincResults); } public static Solver getTaskWithGreedyConstraintsV2_Solver() { return new Solver("conversion_2_greedy_v2.mzn", Task::portToMiniZincGreedyV2, MZnResultsResolver::resolveMiniZincResults); } private static String testCase(String file, Solver solver) { TaskCase task = new TaskCase(); try { task.deserialize(file); } catch (IOException e) { return "Error : " + e.getMessage(); } double expected_result = task.getSolution_result(); solver.setTask(task); String error = solver.solve(); double result = task.getSolution_result(); if (!error.isEmpty()) { return "Error : " + error; } if (expected_result != result) { return "WA : expected " + expected_result + ", found " + result; } return "OK"; } public static void testGroup(String group, Solver solver, int timeLimitS) { testGroup("tests", group, solver, timeLimitS); } public static void testGroup(String topDir, String group, Solver solver, int timeLimitS) { File testDir = new File(topDir + "/" + group + "/"); System.out.println(testDir.toString() + " :"); solver.setTimeLimitS(timeLimitS); int nPassedTests = 0; for (File file : testDir.listFiles()) { System.out.println(" " + file.getName() + " : "); long start = System.currentTimeMillis(); String res = testCase(testDir.toString() + "/" + file.getName(), solver); long finish = System.currentTimeMillis(); System.out.println(" " + res + ", " + (finish - start) / 1000.0 + " s"); if (res.equals("OK")) { nPassedTests++; } } System.out.println("Passed tests : " + nPassedTests + "/" + testDir.listFiles().length); } public static void testWithPartialOp(int timeLimitS) { testGroup("with_typing", getTaskWithPartialCargoOp_Solver(), timeLimitS); } static void testingWithDiffParameters(String test, double cargoMult, double cargoMultMax, double nIntevals, double nIntervalsMax, double step, int timeLimitS) throws IOException { System.out.println(test); System.out.print("* "); for (double i = Math.ceil(nIntevals); i <= nIntervalsMax; i = Math.ceil(step * i)) { System.out.print(i + " "); } System.out.println(); File directory = new File("debug_dir/"); if (!directory.exists()) { directory.mkdir(); } for (double mult = Math.ceil(cargoMult); mult <= cargoMultMax; mult = Math.ceil(step * mult)) { System.out.print(mult + " "); for (double i = Math.ceil(nIntevals); i <= nIntervalsMax; i = Math.ceil(step * i)) { TaskCase taskCase = new TaskCase(); taskCase.deserialize(test); taskCase.setPlanningInterval(i); for (OperationTemplate template : taskCase.getTemplates()) { if (template instanceof LoadingTemplate) { LoadingTemplate op = (LoadingTemplate)template; op.setIntensity(op.getIntensity() * mult); } } for (TransportShip ship : taskCase.getShips()) { ArrayList> sections = new ArrayList<>(); for (Pair p : ship.getStorageSections()) { sections.add(new Pair<>(p.getKey(), p.getValue() * mult)); } ship.setStorageSections(sections); } for (StorageState state : taskCase.getStorageInitialState()) { state.setCargoState(state.getCargoState() * mult); } for (StorageState state : taskCase.getStorageEndState()) { state.setCargoState(state.getCargoState() * mult); } for (Storage storage : taskCase.getStorages()) { ArrayList> sections = new ArrayList<>(); for (Pair p : storage.getStorageSections()) { sections.add(new Pair<>(p.getKey(), p.getValue() * mult)); } storage.setStorageSections(sections); } for (Operation operation : taskCase.getSolution()) { final double m = mult; operation.setIntensity(operation.getIntensity().map(v -> (int)(v * m))); } long start = System.currentTimeMillis(); Solver s = getTaskWithPartialCargoOp_Solver(); s.setTimeLimitS(timeLimitS); s.setTempDir("temp_data"); String result = s.solve(); long finish = System.currentTimeMillis(); if (result.isEmpty()) { System.out.print((finish - start) / 1000.0 + " "); } else if (result.equals("Time limit exceeded.")) { System.out.print("TL "); } else { System.out.print("ER "); } } System.out.println(); } } }