Ifi6057w2: erinevus redaktsioonide vahel
Mine navigeerimisribale
Mine otsikasti
| (ei näidata sama kasutaja 2 vahepealset redaktsiooni) | |||
| 8. rida: | 8. rida: | ||
Videoloeng: [http://www.youtube.com/watch?v=owK5uEYtA4c olekuruum] | Videoloeng: [http://www.youtube.com/watch?v=owK5uEYtA4c olekuruum] | ||
== Harjutustund == | |||
Eeldus: olemas on [https://github.com/aimacode/aima-python aima-python] ja [https://github.com/aimacode/aima-data aima-data]. Lahendame [https://en.wikipedia.org/wiki/15_puzzle 8-Puzzle]: | |||
1 2 3 | |||
7 5 | |||
8 4 6 | |||
* <code>search.py</code> sees on otsingu funktsioonid ja otsinguülesande formulatsioon <code>Problem</code> class | |||
* oma töö jaoks uus fail <code>ht2.py</code> | |||
import search | |||
# ... siin tuleb problem defineerida | |||
asi = search.breadth_first_tree_search(problem) | |||
* defineeri algolek ja lõppolek | |||
inistate = (1,2,3,7,0,5,8,4,6) | |||
goal = (1,2,3,4,5,6,7,8,0) | |||
* tee oma klass <code>EightPuzzle</code> mis põhineb <code>Problem</code> klassil | |||
* defineeri <code>actions</code> meetod | |||
def actions(self, state): | |||
space = state.index(0) | |||
row = space // 3 | |||
col = space % 3 | |||
actions = [] | |||
if row == 0: | |||
actions.append(state[space + 3]) | |||
elif row == 1: | |||
actions.append(state[space + 3]) | |||
actions.append(state[space - 3]) | |||
elif row == 2: | |||
actions.append(state[space - 3]) | |||
if col == 0: | |||
actions.append(state[space + 1]) | |||
elif col == 1: | |||
actions.append(state[space + 1]) | |||
actions.append(state[space - 1]) | |||
elif col == 2: | |||
actions.append(state[space - 1]) | |||
return actions | |||
* defineeri <code>result</code> meetod | |||
def result(self, state, action): | |||
newstate = list(state) | |||
klotsi_idx = newstate.index(action) | |||
space = newstate.index(0) | |||
newstate[space] = action | |||
newstate[klotsi_idx] = 0 | |||
return tuple(newstate) | |||
* kontrolli, et <code>goal_test</code> ja <code>path_cost</code> teevad õiget asjad | |||
* käivita mingi otsingualgoritm, vaata mis vastus tuleb | |||
problem = EightPuzzle(inistate, goal) | |||
goalnode = search.breadth_first_search(problem) | |||
print(goalnode.solution()) | |||
print(goalnode.path()) | |||
Viimane redaktsioon: 23. september 2017, kell 14:12
2. Nädal
Hulgateooria kordamine.
Algoritmide ja ülesannete keerukus: PDF.
Probleemi formuleerimine ja olekuruum. Lühendatud slaidid PDF; AIMA chapter 3.1-3.2, TI 4-5.
Videoloeng: olekuruum
Harjutustund
Eeldus: olemas on aima-python ja aima-data. Lahendame 8-Puzzle:
1 2 3 7 5 8 4 6
search.pysees on otsingu funktsioonid ja otsinguülesande formulatsioonProblemclass- oma töö jaoks uus fail
ht2.py
import search # ... siin tuleb problem defineerida asi = search.breadth_first_tree_search(problem)
- defineeri algolek ja lõppolek
inistate = (1,2,3,7,0,5,8,4,6) goal = (1,2,3,4,5,6,7,8,0)
- tee oma klass
EightPuzzlemis põhinebProblemklassil - defineeri
actionsmeetod
def actions(self, state):
space = state.index(0)
row = space // 3
col = space % 3
actions = []
if row == 0:
actions.append(state[space + 3])
elif row == 1:
actions.append(state[space + 3])
actions.append(state[space - 3])
elif row == 2:
actions.append(state[space - 3])
if col == 0:
actions.append(state[space + 1])
elif col == 1:
actions.append(state[space + 1])
actions.append(state[space - 1])
elif col == 2:
actions.append(state[space - 1])
return actions
- defineeri
resultmeetod
def result(self, state, action):
newstate = list(state)
klotsi_idx = newstate.index(action)
space = newstate.index(0)
newstate[space] = action
newstate[klotsi_idx] = 0
return tuple(newstate)
- kontrolli, et
goal_testjapath_costteevad õiget asjad - käivita mingi otsingualgoritm, vaata mis vastus tuleb
problem = EightPuzzle(inistate, goal) goalnode = search.breadth_first_search(problem) print(goalnode.solution()) print(goalnode.path())