Skip to content

Commit fc84611

Browse files
committed
Merge branch 'visualising' into OO-approach
2 parents e86d54c + fe81e9b commit fc84611

File tree

97 files changed

+45
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+45
-18
lines changed

Queens/Agents.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
def steepestAscentAgent():
55
state = yield "steepestAscentAgent"
66
while True:
7-
# yield "Success", np.arange(0, 7, 1, dtype=int)
87
cols = count_collisions(state)
98
if cols == 0:
109
state = yield "Success", state
@@ -16,8 +15,8 @@ def steepestAscentAgent():
1615
state = yield "NoOp", state
1716

1817

19-
def plateauExplorerGenerator():
20-
state = yield "plateauExplorerGenerator"
18+
def plateauExplorerAgent():
19+
state = yield "plateauExplorerAgent"
2120
plateau = None
2221
while True:
2322
cols = count_collisions(state)
@@ -40,7 +39,7 @@ def plateauExplorerGenerator():
4039
state = yield "NoOp", state
4140

4241

43-
def plateauLimitedGenerator(threshold):
42+
def plateauLimitedAgent(threshold):
4443
state = yield "PlateauLimited, max={}".format(threshold)
4544
plateau, count = None, 0
4645
while True:
@@ -70,7 +69,7 @@ def plateauLimitedGenerator(threshold):
7069
state = yield "NoOp", state
7170

7271

73-
def masterBeamGenerator(queens):
72+
def masterBeamAgent(queens):
7473
tables = yield "Master Beam Generator"
7574
k = len(tables)
7675
while True:

Queens/Environment.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def __init__(self, agents, queens=8, master=None):
1616
self.tables = [Table(a, queens) for a in agents]
1717
if master:
1818
self.master = Table(master, queens)
19+
else:
20+
self.master = None
1921
self.stats = StatsModule(agents, self.master)
2022

2123
def step(self):
@@ -49,7 +51,7 @@ def find_sol(self, how_many):
4951
max_found = 0
5052
while max_found < how_many:
5153
self.step()
52-
max_found = max(self.stats.solutions.values())
54+
max_found = max(map(len, self.stats.solutions.values()))
5355
progress_bar(max_found, how_many, "solutions found")
5456
self.print_stats()
5557

Queens/Stats.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import matplotlib.pyplot as plt
23
import warnings
34

45

@@ -42,6 +43,8 @@ def print_stats(self, tables):
4243
with warnings.catch_warnings():
4344
warnings.simplefilter("ignore", category=RuntimeWarning)
4445
self.print_table_stats(t)
46+
for b in self.solutions[t.agent]:
47+
self.plot_board(b)
4548

4649
def print_table_stats(self, table):
4750
win = len(self.win_times[table.agent])
@@ -52,7 +55,18 @@ def print_table_stats(self, table):
5255
print("Avg. win time:", np.mean(self.win_times[table.agent]))
5356
print("Avg. loss time:", np.mean(self.loss_times[table.agent]))
5457
print()
55-
58+
59+
def plot_board(self, board):
60+
q = len(board)
61+
im = np.zeros((q, q))
62+
for i in board:
63+
im[i, board[i]] = 1
64+
dpp = 15
65+
im = np.repeat(np.repeat(im, dpp, axis=0), dpp, axis=1)
66+
num = "".join(map(lambda k: str(k), board))
67+
path = './plots/' + str(q).zfill(2) + 'board' + num + '.png'
68+
plt.imsave(fname=path, arr=im, cmap=plt.cm.binary)
69+
5670
def add_win(self, table):
5771
"""Called when agent reports winning combination as it's state. Returns the number of new
5872
solutions."""

Queens/main.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
from Queens.Environment import QueensEnv
22
from Queens.Agents import *
33

4-
queens = 8
5-
k = 4
4+
queens = 8 # how big is the chessboard
65

7-
# plateauLimitedGenerator = PlateauLimitedAgent()
8-
# stepGenerator = SteepestAscentAgent()
6+
env = QueensEnv([steepestAscentAgent()], queens=queens)
7+
env.run(200)
8+
# Let agent do 2000 steps and see how many solutions it finds
9+
10+
print()
11+
steep = steepestAscentAgent()
12+
plateau = plateauExplorerAgent()
13+
env = QueensEnv([steep, plateau], queens=queens)
14+
env.find_sol(73)
15+
# challenge which agent finds 90% of all solutions possible
16+
17+
print()
18+
agents = [plateauLimitedAgent(t) for t in range(6)]
19+
env = QueensEnv(agents, queens=queens)
20+
env.find_sol(73)
21+
# see for how long is it efficient to explore the plateau
22+
23+
print()
924
agents = [steepestAscentAgent() for t in range(4)]
10-
master = masterBeamGenerator(queens)
25+
master = masterBeamAgent(queens)
1126
env = QueensEnv(agents, master=master, queens=queens)
27+
env.find_sol(73)
28+
# use beam search to find all the solutions
1229

13-
14-
env.find_sol_master(82)
15-
# env.find_sol(10)
16-
# env.run(400)
17-
# env.print_stats()
18-
# print(env.stats.solutions)
30+
# remember to check the plots ;)

Queens/plots/08board04752613.png

447 Bytes

Queens/plots/08board05726314.png

447 Bytes

Queens/plots/08board06357142.png

443 Bytes

Queens/plots/08board06471352.png

441 Bytes

Queens/plots/08board13572064.png

441 Bytes

Queens/plots/08board14602753.png

443 Bytes

0 commit comments

Comments
 (0)