-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluations.py
124 lines (83 loc) · 4.3 KB
/
evaluations.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""This python script can be used to test the correctness and finiteness of the algorithms."""
from multiprocessing import Process
from BASolver2 import bASolve, bASolverHandle
from OPBASolver import OPSolverHandle
import time
# bASolve()
class NonFiniteException(Exception):
pass
def testcorrectness(algo):
""""Test the algorithm specified in <algo>
algo:
1: BA-Algorithm
2: OPBA-Algorithm
3: Algorithm X
The input will be passed to the algorithm directly
"""
if algo == 1:
return testBA()
elif algo == 2:
return testOPBA()
elif algo == 3:
return testAlgoX()
def locBASOLVE(grid):
print(bASolverHandle(grid))
# print(grid)
def testBA():
inputs = []
validInput = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 0, 0, 0, 7, 5, 0, 0, 9], [0, 0, 7, 0, 4, 0, 2, 6, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 0, 1, 0, 5,0, 9, 3, 0], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7]]
inputs.append(validInput)
wrongdim = [[1,2,3,4],[4,3,2,1],[2,1,4,3],[3,4,1,2]] # 4x4 instead of 9x9
inputs.append(wrongdim)
# 8 is two times in a collomn at start, therfore unsolvable.
invStart = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 8, 0, 0, 7, 5, 0, 0, 9], [0, 0, 7, 0, 4, 0, 2, 6, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 0, 1, 0, 5,0, 9, 3, 0], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7]]
inputs.append(invStart)
# 22 is not valid.
invNumbers = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 22, 0, 0, 7, 5, 0, 0, 9], [0, 0, 7, 0, 4, 0, 2, 6, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 0, 1, 0, 5,0, 9, 3, 0], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7]]
inputs.append(invNumbers)
emptyinp = [[0 for _ in range(9)] for _ in range(9)]
inputs.append(emptyinp)
for inp in inputs:
proc = Process(target=locBASOLVE, args=[inp])
proc.start()
curtim = time.time()
proc.join(timeout=11) # This stops the test if it takes longer than 10 seconds
if abs(curtim-time.time()) >10:
print("ERROR, took longer than 10 seconds. Stoped after 10 seconds")
raise NonFiniteException("The solver took more than 10 seconds.")
proc.terminate()
print("NEXT")
def testOP():
inputs = []
validInput = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 0, 0, 0, 7, 5, 0, 0, 9], [0, 0, 7, 0, 4, 0, 2, 6, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 0, 1, 0, 5,0, 9, 3, 0], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7]]
inputs.append(validInput)
wrongdim = [[1,2,3,4],[4,3,2,1],[2,1,4,3],[3,4,1,2]] # 4x4 instead of 9x9
inputs.append(wrongdim)
# 8 is two times in a collomn at start, therfore unsolvable.
invStart = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 8, 0, 0, 7, 5, 0, 0, 9], [0, 0, 7, 0, 4, 0, 2, 6, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 0, 1, 0, 5,0, 9, 3, 0], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7]]
inputs.append(invStart)
# 22 is not valid.
invNumbers = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 22, 0, 0, 7, 5, 0, 0, 9], [0, 0, 7, 0, 4, 0, 2, 6, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 0, 1, 0, 5,0, 9, 3, 0], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7]]
inputs.append(invNumbers)
# Empty field:
emptyinp = [[0 for _ in range(9)] for _ in range(9)]
inputs.append(emptyinp)
for inp in inputs:
proc = Process(target=OPSolverHandle, args=[inp])
proc.start()
curtim = time.time()
proc.join(timeout=11) # This stops the test if it takes longer than 10 seconds
if abs(curtim-time.time()) >10:
print("ERROR, took longer than 10 seconds. Stoped after 10 seconds")
raise NonFiniteException("The solver took more than 10 seconds.")
proc.terminate()
print("NEXT")
def testOPBA():
pass
def testAlgoX():
pass
if __name__ == "__main__":
testBA()
testOP()
testAlgoX()
# print(bASolverHandle([[0 for _ in range(9)] for _ in range(9)]))