#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
from linked_lists import LinkedList, Cons, Nil
from best_search import BestSearch
biggest = 99
start = LinkedList([(2, 2), (3, 1), (2, 3), (2, 1), (3, 3), (1, 2), (3, 2), (1, 3), (1, 1)])
goal = LinkedList([(1, 3), (2, 3), (3, 3), (1, 2), (2, 2), (3, 2), (1, 1), (2, 1), (3, 1)])
def is_goal(state):
return state == goal
def move_anyYC(numbers):
if numbers == Nil:
return
xb, yb = numbers.head
if xb > 1: # pohyb mezery doleva
xl = xb - 1
new_tail = replace((xl, yb), (xb, yb), numbers.tail)
yield (Cons((xl, yb), new_tail), 1)
if xb < 3: # pohyb mezery doprava
xr = xb + 1
new_tail = replace((xr, yb), (xb, yb), numbers.tail)
yield (Cons((xr, yb), new_tail), 1)
if yb > 1: # pohyb mezery dolu
yd = yb - 1
new_tail = replace((xb, yd), (xb, yb), numbers.tail)
yield (Cons((xb, yd), new_tail), 1)
if yb < 3: # pohyb mezery nahoru
yu = yb + 1
new_tail = replace((xb, yu), (xb, yb), numbers.tail)
yield (Cons((xb, yu), new_tail), 1)
def replace(x, y, xs):
if x == xs.head:
return Cons(y, xs.tail)
return Cons(xs.head, replace(x, y, xs.tail))
def h1(state):
a, b, c, d, e, f, g, h, i = state
return (a != (1, 3)) + (b != (2, 3)) + (c != (3, 3)) + \
(d != (1, 2)) + (e != (2, 2)) + (f != (3, 2)) + \
(g != (1, 1)) + (h != (2, 1)) + (i != (3, 1))
def h2(state):
a, b, c, d, e, f, g, h, i = state
return dist(a, (1, 3)) + dist(b, (2, 3)) + dist(c, (3, 3)) + \
dist(d, (1, 2)) + dist(e, (2, 2)) + dist(f, (3, 2)) + \
dist(g, (1, 1)) + dist(h, (2, 1)) + dist(i, (3, 1))
def dist(a, b): # manhattanska vzdalenost
xa, ya = a
xb, yb = b
return abs(xa-xb) + abs(ya-yb)
def writelist(xs):
writelist_(xs, 1)
def writelist_(xs, i):
if xs != Nil:
print("%d: %s" % (i, xs.head))
writelist_(xs.tail, i+1)
def length(xs):
if xs == Nil:
return 0
return 1 + length(xs.tail)
# demonstracni vypis
if __name__ == "__main__":
print("8-posunovacka, algoritmus A*\n")
print("Pocatecni stav: %s" % start)
print("\nNalezene reseni (heuristika h1):")
astar = BestSearch(biggest, is_goal, move_anyYC, h1)
solution = next(astar.search(start))
print("Prohledano %d stavu, vysledne reseni ma cenu %d." % (astar.total, length(solution)-1))
writelist(solution.reverse())
print("\nNalezene reseni (heuristika h2):")
astar = BestSearch(biggest, is_goal, move_anyYC, h2)
solution = next(astar.search(start))
print("Prohledano %d stavu, vysledne reseni ma cenu %d." % (astar.total, length(solution)-1))
writelist(solution.reverse())
8-posunovacka, algoritmus A*
Pocatecni stav: [(2, 2), (3, 1), (2, 3), (2, 1), (3, 3), (1, 2), (3, 2), (1, 3), (1, 1)]
Nalezene reseni (heuristika h1):
Prohledano 170244 stavu, vysledne reseni ma cenu 26.
1: [(2, 2), (3, 1), (2, 3), (2, 1), (3, 3), (1, 2), (3, 2), (1, 3), (1, 1)]
2: [(1, 2), (3, 1), (2, 3), (2, 1), (3, 3), (2, 2), (3, 2), (1, 3), (1, 1)]
3: [(1, 3), (3, 1), (2, 3), (2, 1), (3, 3), (2, 2), (3, 2), (1, 2), (1, 1)]
4: [(2, 3), (3, 1), (1, 3), (2, 1), (3, 3), (2, 2), (3, 2), (1, 2), (1, 1)]
5: [(2, 2), (3, 1), (1, 3), (2, 1), (3, 3), (2, 3), (3, 2), (1, 2), (1, 1)]
6: [(3, 2), (3, 1), (1, 3), (2, 1), (3, 3), (2, 3), (2, 2), (1, 2), (1, 1)]
7: [(3, 1), (3, 2), (1, 3), (2, 1), (3, 3), (2, 3), (2, 2), (1, 2), (1, 1)]
8: [(2, 1), (3, 2), (1, 3), (3, 1), (3, 3), (2, 3), (2, 2), (1, 2), (1, 1)]
9: [(1, 1), (3, 2), (1, 3), (3, 1), (3, 3), (2, 3), (2, 2), (1, 2), (2, 1)]
10: [(1, 2), (3, 2), (1, 3), (3, 1), (3, 3), (2, 3), (2, 2), (1, 1), (2, 1)]
11: [(2, 2), (3, 2), (1, 3), (3, 1), (3, 3), (2, 3), (1, 2), (1, 1), (2, 1)]
12: [(3, 2), (2, 2), (1, 3), (3, 1), (3, 3), (2, 3), (1, 2), (1, 1), (2, 1)]
13: [(3, 1), (2, 2), (1, 3), (3, 2), (3, 3), (2, 3), (1, 2), (1, 1), (2, 1)]
14: [(2, 1), (2, 2), (1, 3), (3, 2), (3, 3), (2, 3), (1, 2), (1, 1), (3, 1)]
15: [(1, 1), (2, 2), (1, 3), (3, 2), (3, 3), (2, 3), (1, 2), (2, 1), (3, 1)]
16: [(1, 2), (2, 2), (1, 3), (3, 2), (3, 3), (2, 3), (1, 1), (2, 1), (3, 1)]
17: [(2, 2), (1, 2), (1, 3), (3, 2), (3, 3), (2, 3), (1, 1), (2, 1), (3, 1)]
18: [(3, 2), (1, 2), (1, 3), (2, 2), (3, 3), (2, 3), (1, 1), (2, 1), (3, 1)]
19: [(3, 3), (1, 2), (1, 3), (2, 2), (3, 2), (2, 3), (1, 1), (2, 1), (3, 1)]
20: [(2, 3), (1, 2), (1, 3), (2, 2), (3, 2), (3, 3), (1, 1), (2, 1), (3, 1)]
21: [(1, 3), (1, 2), (2, 3), (2, 2), (3, 2), (3, 3), (1, 1), (2, 1), (3, 1)]
22: [(1, 2), (1, 3), (2, 3), (2, 2), (3, 2), (3, 3), (1, 1), (2, 1), (3, 1)]
23: [(2, 2), (1, 3), (2, 3), (1, 2), (3, 2), (3, 3), (1, 1), (2, 1), (3, 1)]
24: [(3, 2), (1, 3), (2, 3), (1, 2), (2, 2), (3, 3), (1, 1), (2, 1), (3, 1)]
25: [(3, 3), (1, 3), (2, 3), (1, 2), (2, 2), (3, 2), (1, 1), (2, 1), (3, 1)]
26: [(2, 3), (1, 3), (3, 3), (1, 2), (2, 2), (3, 2), (1, 1), (2, 1), (3, 1)]
27: [(1, 3), (2, 3), (3, 3), (1, 2), (2, 2), (3, 2), (1, 1), (2, 1), (3, 1)]
Nalezene reseni (heuristika h2):
Prohledano 7087 stavu, vysledne reseni ma cenu 26.
1: [(2, 2), (3, 1), (2, 3), (2, 1), (3, 3), (1, 2), (3, 2), (1, 3), (1, 1)]
2: [(1, 2), (3, 1), (2, 3), (2, 1), (3, 3), (2, 2), (3, 2), (1, 3), (1, 1)]
3: [(1, 3), (3, 1), (2, 3), (2, 1), (3, 3), (2, 2), (3, 2), (1, 2), (1, 1)]
4: [(2, 3), (3, 1), (1, 3), (2, 1), (3, 3), (2, 2), (3, 2), (1, 2), (1, 1)]
5: [(2, 2), (3, 1), (1, 3), (2, 1), (3, 3), (2, 3), (3, 2), (1, 2), (1, 1)]
6: [(2, 1), (3, 1), (1, 3), (2, 2), (3, 3), (2, 3), (3, 2), (1, 2), (1, 1)]
7: [(1, 1), (3, 1), (1, 3), (2, 2), (3, 3), (2, 3), (3, 2), (1, 2), (2, 1)]
8: [(1, 2), (3, 1), (1, 3), (2, 2), (3, 3), (2, 3), (3, 2), (1, 1), (2, 1)]
9: [(2, 2), (3, 1), (1, 3), (1, 2), (3, 3), (2, 3), (3, 2), (1, 1), (2, 1)]
10: [(3, 2), (3, 1), (1, 3), (1, 2), (3, 3), (2, 3), (2, 2), (1, 1), (2, 1)]
11: [(3, 3), (3, 1), (1, 3), (1, 2), (3, 2), (2, 3), (2, 2), (1, 1), (2, 1)]
12: [(2, 3), (3, 1), (1, 3), (1, 2), (3, 2), (3, 3), (2, 2), (1, 1), (2, 1)]
13: [(1, 3), (3, 1), (2, 3), (1, 2), (3, 2), (3, 3), (2, 2), (1, 1), (2, 1)]
14: [(1, 2), (3, 1), (2, 3), (1, 3), (3, 2), (3, 3), (2, 2), (1, 1), (2, 1)]
15: [(2, 2), (3, 1), (2, 3), (1, 3), (3, 2), (3, 3), (1, 2), (1, 1), (2, 1)]
16: [(3, 2), (3, 1), (2, 3), (1, 3), (2, 2), (3, 3), (1, 2), (1, 1), (2, 1)]
17: [(3, 1), (3, 2), (2, 3), (1, 3), (2, 2), (3, 3), (1, 2), (1, 1), (2, 1)]
18: [(2, 1), (3, 2), (2, 3), (1, 3), (2, 2), (3, 3), (1, 2), (1, 1), (3, 1)]
19: [(2, 2), (3, 2), (2, 3), (1, 3), (2, 1), (3, 3), (1, 2), (1, 1), (3, 1)]
20: [(3, 2), (2, 2), (2, 3), (1, 3), (2, 1), (3, 3), (1, 2), (1, 1), (3, 1)]
21: [(3, 3), (2, 2), (2, 3), (1, 3), (2, 1), (3, 2), (1, 2), (1, 1), (3, 1)]
22: [(2, 3), (2, 2), (3, 3), (1, 3), (2, 1), (3, 2), (1, 2), (1, 1), (3, 1)]
23: [(2, 2), (2, 3), (3, 3), (1, 3), (2, 1), (3, 2), (1, 2), (1, 1), (3, 1)]
24: [(2, 1), (2, 3), (3, 3), (1, 3), (2, 2), (3, 2), (1, 2), (1, 1), (3, 1)]
25: [(1, 1), (2, 3), (3, 3), (1, 3), (2, 2), (3, 2), (1, 2), (2, 1), (3, 1)]
26: [(1, 2), (2, 3), (3, 3), (1, 3), (2, 2), (3, 2), (1, 1), (2, 1), (3, 1)]
27: [(1, 3), (2, 3), (3, 3), (1, 2), (2, 2), (3, 2), (1, 1), (2, 1), (3, 1)]
|