-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.kt
108 lines (90 loc) · 4.01 KB
/
main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.io.File
fun solveSinglePuzzle(solver: SolverAStar) {
// record starting time
val sysDate = System.currentTimeMillis()
var depth_Max: Byte = 0
var ResultArray: Array<FifteenPuzzle>? = null
while (ResultArray == null) {
ResultArray = solver.searchClosest()
val currentState = solver.openSet.peek()
if (currentState != null && currentState.depth > depth_Max) {
print("max Search depth: ${currentState.depth}")
println("\tclose set size: ${solver.closeSet.size}\topen set size: ${solver.openSet.size()}")
depth_Max = currentState.depth
}
}
println("\nFound Solution in search depth: ${ResultArray[0].depth}")
println("Total: ${ResultArray[0].depth + ResultArray[1].depth} steps")
val timelaps = System.currentTimeMillis() - sysDate
println(ResultArray[0].printSolutionRecursive(ResultArray[1]))
println("Used time: ${timelaps * 0.001}s")
println("Total Nodes: ${solver.openSet.size() + solver.closeSet.size + solver.targetSet.size}")
}
fun main(args: Array<String>) {
// Check if the correct number of arguments is provided
if (args.size != 2) {
println("Usage: java -jar -Xms8g mainkt.jar <input_file> <target_file>")
println("Usage: java -jar -Xms8g mainkt.jar batch <input_file>")
return
}
if (args[0] == "batch" && args.size == 2) {
// Run batch processing
val batchInputFile = File(args[1])
val batchInputLines = batchInputFile.useLines { it.toList() }
.filterNot { it.startsWith("#") }
var isTargetLine = true
var end: FifteenPuzzle? = null
for (line in batchInputLines) {
val numbers = line.trim().split("\\s+".toRegex())
.filterNot { it.isBlank() }
.map { it.toInt() }
if (isTargetLine && numbers.size == 16) {
// This line is the target
val targetBoard = numbers.toIntArray()
end = FifteenPuzzle(targetBoard)
isTargetLine = false
} else if (end != null && numbers.size == 17) {
// This line is a test case
val startBoard = numbers.take(16).toIntArray()
val start = FifteenPuzzle(startBoard)
// Add your solving logic here using 'start' and 'end'
val solver = SolverAStar(start, end, 72)
solveSinglePuzzle(solver)
}
}
} else {
// Read the input file
val inputFile = File(args[0])
val inputLines = inputFile.useLines { it.toList() }
// Initialize the starting puzzle board
val startBoard = inputLines
.filterNot { it.startsWith("#") || it.isBlank() }
.map { line -> line.trim().split("\\s+".toRegex()).map { it.toInt() }.toIntArray() }
.toTypedArray()
val start = FifteenPuzzle(startBoard)
// Read the target file
val targetFile = File(args[1])
val targetLines = targetFile.useLines { it.toList() }
// Initialize the target puzzle board
val endBoard = targetLines
.filterNot { it.startsWith("#") || it.isBlank() }
.map { line -> line.trim().split("\\s+".toRegex()).map { it.toInt() }.toIntArray() }
.toTypedArray()
val end = FifteenPuzzle(endBoard)
println("start: \n${start}")
println("start manhattan: " + start.heuristics)
println("start parity: " + start.getParity())
println("end parity: " + end.getParity())
// parity check
val parityDifference = start.getParity() - end.getParity()
if (parityDifference % 2 == 0) {
println("parity check passed.")
} else {
println("parity check shows unsolvable, exiting.")
return
}
val solver = SolverAStar(start, end, 72)
println("sample length ${solver.targetArray.size}")
solveSinglePuzzle(solver)
}
}