|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": null, |
| 5 | + "execution_count": 80, |
6 | 6 | "id": "8538faa5-d001-4cd1-aca7-713308a31db2", |
7 | 7 | "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 | + ], |
9 | 18 | "source": [ |
10 | 19 | "from search_2 import *\n", |
| 20 | + "import time\n", |
| 21 | + "\n", |
| 22 | + "N = 100\n", |
11 | 23 | "\n", |
| 24 | + "# First we code up the search agent\n", |
12 | 25 | "class VacuumSearch(Problem):\n", |
13 | 26 | " def __init__(self, initial=(0,0), goal=None, dirt=[], **kwds):\n", |
14 | 27 | " Problem.__init__(self, initial=initial, goal=goal, dirt=dirt, **kwds)\n", |
|
18 | 31 | "\n", |
19 | 32 | " def action_cost(self, s, action, s1):\n", |
20 | 33 | " if s == s1:\n", |
21 | | - " # we have sucked up some dirt\n", |
22 | 34 | " return -100\n", |
23 | 35 | " return 1\n", |
24 | 36 | " \n", |
25 | 37 | " def is_goal(self, state):\n", |
26 | 38 | " return self.dirt == []\n", |
27 | 39 | "\n", |
28 | 40 | " def result(self, state, action):\n", |
29 | | - " return action if action not in self.dirt else state\n", |
| 41 | + " return action\n", |
30 | 42 | " \n", |
31 | 43 | " def actions(self, state):\n", |
32 | 44 | " if state in self.dirt:\n", |
33 | 45 | " self.dirt.remove(state)\n", |
34 | | - " return set(state)\n", |
| 46 | + " S = {(state[0],state[1])}\n", |
| 47 | + " return S\n", |
35 | 48 | " x, y = state\n", |
36 | 49 | " 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", |
38 | 51 | " return Z\n", |
39 | 52 | "\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) " |
42 | 67 | ] |
43 | 68 | }, |
44 | 69 | { |
45 | 70 | "cell_type": "code", |
46 | 71 | "execution_count": null, |
47 | | - "id": "dab5c21d-7f22-4cb2-938d-6a32d3aa4b93", |
| 72 | + "id": "952c637b-82fc-4ad6-bc49-39998d9acd8e", |
48 | 73 | "metadata": {}, |
49 | 74 | "outputs": [], |
50 | 75 | "source": [] |
51 | 76 | }, |
52 | 77 | { |
53 | 78 | "cell_type": "code", |
54 | 79 | "execution_count": null, |
55 | | - "id": "5d4efa04-60c9-4f4d-a100-9bd48aeaa5af", |
| 80 | + "id": "4f3bf803-5b3d-48ca-bf5a-75161303aeb0", |
56 | 81 | "metadata": {}, |
57 | 82 | "outputs": [], |
58 | 83 | "source": [] |
|
0 commit comments