/* Artificial Intelligence */ :-dynamic agenda/4. goalreached(f). successor(X,Y) :- piece_cost(X,Y,_). not(X) :- call(X), !, fail. not(_). eval(a,8). eval(b,7). eval(c,8). eval(d,5). eval(e,3). eval(f,1). eval(g,2). eval(h,0). cost([X],0). cost([X,Y|L],E):-piece_cost(Y,X,E2),cost([Y|L],E3),E is E2 + E3. piece_cost(a,b,3). piece_cost(a,d,5). piece_cost(b,c,1). piece_cost(b,d,2). piece_cost(d,e,2). piece_cost(d,g,3). piece_cost(e,f,2). piece_cost(e,g,1). piece_cost(g,h,2). astarsearch(Start,Goalpathlist):-cleandatabase, add_state(Start,[]),repeatifagenda, pick_best_state(State,Pathlist), add_successors(State,Pathlist), agenda(State,Goalpathlist,C,D), retract(agenda(State,Goalpathlist,C,D)), measurework. pick_best_state(State,Pathlist):- asserta(beststate(dummy,dummy,dummy)), agenda(S,SL,C,D),beststate(S2,SL2,D2),special_less_than(D,D2), retract(beststate(S2,SL2,D2)),asserta(beststate(S,SL,D)),fail. pick_best_state(State,Pathlist):-beststate(State,Pathlist,D), retract(beststate(State,Pathlist,D)),not(D=dummy),!. add_successors(State,Pathlist):-goalreached(State),!. add_successors(State,Pathlist):-successor(State,Newstate), add_state(Newstate,Pathlist),fail. add_successors(State,Pathlist):- retract(agenda(State,Pathlist,C,D)), asserta(usedstate(State,C)),fail. add_state(Newstate,Pathlist):-cost([Newstate|Pathlist],Cnew),!, agenda_check(Newstate,Cnew),!, usedstate_check(Newstate,Pathlist,Cnew),!, eval(Newstate,Enew),D is Enew+Cnew, asserta(agenda(Newstate,[Newstate|Pathlist],Cnew,D)),!. add_state(Newstate,Pathlist):- not(cost([Newstate|Pathlist],Cnew)), write('Warning:your cost function failed on path list'), write(Pathlist),nl,!. add_state(Newstate,Pathlist):-not(eval(Newstate,Enew)), write('Warning: your evaluation function failed on state'), write(Newstate),nl,!. agenda_check(S,C):-agenda(S,P2,C2,D2),C