Ifi6057w5

Allikas: Lambda
Redaktsioon seisuga 4. oktoober 2017, kell 11:37 kasutajalt PriitJ (arutelu | kaastöö) (→Harjutustund)
Mine navigeerimisribale Mine otsikasti

5. Nädal

Lokaalne otsing; AIMA chapter 4.1-4.1.1.

Lühendatud slaidid: PDF

N-Queens hill climbing otsinguga

Harjutustund

Heuristiline otsing.

Toorik koos testseisudega:

import search

class EightPuzzle(search.Problem):
    def actions(self, state):
        space = state.index(0)
        row = space // 3
        col = space % 3

        act = []
        if row == 0:
            act.append(state[space + 3])
        elif row == 1:
            act.append(state[space + 3])
            act.append(state[space - 3])
        elif row == 2:
            act.append(state[space - 3])
        if col == 0:
            act.append(state[space + 1])
        elif col == 1:
            act.append(state[space + 1])
            act.append(state[space - 1])
        elif col == 2:
            act.append(state[space - 1])
        return act

    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)



inistate = (1,2,3,7,0,5,8,4,6)
test1=(1,3,4,8,6,2,7,0,5)
test2=(2,8,1,0,4,3,7,6,5)
test3=(2,8,1,4,6,3,0,7,5)
test4=(5,6,7,4,0,8,3,2,1)

goal = (1,2,3,4,5,6,7,8,0)
#problem = EightPuzzle(inistate, goal)
problem = EightPuzzle(test4, goal)

#print(problem.actions(goal))
#print(problem.result(inistate, 7))

#asi = search.breadth_first_tree_search(problem)
asi = search.breadth_first_search(problem)
print(asi.solution())
#print(asi.path())