Skip to content

Commit fe81e9b

Browse files
committed
Informative main added, code improvements
1 parent 504a695 commit fe81e9b

13 files changed

Lines changed: 30 additions & 17 deletions

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/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 # how big is the
4+
queens = 8 # how big is the chessboard
55

6-
# plateauLimitedGenerator = PlateauLimitedAgent()
7-
# stepGenerator = SteepestAscentAgent()
8-
agents = [steepestAscentAgent() for t in range(4)]
9-
master = masterBeamGenerator(queens)
10-
env = QueensEnv(agents, master=master, queens=queens)
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
1116

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
1222

13-
env.find_sol_master(82)
14-
# env.find_sol(10)
15-
# env.run(400)
16-
# env.print_stats()
17-
# print(env.stats.solutions)
23+
print()
24+
agents = [steepestAscentAgent() for t in range(4)]
25+
master = masterBeamAgent(queens)
26+
env = QueensEnv(agents, master=master, queens=queens)
27+
env.find_sol(73)
28+
# use beam search to find all the solutions
1829

30+
# remember to check the plots ;)

Queens/plots/08board06471352.png

441 Bytes
Loading

Queens/plots/08board14602753.png

443 Bytes
Loading

Queens/plots/08board17502463.png

438 Bytes
Loading

Queens/plots/08board24170635.png

444 Bytes
Loading

Queens/plots/08board37025164.png

444 Bytes
Loading

Queens/plots/08board37046152.png

445 Bytes
Loading

Queens/plots/08board41357206.png

440 Bytes
Loading

0 commit comments

Comments
 (0)