Stáhnout: 7.2_15.py   Zobrazit: jednoduše   7.2_15.pl

#!/usr/bin/env python
# encoding=utf-8 (pep 0263)

from linked_lists import LinkedList, Nil

def alphabeta(pos, alpha, beta):
    poslist = moves(pos)
    if poslist == Nil:
        return (None, staticval(pos))
    return bounded_best(poslist, alpha, beta)

def bounded_best(poslist, alpha, beta):
    pos = poslist.head
    _, val = alphabeta(pos, alpha, beta)
    return good_enough(poslist.tail, alpha, beta, pos, val)

def good_enough(poslist, alpha, beta, pos, val):
    if poslist == Nil or (min_to_move(pos) and val > beta or \
       max_to_move(pos) and val < alpha):
        return (pos, val)
    new_alpha, new_beta = new_bounds(alpha, beta, pos, val)
    pos1, val1 = bounded_best(poslist, new_alpha, new_beta)
    return better_of(pos, val, pos1, val1)

def new_bounds(alpha, beta, pos, val):
    if min_to_move(pos) and val > alpha:
        return (val, beta)
    if max_to_move(pos) and val < beta:
        return (alpha, val)
    return (alpha, beta)

def better_of(pos0, val0, pos1, val1):
    if min_to_move(pos0) and val0 > val1 or max_to_move(pos0) and val0 < val1:
        return (pos0, val0)
    return (pos1, val1)

start = "root"
graph = dict(
    root=("max", LinkedList(["a1", "a2", "a3"])),
    a1=("min", LinkedList(["b1", "b2", "b3"])),
    a2=("min", LinkedList(["c1", "c2", "c3"])),
    a3=("min", LinkedList(["d1", "d2", "d3"])),
    b1=("max", Nil), b2=("max", Nil), b3=("max", Nil),
    c1=("max", Nil), c2=("max", Nil), c3=("max", Nil),
    d1=("max", Nil), d2=("max", Nil), d3=("max", Nil))

def moves(pos):
    # zavisi na resenem problemu
    return graph[pos][1]

def min_to_move(pos):
    return graph[pos][0] == "min"

def max_to_move(pos):
    return graph[pos][0] == "max"

staticvals = dict(
    b1=3, b2=12, b3=8,
    c1=2, c2=4, c3=6,
    d1=14, d2=5, d3=2)

def staticval(pos):
    # zavisi na resenem problemu
    if pos not in staticvals:
        raise ValueError("Uzel %s neni cilovy." % pos)
    return staticvals[pos]

# demonstracni vypis
if __name__ == "__main__":
    print("Minimax s alfa-beta prořezáváním - hra na jedno kolo\n")

    print("            root            ")
    print("      a1     a2     a3      ")
    print("b1 b2 b3  c1 c2 c3  d1 d2 d3\n")

    print(" 3 12  8   2  4  6  14  5  2\n")

    print("Vysledek volani alphabeta('root', 0, 9999): %s" % (alphabeta("root", 0, 9999),))

% nacteni:
/* ['7.2_15.pl']. */

alphabeta(Pos, Alpha, Beta, GoodPos, Val) :- moves( Pos, PosList), !,
    boundedbest( PosList, Alpha, Beta, GoodPos, Val);
    staticval(Pos, Val ). % Static value of Pos
    
boundedbest([Pos | PosList], Alpha, Beta, GoodPos, GoodVal) :-
    alphabeta(Pos, Alpha, Beta,_ , Val ),
    goodenough(PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal).
goodenough([],_,_, Pos, Val, Pos, Val) :- ! . % No other candidate
goodenough(_, Alpha, Beta, Pos, Val, Pos, Val) :-
    min_to_move(Pos), Val > Beta, ! % Maximizer attained upper bound
    ; 
    max_to_move(Pos), Val < Alpha, !. % Minimizer attained lower bound
goodenough(PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal) :-
    newbounds(Alpha, Beta, Pos, Val, NewAlpha, NewBeta), % Refine bounds
    boundedbest(PosList, NewAlpha, NewBeta, Pos1, Val1),
    betterof(Pos, Val, Pos1, Val1, GoodPos, GoodVal).
    
newbounds( Alpha, Beta, Pos, Val, Val, Beta) :-
    min_to_move( Pos), Val > Alpha, !. % Maximizer increased lower bound
newbounds( Alpha, Beta, Pos, Val, Alpha, Val) :-
    max_to_move( Pos), Val < Beta, !. % Minimizer decreased upper bound
newbounds( Alpha, Beta, _, _, Alpha, Beta). % Otherwise bounds unchanged

betterof(Pos, Val, _Pos1, Val1, Pos, Val) :- min_to_move( Pos), Val > Val1, !
    ; max_to_move( Pos), Val < Val1, !. % Pos better than Pos1
betterof(_,_, Pos1, Val1, Pos1, Val1). % Otherwise Pos1 better

 Stáhnout: 7.2_15.py   Zobrazit: jednoduše   7.2_15.pl