def getcanmove(block): ## 경계노드 출력 함수 canmove = { "Red": [], "Blue": [], "Yellow": [], "Green": [], } for i in block: canmove[i.color] = RushHour.canmove(i) print("This was frontier node") print(canmove) return canmove
def test3(self): rh = RushHour.RushHour(self) rh.__nbCars = 12 rh.__horiz = [ True, False, True, False, False, True, False, True, False, True, False, True ] rh.__len = [2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 3] rh.__moveon = [2, 2, 0, 0, 3, 1, 1, 3, 0, 4, 5, 5] s = State([1, 0, 3, 1, 1, 4, 3, 4, 4, 2, 4, 1], rh) s2 = State([1, 0, 3, 1, 1, 4, 3, 4, 4, 2, 4, 2], rh) print(len(rh.moves(s))) print(len(rh.moves(s2)))
def solve1(self): rh = RushHour.RushHour(self) rh.__nbCars = 8 rh.__color = [ "rouge", "vert clair", "violet", "orange", "vert", "bleu ciel", "jaune", "bleu" ] rh.__horiz = [True, True, False, False, True, True, False, False] rh.__len = [2, 2, 3, 2, 3, 2, 3, 3] rh.__moveon = [2, 0, 0, 0, 5, 4, 5, 3] s = State([1, 0, 1, 4, 2, 4, 0, 1], rh) s = rh.solve(s) rh.printSolution(s)
def solveAStar(self): rh = RushHour.RushHour(self) rh.__nbCars = 12 rh.__color = [ "rouge", "vert clair", "jaune", "orange", "violet clair", "bleu ciel", "rose", "violet", "vert", "noir", "beige", "bleu" ] rh.__horiz = [ True, False, True, False, False, True, False, True, False, True, False, True ] rh.__len = [2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 3] rh.__moveon = [2, 2, 0, 0, 3, 1, 1, 3, 0, 4, 5, 5] s = State([1, 0, 3, 1, 1, 4, 3, 4, 4, 2, 4, 1], rh) s = rh.solve(s) rh.printSolution(s)
def test4(self): rh = RushHour.RushHour(self) rh.__nbCars = 12 rh.__color = [ "rouge", "vert clair", "jaune", "orange", "violet clair", "bleu ciel", "rose", "violet", "vert", "noir", "beige", "bleu" ] rh.__horiz = [ True, False, True, False, False, True, False, True, False, True, False, True ] rh.__len = [2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 3] rh.__moveon = [2, 2, 0, 0, 3, 1, 1, 3, 0, 4, 5, 5] s = State([1, 0, 3, 1, 1, 4, 3, 4, 4, 2, 4, 1], rh) n = 0 s = rh.solve(s) while (s.__prev != None): n += 1 s = s.__prev print n
def test2(self): res = [[False, False, True, True, True, False], [False, True, True, False, True, False], [False, False, False, False, True, False], [False, True, True, False, True, True], [False, True, True, True, False, False], [False, True, False, False, False, True]] rh = RushHour.RushHour(self) rh.__nbCars = 8 rh.__horiz = [True, True, False, False, True, True, False, False] rh.__len = [2, 2, 3, 2, 3, 2, 3, 3] rh.__moveon = [2, 0, 0, 0, 5, 4, 5, 3] s = State([1, 0, 1, 4, 2, 4, 0, 1], rh) rh.initFree(s) b = True for i in xrange(6): for j in xrange(6): print(rh.free[i][j] + "\t") b = (b and (rh.free[i][j] == res[i][j])) print "\n" if (b): print "resultat correct" else: print "mauvais resultat"
def queryStack(blocklist, data): ## 문제 해결 함수 query = [] cmap = colors.ListedColormap( ['White', 'Red', 'Blue', 'Yellow', 'Green', 'Brown', 'Orange']) for i in range(0, blocklist[0].x1): ## 목표상태가 되도록 초기 스택 작성 query.append([]) query[i].append(blocklist[0].color) query[i].append("LEFT") while query is not None: if isGoalState(data): print("Goal State") break elif 1 < data[2][0] < 6: print("Can't Solve") break frontier = getcanmove(blocklist) if frontier.values() is None: print("Can't Solve") break dataset = [] if "LEFT" in frontier["Red"]: data = blocklist[0].move("LEFT", data) query.pop() print("Popped") print("Query now : ", query) else: values = [] index = [] color = [] scores = [] count = 0 for k in frontier.keys(): colnum = 0 for v in frontier[k]: dataset.append( RushHour.resetandmove(blocklist[coltonum(k)], v)) ## 휴리스틱 값 계산을 위한 상태 저장 colnum = colnum + 1 color.append(colnum) for n in range(0, len(dataset)): scores.append(getscore(dataset[n])) print("This was frontiers scores : ", scores) decision = max(scores) print("This was Selected scores : ", decision) for m in range(0, len(scores)): if scores[m] == decision: count = m for o in range(0, 4): index.append(sum(color[0:o + 1])) for q in range(0, len(index) - 1): if index[q] <= count + 1 <= index[q + 1]: break index.clear() for r in frontier.values(): for s in r: values.append(s) direction = values[count] query.append([]) query[-1].append(blocklist[q + 1].color) ##SelectedNode.color query[-1].append(direction) ##SelectedNode.direction print("Added Query : ", query[-1][-2], query[-1][-1]) query.pop() print("Popped") print("Query now : ", query) data = blocklist[q + 1].move(direction, data) plt.figure(figsize=(6, 6)) plt.pcolor(data[::-1], cmap=cmap, edgecolors='k', linewidths=3) plt.show()
def main(argv): defaultBoard = " o aa| o |xxo |ppp q| q| q" l1=[] board = RushHour('x',defaultBoard) if argv[0] == 'print': if len(argv) <=1: #board = Cars("x", defaultBoard) l1.append(board.boards(defaultBoard)) board.sequenceBoards(l1) else: board.argv = argv[1] l1.append(board.boards(board.argv)) board.sequenceBoards(l1) elif argv[0]=='done': if len(argv) <=1: board.argv = defaultBoard else: board.argv = argv[1] board.done(board.argv) elif argv[0]=='next': if len(argv) <= 1: board.argv = defaultBoard else: board.argv = argv[1] board.next(True,board.argv) elif argv[0]=='random': if len(argv) <= 1: board.argv = defaultBoard else: board.argv = argv[1] board.random() elif argv[0] == 'bfs': if len(argv) <= 1: board.argv = defaultBoard else: board.argv = argv[1] board.bfs() elif argv[0] == 'dfs': if len(argv) <= 1: board.argv = defaultBoard else: board.argv = argv[1] board.dfs() elif argv[0] == 'astar': if len(argv) <= 1: board.argv = defaultBoard else: board.argv = argv[1] board.astar()