Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e1df989

Browse files
Working Hill Climber for Queens problem
1 parent adb2c42 commit e1df989

File tree

2 files changed

+381
-1
lines changed

2 files changed

+381
-1
lines changed

Our_vacuum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ def execute_action(self, agent, action):
3131
def OurVacuum():
3232
Env = TrivialVacuumEnvironment()
3333
Env.add_thing(TraceAgent(RandomVacuumAgent()))
34-
return Env #dupa
34+
return Env

Queens.ipynb

Lines changed: 380 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 174,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"from random import random\n",
12+
"from agents import *\n",
13+
"import agents\n",
14+
"from copy import copy"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 249,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"class QueensEnvironment(agents.Environment):\n",
24+
" \n",
25+
" def __init__(self):\n",
26+
" super().__init__()\n",
27+
" self.random_state()\n",
28+
" \n",
29+
" def random_state(self):\n",
30+
" self.state = [int(8 * random.random()) for j in range(8) ]\n",
31+
" \n",
32+
" def percept(self, agent):\n",
33+
" return self.state\n",
34+
" \n",
35+
" def execute_action(self, agent, action):\n",
36+
" if action == \"NoOp\":\n",
37+
" print(\"new board generated\")\n",
38+
" self.random_state()\n",
39+
" return state\n",
40+
" elif action == \"Success\":\n",
41+
" agent.alive = False\n",
42+
" return None\n",
43+
" agent.performance -= 1\n",
44+
" num, pos = action\n",
45+
" self.state[num] = pos"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 263,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"def QueensHillClimbAgent():\n",
55+
" \n",
56+
" def count_collisions(state):\n",
57+
" count = 0\n",
58+
" for i in range(8):\n",
59+
" for j in range(i+1, 8):\n",
60+
" if state[i] == state[j] or abs(state[i] - state[j]) == j - i:\n",
61+
" count += 1\n",
62+
" return count\n",
63+
" \n",
64+
" def print_board(state):\n",
65+
" print(\"--\"*10)\n",
66+
" for row in state:\n",
67+
" print(str(row) + \" \" + \" \" * row + \"x\" )\n",
68+
" print(\"--\"*10)\n",
69+
"\n",
70+
" def check_actions(state):\n",
71+
" res = []\n",
72+
" run = copy(state)\n",
73+
" for q in range(8):\n",
74+
" col_res = []\n",
75+
" pos = state[q]\n",
76+
" for i in range(8):\n",
77+
" run[q] = i\n",
78+
" col_res.append(count_collisions(run))\n",
79+
" col_res[pos] = 100\n",
80+
" run[q] = pos\n",
81+
" res.append(col_res)\n",
82+
" return res\n",
83+
" \n",
84+
" def program(state):\n",
85+
" print_board(state)\n",
86+
" current = count_collisions(state) \n",
87+
" \n",
88+
" if(current == 0):\n",
89+
" print(\"Success\", state)\n",
90+
" print_board(state)\n",
91+
" return \"Success\"\n",
92+
" \n",
93+
" grid = check_actions(state)\n",
94+
" d = dict()\n",
95+
" for i in range(8):\n",
96+
" for j in range(8):\n",
97+
" d[grid[i][j]] = d.get(grid[i][j], []) + [(i, j)]\n",
98+
" new_min = min(d)\n",
99+
"\n",
100+
" if(new_min < current):\n",
101+
" return random.choice(d[new_min])\n",
102+
" else:\n",
103+
" print(\"Local minimum reached at: \" + str(new_min))\n",
104+
" return \"NoOp\"\n",
105+
" \n",
106+
" return program"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 264,
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"q = QueensEnvironment()"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 265,
121+
"metadata": {},
122+
"outputs": [],
123+
"source": [
124+
"q.add_thing(Agent(QueensHillClimbAgent()))"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 266,
130+
"metadata": {},
131+
"outputs": [
132+
{
133+
"name": "stdout",
134+
"output_type": "stream",
135+
"text": [
136+
"--------------------\n",
137+
"2 x\n",
138+
"0 x\n",
139+
"7 x\n",
140+
"4 x\n",
141+
"6 x\n",
142+
"1 x\n",
143+
"2 x\n",
144+
"2 x\n",
145+
"--------------------\n",
146+
"--------------------\n",
147+
"2 x\n",
148+
"0 x\n",
149+
"7 x\n",
150+
"4 x\n",
151+
"6 x\n",
152+
"1 x\n",
153+
"2 x\n",
154+
"5 x\n",
155+
"--------------------\n",
156+
"--------------------\n",
157+
"3 x\n",
158+
"0 x\n",
159+
"7 x\n",
160+
"4 x\n",
161+
"6 x\n",
162+
"1 x\n",
163+
"2 x\n",
164+
"5 x\n",
165+
"--------------------\n",
166+
"Local minimum reached at: 1\n",
167+
"new board generated\n",
168+
"--------------------\n",
169+
"2 x\n",
170+
"3 x\n",
171+
"7 x\n",
172+
"2 x\n",
173+
"3 x\n",
174+
"6 x\n",
175+
"5 x\n",
176+
"6 x\n",
177+
"--------------------\n",
178+
"--------------------\n",
179+
"2 x\n",
180+
"3 x\n",
181+
"7 x\n",
182+
"2 x\n",
183+
"3 x\n",
184+
"6 x\n",
185+
"0 x\n",
186+
"6 x\n",
187+
"--------------------\n",
188+
"--------------------\n",
189+
"2 x\n",
190+
"3 x\n",
191+
"7 x\n",
192+
"2 x\n",
193+
"4 x\n",
194+
"6 x\n",
195+
"0 x\n",
196+
"6 x\n",
197+
"--------------------\n",
198+
"--------------------\n",
199+
"2 x\n",
200+
"3 x\n",
201+
"7 x\n",
202+
"2 x\n",
203+
"4 x\n",
204+
"6 x\n",
205+
"0 x\n",
206+
"5 x\n",
207+
"--------------------\n",
208+
"--------------------\n",
209+
"7 x\n",
210+
"3 x\n",
211+
"7 x\n",
212+
"2 x\n",
213+
"4 x\n",
214+
"6 x\n",
215+
"0 x\n",
216+
"5 x\n",
217+
"--------------------\n",
218+
"Local minimum reached at: 1\n",
219+
"new board generated\n",
220+
"--------------------\n",
221+
"1 x\n",
222+
"4 x\n",
223+
"4 x\n",
224+
"4 x\n",
225+
"3 x\n",
226+
"4 x\n",
227+
"7 x\n",
228+
"6 x\n",
229+
"--------------------\n",
230+
"--------------------\n",
231+
"1 x\n",
232+
"4 x\n",
233+
"4 x\n",
234+
"0 x\n",
235+
"3 x\n",
236+
"4 x\n",
237+
"7 x\n",
238+
"6 x\n",
239+
"--------------------\n",
240+
"--------------------\n",
241+
"1 x\n",
242+
"4 x\n",
243+
"4 x\n",
244+
"0 x\n",
245+
"3 x\n",
246+
"5 x\n",
247+
"7 x\n",
248+
"6 x\n",
249+
"--------------------\n",
250+
"--------------------\n",
251+
"1 x\n",
252+
"4 x\n",
253+
"4 x\n",
254+
"0 x\n",
255+
"3 x\n",
256+
"5 x\n",
257+
"7 x\n",
258+
"2 x\n",
259+
"--------------------\n",
260+
"--------------------\n",
261+
"1 x\n",
262+
"4 x\n",
263+
"6 x\n",
264+
"0 x\n",
265+
"3 x\n",
266+
"5 x\n",
267+
"7 x\n",
268+
"2 x\n",
269+
"--------------------\n",
270+
"Local minimum reached at: 1\n",
271+
"new board generated\n",
272+
"--------------------\n",
273+
"7 x\n",
274+
"6 x\n",
275+
"1 x\n",
276+
"7 x\n",
277+
"5 x\n",
278+
"5 x\n",
279+
"5 x\n",
280+
"6 x\n",
281+
"--------------------\n",
282+
"--------------------\n",
283+
"7 x\n",
284+
"6 x\n",
285+
"1 x\n",
286+
"7 x\n",
287+
"5 x\n",
288+
"5 x\n",
289+
"0 x\n",
290+
"6 x\n",
291+
"--------------------\n",
292+
"--------------------\n",
293+
"2 x\n",
294+
"6 x\n",
295+
"1 x\n",
296+
"7 x\n",
297+
"5 x\n",
298+
"5 x\n",
299+
"0 x\n",
300+
"6 x\n",
301+
"--------------------\n",
302+
"--------------------\n",
303+
"2 x\n",
304+
"6 x\n",
305+
"1 x\n",
306+
"7 x\n",
307+
"5 x\n",
308+
"3 x\n",
309+
"0 x\n",
310+
"6 x\n",
311+
"--------------------\n",
312+
"--------------------\n",
313+
"2 x\n",
314+
"6 x\n",
315+
"1 x\n",
316+
"7 x\n",
317+
"5 x\n",
318+
"3 x\n",
319+
"0 x\n",
320+
"4 x\n",
321+
"--------------------\n",
322+
"Success [2, 6, 1, 7, 5, 3, 0, 4]\n",
323+
"--------------------\n",
324+
"2 x\n",
325+
"6 x\n",
326+
"1 x\n",
327+
"7 x\n",
328+
"5 x\n",
329+
"3 x\n",
330+
"0 x\n",
331+
"4 x\n",
332+
"--------------------\n"
333+
]
334+
}
335+
],
336+
"source": [
337+
"q.run()"
338+
]
339+
},
340+
{
341+
"cell_type": "code",
342+
"execution_count": null,
343+
"metadata": {
344+
"collapsed": true
345+
},
346+
"outputs": [],
347+
"source": []
348+
},
349+
{
350+
"cell_type": "code",
351+
"execution_count": null,
352+
"metadata": {
353+
"collapsed": true
354+
},
355+
"outputs": [],
356+
"source": []
357+
}
358+
],
359+
"metadata": {
360+
"kernelspec": {
361+
"display_name": "Python 3",
362+
"language": "python",
363+
"name": "python3"
364+
},
365+
"language_info": {
366+
"codemirror_mode": {
367+
"name": "ipython",
368+
"version": 3
369+
},
370+
"file_extension": ".py",
371+
"mimetype": "text/x-python",
372+
"name": "python",
373+
"nbconvert_exporter": "python",
374+
"pygments_lexer": "ipython3",
375+
"version": "3.6.2"
376+
}
377+
},
378+
"nbformat": 4,
379+
"nbformat_minor": 2
380+
}

0 commit comments

Comments
 (0)