-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.py
128 lines (90 loc) · 3.47 KB
/
performance.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
125
126
127
128
#!/usr/bin/env python3
import cProfile
from gridcreater import retrive_json
from time import sleep, perf_counter
import XSolver as ds
import BASolver as basicsolver
import BASolver2 as bs2
import OPBASolver as advsolve
# Stuff
def main(infile=None,outfile=None,n=None):
"""
Function to test algorithms
:param infile: Path of Pickle file with sudokus
:param outfile: Name of text file with stats about solver
"""
print("This programm compares the three algorithms to solve Sudokus")
if infile is None:
infile = input("Path to input file: ")
if outfile is None:
outfile = input("Path to output file: ")
if n is None:
try:
n = int(input("Number of times executed: "))
except ValueError:
print("Invalid input, using 1.")
n=1
sudoku_set = retrive_json(infile)
with open(outfile,"a+") as f:
f.write("\n\r\n\r==============================================")
f.write("==============================================\n\r")
f.write(f"Start of Benchmark with {n} executions of the set with {len(sudoku_set)} sudokus.\n\r")
f.write("==============================================\n\r")
for m in range(n):
time = execute(sudoku_set,normalBacktrack)
timeoutput(outfile, time,"Normal Backtracking",m)
print(f"Normal Backtracking done in {time} seconds")
sleep(60*5) # Waiting 5 minutes between executing the algorithms.
time2 = execute(sudoku_set,advancedBacktrack)
timeoutput(outfile, time2,"Advanced Backtracking",m)
print(f"Advanced Backtracking done in {time2} seconds")
sleep(60*5) # Waiting 5 minutes between executing the algorithms.
time3 = execute(sudoku_set,algox)
timeoutput(outfile, time3,"Dancing Links",m)
print(f"Dancing Links done in {time3} seconds")
print(f"Finished in {time+time2+time3} seconds")
sleep(60*5) # Waiting 5 minutes between executing the algorithms.
def timeoutput(outfile, time, method, n):
"""Writes Measurements to <outfile>"""
with open(outfile,"a+") as f:
f.write(f"Solved a set of sudokus in {time:.3f} Seconds using the {method} algorithm.\n\r")
def executeMe1():
"""This gets executed with the profiler."""
infile = "sudoku1.json"
sudoku_set = retrive_json(infile)
for sud in sudoku_set:
normalBacktrack(sud)
# advancedBacktrack(sud)
# algox(sud)
def profiling():
"""Profiling to analyze the algorithms in detail."""
cProfile.run("executeMe1()",)
def execute(sud_set,algorithm):
"""Measures the time of execution time for a algorithm for a set of Sudoukus with """
tic= perf_counter()
# Do stuff
for sud in sud_set:
algorithm(sud)
toc=perf_counter()
return toc-tic
def executenotime(sud_set,algorithm):
for sud in sud_set:
algorithm(sud)
def normalBacktrack(sud):
""" Interface for Native Backtracking """
bo=ds.exact_to_matrix(9, sud)
bs2.bASolve(bo)
def advancedBacktrack(sud):
""" Interface for Native Backtracking """
abc=[]
bo= ds.exact_to_matrix(9, sud)
advsolve.solveadv(bo,sols=abc)
return abc
def algox(sud):
""" Interface for Algorithm X """
ds.sudoku(problem=sud)
if __name__ == "__main__":
# main(infile="finallSudoku1000.json",outfile="outsud/finalout.txt",n=1)
main(infile="sudoku1.json",outfile="outsud/sut22.txt",n=1)
# profiling()
# executeMe1()