Commit ef1aa7f0 authored by Vladislav Kiselev's avatar Vladislav Kiselev

Добавлено автоматическое тестирование и новые тесты.

parent 873682ad
This diff is collapsed.
......@@ -2,19 +2,10 @@ package inport;
import java.io.*;
public class Main {
import static inport.Testing.solveTask_1;
import static inport.Testing.test_1;
private static void removeDirectory(File dir) {
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File aFile : files) {
removeDirectory(aFile);
}
}
}
dir.delete();
}
public class Main {
public static void main(String[] args) {
if (args.length == 0) {
......@@ -26,55 +17,26 @@ public class Main {
switch (type) {
case "solve" : {
String fileName = args[1];
File directory = new File("temp_data");
if (!directory.exists()) {
directory.mkdir();
}
String tempDir = "temp_data/";
String minizincData = tempDir + "minizinc_data.dzn";
String solverResults = tempDir + "solver_results.txt";
String constraints = tempDir + "constraints.mzn";
long start = System.currentTimeMillis();
TaskCase task = new TaskCase();
try {
try (FileWriter res = new FileWriter(constraints)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream("/constraints/conversion_1.mzn")));
String line;
while ((line = reader.readLine()) != null) {
res.write(line + "\n");
}
}
task.deserialize(fileName);
ConversionUtil.portToMiniZinc_1(task, minizincData);
long start = System.currentTimeMillis();
ProcessBuilder pb = new ProcessBuilder("minizinc",
"--solver", "Chuffed",
constraints, minizincData,
"-o", solverResults);
Process process = pb.start();
int exitCode = process.waitFor();
assert exitCode == 0;
} catch (IOException e) {
System.out.println(e.getMessage());
break;
}
long finish = System.currentTimeMillis();
String error = solveTask_1(task);
System.out.println((finish - start) + " milliseconds");
long finish = System.currentTimeMillis();
System.out.println((finish - start) + " milliseconds");
ConversionUtil.resolveMiniZincResults(task, solverResults);
if (!error.isEmpty()) {
System.out.println("Error : " + error);
} else {
task.serialize(fileName);
} catch (IOException | InterruptedException ex) {
System.out.println(ex.getMessage());
} catch (ParserException ex) {
if (ex.getMessage().equals("No solution.")) {
System.out.println(ex.getMessage());
} else {
System.out.println("ParserException : " + ex.getMessage());
}
}
removeDirectory(directory);
break;
}
case "to_MiniZinc_0" : {
......@@ -131,6 +93,9 @@ public class Main {
}
break;
}
case "testing" :
test_1();
break;
default:
System.out.println("Unknown type \"" + type + "\"");
}
......
......@@ -13,7 +13,7 @@ import java.util.OptionalInt;
public class MovingTemplate extends TowUsingTemplate {
private MovingObject mover;
private OptionalInt moverType;
private OptionalInt moverType = OptionalInt.empty();
private Berth destination;
......
package inport;
public class ParserException extends Exception {
public class ParserException extends RuntimeException {
ParserException(String mess) {
super(mess);
}
......
......@@ -626,31 +626,34 @@ public class TaskCase {
templates.add(mt);
m_template.put(mt.getId(), mt);
}
break;
}
break;
case Time_Windows:
break;
case Cargo_Flows: cargoFlows.add(new CargoFlow(strLine, m_storage, m_cargo));
break;
case Initial_Vessel_State: vesselInitialState.add(fromString(strLine, m_berth, m_vessel));
break;
break;
case Initial_Storage_State: storageInitialState.add(new StorageState(strLine, m_storage, m_vessel, m_cargo));
break;
case Final_Vessel_State: vesselEndState.add(fromString(strLine, m_berth, m_vessel));
break;
break;
case Final_Storage_State: storageEndState.add(new StorageState(strLine, m_storage, m_vessel, m_cargo));
break;
break;
case Task_Properties:
{
String[] rs = strLine.split(";");
planningInterval = Double.parseDouble(rs[0].trim());
criterionType = Integer.parseInt(rs[1].trim());
}
break;
break;
case Solution: // Тут чтение операций если надо. Потом подумаем
break;
if (numInside == 1) {
solution_result = Double.parseDouble(strLine.trim());
}
break;
default:
break;
break;
}
}
//Close the input stream
......
package inport;
import java.io.*;
import java.util.ArrayList;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
public class Testing {
private static void removeDirectory(File dir) {
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File aFile : files) {
removeDirectory(aFile);
}
}
}
dir.delete();
}
public static String solveTask_1(TaskCase task) {
return solveTask(
task,
"conversion_1.mzn",
ConversionUtil::portToMiniZinc_1,
ConversionUtil::resolveMiniZincResults);
}
/* Возвращает описание ошибки, если ошибки не было, то пустую строку. */
public static String solveTask(TaskCase task,
String constraintName,
BiConsumer<TaskCase, String> converterToMinizincFormat,
BiConsumer<TaskCase, String> interpreter) {
File directory = new File("temp_data");
if (!directory.exists()) {
directory.mkdir();
}
String tempDir = "temp_data/";
String minizincData = tempDir + "minizinc_data.dzn";
String solverResults = tempDir + "solver_results.txt";
String constraints = tempDir + "constraints.mzn";
try {
try (FileWriter res = new FileWriter(constraints)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream("/constraints/" + constraintName)));
String line;
while ((line = reader.readLine()) != null) {
res.write(line + "\n");
}
}
converterToMinizincFormat.accept(task, minizincData);
ProcessBuilder pb = new ProcessBuilder("minizinc",
"--solver", "Chuffed",
constraints, minizincData,
"-o", solverResults);
Process process = pb.start();
int exitCode = process.waitFor();
assert exitCode == 0;
BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = br.lines().collect(Collectors.joining("\n"));
if (output.trim().equals("=====UNSATISFIABLE=====")) {
task.setSolution(new ArrayList<>());
task.setSolution_result(-1);
} else {
interpreter.accept(task, solverResults);
}
} catch (UncheckedIOException | IOException | InterruptedException | ParserException ex) {
return ex.getMessage();
} finally {
removeDirectory(directory);
}
return "";
}
public static String testCase(String file) {
TaskCase task = new TaskCase();
try {
task.deserialize(file);
} catch (IOException e) {
return "Error : " + e.getMessage();
}
double expected_result = task.getSolution_result();
String error = solveTask_1(task);
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 test_1() {
File testDir = new File("tests/without_typing/");
System.out.println(testDir.toString() + " :");
int nPassedTests = 0;
for (File file : testDir.listFiles()) {
System.out.println(" " + file.getName() + " : ");
long start = System.currentTimeMillis();
String res = testCase(testDir.toString() + "/" + file.getName());
long finish = System.currentTimeMillis();
System.out.println(" " + res + ", " + (finish - start) + " ms");
if (res.equals("OK")) {
nPassedTests++;
}
}
System.out.println("Passed tests : " + nPassedTests + "/" + testDir.listFiles().length);
}
}
Cargoes
83;Груз1;0.0
84;Груз2;0.0
85;Груз3;0.0
Berths
86;Рейд
87;Терминал1
88;Терминал2
Storages
89;Хранилище1;83;1000.0
90;Хранилище2;84;1000.0
91;Хранилище3;85;1000.0
Bunkers
Tows
94;Буксир1;1000000
95;Буксир2;1000000
Loading Equipments
99;Плавучий кран 1
100;Плавучий кран 2
101;Плавучий кран 3
Transport Ships
96;Судно1;1000000.0
Templates
10;mrn;[];96;88;[94,95];1.0
13;mov;[];94;86;88;[];1.0
16;mov;[];94;87;88;[];3.0
15;mov;[];94;87;86;[];1.0
11;mov;[];94;88;86;[];1.0
12;mov;[];95;88;86;[];1.0
17;mov;[];95;87;88;[];3.0
14;mov;[];95;86;88;[];1.0
8;mov;[];96;86;88;[94];2.0
9;mov;[];96;86;88;[95];2.0
2;loa;[];96;83;89;88;[99,100];20.0;U
4;loa;[];96;84;90;88;[99,101];10.0;U
5;loa;[];96;84;90;88;[100,101];10.0;U
7;loa;[];96;85;91;88;[101];10.0;U
Cargo Flows
Initial Vessel State
96;86
99;88
100;88
101;88
94;87
95;88
Initial Storage State
83;89;0.0
84;90;0.0
85;91;0.0
83;96;60.0
84;96;60.0
85;96;60.0
Final Vessel State
Final Storage State
83;96;0.0
84;96;0.0
85;96;0.0
Task Properties
20.0;0
Solution
15.0
15; R; 0.0; 1.0
8; R; 1.0; 2.0
12; R; 2.0; 1.0
14; R; 3.0; 1.0
2; R; 3.0; 2.0
7; R; 3.0; 5.0
4; R; 8.0; 1.0
12; R; 9.0; 1.0
2; R; 9.0; 1.0
7; R; 9.0; 1.0
11; R; 10.0; 1.0
4; R; 10.0; 1.0
13; R; 11.0; 1.0
5; R; 11.0; 1.0
14; R; 12.0; 1.0
4; R; 12.0; 3.0
11; R; 14.0; 1.0
Cargoes
10; Груз1
Berths
6; Рейд
7; Терминал1
8; Терминал2
Storages
9; Хранилище1; 10; 5000
Transport Ships
12; Судно1; 1000
13; Судно2; 1000
Loading Equipments
Templates
4367; mov; []; 12; 6; 7; []; 1
4374; mov; []; 13; 8; 7; []; 1
4369; mov; []; 12; 6; 8; []; 1
4372; mov; []; 13; 7; 8; []; 1
4370; mov; []; 13; 6; 8; []; 1
4371; mov; []; 12; 7; 8; []; 1
4368; mov; []; 13; 6; 7; []; 1
4373; mov; []; 12; 8; 7; []; 1
4360; loa; []; 9; 10; 12; 7; []; 100; U
4364; loa; []; 9; 10; 13; 7; []; 100; U
4362; loa; []; 9; 10; 12; 8; []; 50; U
4366; loa; []; 9; 10; 13; 8; []; 50; U
Initial Vessel State
12; 6
13; 6
Final Vessel State
Initial Storage State
10; 12; 0
10; 13; 0
10; 9; 5000
Final Storage State
10; 12; 1000
10; 13; 1000
Task Properties
50; 0
Solution
15.0
4367; R; 0.0; 1.0
4370; R; 0.0; 1.0
4360; R; 1.0; 7.0
4366; R; 1.0; 6.0
4374; R; 7.0; 1.0
4371; R; 8.0; 1.0
4364; R; 8.0; 7.0
4362; R; 9.0; 6.0
Cargoes
0;LNG;1.0
Berths
1;Raid
2;Pier 1
3;Pier 2
Storages
4;Storage 1;0;270.0
Bunkers
Tows
6;Tow 1
Loading Equipments
5;Плавучий кран
Transport Ships
7;Ship 1;2000.0
Templates
8;mov;[];7;1;2;[6];2.0
9;mov;[];7;2;1;[6];2.0
10;mov;[];6;3;1;[];2.0
11;mov;[];6;2;1;[];2.0
12;mov;[];6;2;3;[];1.0
13;mov;[];5;3;2;[6];2.0
14;loa;[];7;0;4;2;[5];20.0; U
Cargo Flows
Initial Vessel State
7;1
6;2
5;3
Initial Storage State
0;4;0.0
0;7;100.0
Final Vessel State
7;1
Final Storage State
0;7;0.0
Task Properties
18.0;0
Solution
14.0
Cargoes
0;LNG;0.0
Berths
1;Raid
2;Pier 1
3;Pier 2
Storages
4;Storage 1;0;10000.0
Bunkers
Tows
Loading Equipments
Transport Ships
5;Ship 1;2000.0
6;Ship 2;2000.0
Templates
7;mov;[];5;1;2;[];1.0
8;mov;[];5;2;1;[];1.0
9;mov;[];5;1;3;[];1.0
10;mov;[];5;3;1;[];1.0
11;mov;[];5;2;3;[];1.0
12;mov;[];5;3;2;[];1.0
13;mov;[];6;1;2;[];1.0
14;mov;[];6;2;1;[];1.0
15;mov;[];6;1;3;[];1.0
16;mov;[];6;3;1;[];1.0
17;mov;[];6;2;3;[];1.0
18;mov;[];6;3;2;[];1.0
19;loa;[];4;0;5;2;[];100.0; U
20;loa;[];4;0;5;3;[];50.0; U
21;loa;[];4;0;6;2;[];100.0; U
22;loa;[];4;0;6;3;[];50.0; U
Cargo Flows
Initial Vessel State
5;1
6;1
Initial Storage State
0;5;0.0
0;6;0.0
0;4;10000.0
Final Vessel State
5;1
6;1
Final Storage State
0;5;1000.0
0;6;1000.0
Task Properties
24.0;0
Solution
16.0
7; R; 1.0; 1.0
10; R; 16.0; 1.0
11; R; 9.0; 1.0
14; R; 16.0; 1.0
15; R; 1.0; 1.0
18; R; 8.0; 1.0
19; R; 2.0; 7.0
20; R; 10.0; 6.0
21; R; 9.0; 7.0
22; R; 2.0; 6.0
......@@ -45,7 +45,7 @@ Final Storage State
0; 11; 2.0
Task Properties
15.0;0
20.0;0
Solution
12.0
15.0
Cargoes
83; Груз1
Berths
86; Рейд
88; Терминал2
Storages
89; Хранилище1; 83; 1000.0
Bunkers
Tows
Loading Equipments
Transport Ships
96; Судно1; 1000000.0
Templates
10;mrn;[]; 96; 88; []; 1.0
8;mov;[]; 96; 86; 88; []; 2.0
2;loa;[]; 96; 83; 89; 88; []; 20.0; M
Cargo Flows
Initial Vessel State
96; 86
Initial Storage State
83; 89; 0.0
83; 96; 60.0
Final Vessel State
Final Storage State
83; 89; 60.0
Task Properties
20.0;0
Solution
6.0
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment