-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
109 lines (84 loc) · 2.88 KB
/
utils.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
from datetime import datetime
import matplotlib.pyplot as plt
from stats import RunStats
def visualize_stats(multiple_run_stats, color, label):
"""
:type multiple_run_stats: list[list[model.Stat]]
:type color: str
:type label: str
"""
plt.xlabel("Time (seconds)")
plt.ylabel("properly placed queens")
for run_stats in multiple_run_stats:
iterations, seconds, values = split_to_iters_time_and_values(run_stats)
plt.plot(seconds, values, "{}--".format(color), label=label)
def get_avg_iterations(mutliple_run_iterations):
"""
:type mutliple_run_iterations: list[list[model.Stat]]
:rtype: float
"""
iterations_sum = 0
for run_iterations in mutliple_run_iterations:
iterations_sum += len(run_iterations)
return float(iterations_sum) / float(len(mutliple_run_iterations))
def get_avg_seconds(mutliple_run_stats):
"""
:type mutliple_run_stats: list[stats.RunStats]
:rtype: float
"""
time_sum = 0
for run_stats in mutliple_run_stats:
time_sum += run_stats.run_time()
return float(time_sum) / float(len(mutliple_run_stats))
def split_to_iters_time_and_values(run_stats):
"""
:type run_stats: list[model.Stat]
:rtype tuple[list]
"""
return zip(*[(stat.iteration, stat.seconds, stat.value) for stat in run_stats])
class DuplicateQueen(Exception):
pass
class QueenOutOfChessboard(Exception):
pass
class ChessboardIsEmpty(Exception):
pass
def log(message):
print("{} | {}".format(datetime.now(), message))
def run_multiple_times(times, function, params):
"""
:type times: int
:type function: function
:type params: list
:rtype: list[stats.RunStats]
"""
multiple_run_stats = []
for i in range(0, times):
board, stats = function(*params)
multiple_run_stats.append(RunStats(params[0], board, params[2], stats))
return multiple_run_stats
def display_chessboard(queens, chessboard_size):
"""
:type queens: list[model.Queen]
:type chessboard_size: int
"""
print("".join(["-"] * ((chessboard_size * 3) + 2)))
for y in range(0, chessboard_size):
row = []
for x in range(0, chessboard_size):
queens_on_pos = filter(lambda queen: queen.x == x and queen.y == y, queens)
row.append(" - " if not queens_on_pos else " X ")
print(" {}|{}".format(y, "".join(row)) if y <= 9 else "{}|{}".format(y, "".join(row)))
print("".join(["-"] * ((chessboard_size * 3) + 2)))
def seconds_since(start_time):
"""
:type start_time: datetime
:rtype: float
"""
return (datetime.now() - start_time).total_seconds()
def are_queens_on_diagonal(first_queen, second_queen):
"""
:type first_queen: model.Queen
:type second_queen: model.Queen
:return: bool
"""
return abs(second_queen.x - first_queen.x) == abs(second_queen.y - first_queen.y)