1

Sorry if this is a silly question but I am confused. Negamax at the very beginning checks whether an end state or a maximum depth has been reached. You then insert a evaluation function which returns a negative or positive score for the state (one being good for one side and bad for the other and vice versa). What I find hard to get my head round is the negation below. Does that mean score returned is multiplied by -1? What does this achieve? I appreciate leaf sates 'bubble' back up alternating between minimum/maximum scores.

line: -NegaMax(c, depth+1, 1-color)

Charles
  • 50,943
  • 13
  • 104
  • 142

2 Answers2

4

This is used to flip perspective in games with alternating moves. In each state you want to have the calculate score according to current player (positive is good, negative is bad). When you look at some child state the opponent will move there, so negamax will return estimated score according to him. You need to negate it to get score of first player.

Example: in each state choose maximum of negated children: example

zch
  • 14,931
  • 2
  • 41
  • 49
  • But if we have two terminal states evaluated at -2 at -500 respectively from the parent node state looking at it from a minimising perspective you would chose between 2 and 500 (multiplication by -1) and choose 500 which is fine but if looking at it from a maximiser perspective you would choose 500 which isn't right?! I'm obviously missing something very obvious as negmax works correctly but if you can help me understand that would great. – user2976086 Nov 10 '13 at 19:01
  • @user2976086 Idea of negamax is that both players are maximizers, but one of them maximizes negated score. If we have state in which player that should move loses it will have score `-infinity`, regardless of which player it is. Negamax will always chose such a state, since it's negated score is `+infinity`. – zch Nov 10 '13 at 20:09
  • That makes a little more sense! So the evaluation function I create and put in negamax will return a score for states that aren't clear wins for either side and that score will be multiplied by player (-1 or +1). However if it is a clear win for either side just return minus infinity (don't multiply by player)? – user2976086 Nov 11 '13 at 11:25
  • @user2976086, in most games final states are losing (opponent just captured my king and won the game) so it's `-inf`, but sometimes they are winning (opponent just lost the game), so `+inf`. – zch Nov 11 '13 at 11:55
  • You previously said "...If we have state in which player that should move loses it will have score -infinity, regardless of which player it is..." – user2976086 Nov 11 '13 at 12:06
  • @user2976086, yes and if we have a state in which player that should move wins (winning state) it's `+inf`. – zch Nov 11 '13 at 12:08
  • I really appreciate you helping me here. If you agree, we can chat later today or tomorrow - I could send you short diagrams of node tree(s) showing exactly where confusion lies for me? Thanks for your patience. – user2976086 Nov 11 '13 at 13:02
  • I apologise if I am being totally stupid here but why has the white player chosen +4 over +inf ? Is he the minimizer? But I thought in Negamax both sides are maximizers?! – user2976086 Nov 11 '13 at 19:02
  • @user2976086, because it's his opponent's +4 - that's why he negates it before finding max. And -inf < -4. – zch Nov 11 '13 at 20:55
  • That makes complete sense! So logically if/when it flips perspective AFTER THAT the opposite happens: negative scores are returned and the negation turns these positive (and the greater one is selected)!? – user2976086 Nov 12 '13 at 08:25
0

I have no idea why you would be incrementing the depth. Negamax maximizes the current players position. When you preform a search you should call -negamax(position,depth-1). When make a move you wanna reverse the eval score on each move. For example, if its white to move then the eval should be regular, and if black is to move then the eval should score blacks pieces as positive values so if (turn == black) then eval = -eval. You do this becuase the negamax algo maximizes the players score.