0

I have modified a generic alpha-beta from a book to be depth limited. When printing out the best position search results it sometimes works and sometimes I get a nuisance result such a the number 8.

This is a very generic alpha-beta from "prolog for artificial intelligence". I'm trying to narrow down to whether the issue is with the alpha-beta or somewhere else in my code.

Can someone please tell me if this depth limited alpha-beta seems ok?

Here is the code:

alphabeta(Pos, Alpha, Beta, GoodPos, Val, Depth):-
   Depth > 0,
   moves(Pos, PosList), !,
   boundedbest(PosList, Alpha, Beta, GoodPos, Val, Depth);
   get_pos_value(Pos,Val).                              % static value of position

boundedbest([Pos | PosList], Alpha, Beta, GoodPos, GoodVal, Depth):-
   NewDepth is Depth - 1,
   alphabeta(Pos, Alpha, Beta,_,Val, NewDepth),
   goodenough(PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal, Depth).

goodenough([],_,_,Pos, Val, Pos, Val,_):- !.                % no other candidate

goodenough(_,Alpha,Beta, Pos, Val, Pos, Val,_) :-
   min_to_move(Pos), Val > Beta, !;                 % Maximizer attainded upper bound
   max_to_move(Pos), Val < Alpha,!.                 % Minimizer attained lower bound

goodenough(PosList, Alpha, Beta, Pos, Val, GoodPos, GoodVal, Depth):-
   newbounds(Alpha, Beta, Pos, Val, NewAlpha, NewBeta), % refine bounds
   boundedbest(PosList, NewAlpha, NewBeta, Pos1, Val1, Depth),
   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):-               % Pos better than Pos1
   min_to_move(Pos), Val > Val1,!;
   max_to_move(Pos), Val < Val1,!.

betterof(_,_,Pos1,Val1,Pos1,Val1).                      % Otherwise Pos1 better
false
  • 10,264
  • 13
  • 101
  • 209
Benny Abramovici
  • 569
  • 1
  • 4
  • 20
  • I hate being a stickler but this is not a well-formulated SO question. You could either post this on Stack Exchange's "Code Review"; or you could debug your code, try to get as far as possible on your own, then reformulate your question so that it is possible to look into it without that much effort. At least, you should show how you run your code, say what result you expect exactly, what it is that is unexpected and so on. (BTW, a lot of unusually written code (cuts, semicolons...) I guess this was like this in the original code you took?) – TA_intern Oct 21 '20 at 06:00
  • Your code is incomplete: what is min_to_move & max_to_move ? To determine if it is necessary to cut e.g. in betterof, or goodenough 2nd clause. – peter.cyc Oct 21 '20 at 11:35
  • Min to move checks if it's black turns and max to move checks if its white turns. moves() matchs all the next legal board positions – Benny Abramovici Oct 21 '20 at 11:49

1 Answers1

0

Here's some comments on these predicates goodenough and betterof which are written in the same way. I suppose when Val > Val1 or Beta, you want min_to_move and in the case Val < Alpha or Val1, you want max_to_move.

goodenough(_,Alpha,Beta, Pos, Val, Pos, Val,_) :-
   min_to_move(Pos), Val > Beta, !;                 % Maximizer attainded upper bound
   max_to_move(Pos), Val < Alpha,!.                 % Minimizer attained lower bound

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

Perhaps more explicitly written:

goodenough(_,Alpha,Beta, Pos, Val, Pos, Val,_) :-
    Val > Beta, !,
    min_to_move(Pos).
goodenough(_,Alpha,Beta, Pos, Val, Pos, Val,_) :-
    Val < Alpha, !,
    max_to_move(Pos).

This code shows that there are cases not handled by your disjunction in goodenough, which interprets as fail. Is this intentional ?

In goodenough/8, if argument 5 Val is different from argument 7 Val, the predicate fails. Is this intentional ?

I have the impression you are optimizing the code before time!

peter.cyc
  • 1,763
  • 1
  • 12
  • 19
  • yes, thats how it shows in the book "prolog programming for artificial intelligence" I added the Depth argument to allow for shallower searches. – Benny Abramovici Oct 23 '20 at 05:50