Skip to content

Commit 2bc8703

Browse files
committed
working vacuum agent
1 parent 2ed977e commit 2bc8703

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

vacuum_search_agent.ipynb

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 80,
66
"id": "8538faa5-d001-4cd1-aca7-713308a31db2",
77
"metadata": {},
8-
"outputs": [],
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"0.5639076232910156\n",
14+
"-1527\n"
15+
]
16+
}
17+
],
918
"source": [
1019
"from search_2 import *\n",
20+
"import time\n",
21+
"\n",
22+
"N = 100\n",
1123
"\n",
24+
"# First we code up the search agent\n",
1225
"class VacuumSearch(Problem):\n",
1326
" def __init__(self, initial=(0,0), goal=None, dirt=[], **kwds):\n",
1427
" Problem.__init__(self, initial=initial, goal=goal, dirt=dirt, **kwds)\n",
@@ -18,41 +31,53 @@
1831
"\n",
1932
" def action_cost(self, s, action, s1):\n",
2033
" if s == s1:\n",
21-
" # we have sucked up some dirt\n",
2234
" return -100\n",
2335
" return 1\n",
2436
" \n",
2537
" def is_goal(self, state):\n",
2638
" return self.dirt == []\n",
2739
"\n",
2840
" def result(self, state, action):\n",
29-
" return action if action not in self.dirt else state\n",
41+
" return action\n",
3042
" \n",
3143
" def actions(self, state):\n",
3244
" if state in self.dirt:\n",
3345
" self.dirt.remove(state)\n",
34-
" return set(state)\n",
46+
" S = {(state[0],state[1])}\n",
47+
" return S\n",
3548
" x, y = state\n",
3649
" S = {(x + dx, y + dy) for (dx, dy) in self.directions}\n",
37-
" Z = {x for x in S if x[0] in range(3) and x[1] in range(3)}\n",
50+
" Z = {x for x in S if x[0] in range(N) and x[1] in range(N)}\n",
3851
" return Z\n",
3952
"\n",
40-
"v = VacuumSearch(dirt=[(0,2), (1,2), (2,2)])\n",
41-
"print(path_states(uniform_cost_search(v)))"
53+
"\n",
54+
"k = int(0.2 * N)\n",
55+
"dirt = [(x,y) for x in range(N) for y in range(N)]\n",
56+
"dirt = random.sample(dirt, k=k)\n",
57+
"v = VacuumSearch(dirt=dirt)\n",
58+
"\n",
59+
"# execution time\n",
60+
"t0 = time.time()\n",
61+
"u1 = uniform_cost_search(v)\n",
62+
"t1 = time.time()\n",
63+
"print(t1-t0)\n",
64+
"\n",
65+
"# path cost of actions\n",
66+
"print(u1.path_cost) "
4267
]
4368
},
4469
{
4570
"cell_type": "code",
4671
"execution_count": null,
47-
"id": "dab5c21d-7f22-4cb2-938d-6a32d3aa4b93",
72+
"id": "952c637b-82fc-4ad6-bc49-39998d9acd8e",
4873
"metadata": {},
4974
"outputs": [],
5075
"source": []
5176
},
5277
{
5378
"cell_type": "code",
5479
"execution_count": null,
55-
"id": "5d4efa04-60c9-4f4d-a100-9bd48aeaa5af",
80+
"id": "4f3bf803-5b3d-48ca-bf5a-75161303aeb0",
5681
"metadata": {},
5782
"outputs": [],
5883
"source": []

0 commit comments

Comments
 (0)