Jump to content

  • Log In with Google      Sign In   
  • Create Account


minimax with alpha beta pruning implementation stuck.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
28 replies to this topic

#1 whitenose   Members   -  Reputation: 124

Like
0Likes
Like

Posted 08 October 2012 - 01:21 PM

Hello,
Im new to this so please escuse my mistakes.
I'm trying to implement a 9Men's Morris game and i'm using minimax with alpha-beta pruning finding BestMove for "computer".

The problem is that if depth >= 3 then getBestMove() returns invalid move (board) , meaning that
it's returning a move (board) from other team sometimes - and I don't know where i go wrong.

Is my algorithm implementation done right ?! What i'm missing ?
Please Help !!


Here is my Java code , and getBestMove() function :


private int getBestMove(board board, int depth, int alpha, int beta, boolean bIsMax)
{
   // is depth reached ? or do we have a winner ? (checkWinner() returns 0 if nobody is winning so far!)
   if (depth == 0 || board.checkWinner()!=0)
   {
	   // if depth reached, or we DO have a winner.
	   return board.evaluateBoard();
   }

  // is "computer" turn
  if (bIsMax)
  {
	 // generate all possible boards for "computer" (MAX).
	 ArrayList<board> boardsArray = board.generateNextPossibleBoards(bIsMax);
	
	  // loop thru all possible previously generated boards
	  for(board nextBoard: boardsArray)
	  {
		  // recurse
		  int score = Math.max(alpha, getBestMove(nextBoard, depth - 1, alpha, beta, !bIsMax));
		  nextBoard.showBoard("next-max[" + boardsArray.size() + "]");  // display to user current board.(debbuging purpose)
		
		  // found a better board ?
		  if ( score > alpha )
		  {
			  alpha = score;
			  chosenBoard = nextBoard; // save best board so far to "chosenBoard"
		  }
		
		  if(beta<=score) break; // prune
	  }
  
   // clear boards array when done.
   boardsArray.clear();
  
   // return alpha.
   return alpha;
  }
  else
  {
	   // generate all possible boards for "player" (MIN).
		ArrayList<board> boardsArray = board.generateNextPossibleBoards(bIsMax);
	  
	  // loop thru all these possible boards.
	  for(board nextBoard: boardsArray)
	  {
			// recurse
			beta = Math.min(beta, getBestMove(nextBoard, depth - 1, alpha, beta, !bIsMax));
			nextBoard.showBoard("next-min[" + boardsArray.size() + "]");
	  
			if(beta <= alpha) break; // prune.
	  }
  
   // clear boards array when done.
   boardsArray.clear();
  
   // return beta.
   return beta;
  }

}



and the calling of this method is:

short depth = 2;
	
	// get best move for "computer" on this phase
	getBestMove(simulationBoard, depth , Integer.MIN_VALUE+1, Integer.MAX_VALUE-1, true);

and this is the evaluation function :

// evaluate current board.
public int evaluateBoard()
{
  int score = 0;

  score = score + (checkWinner());			   // checkWinner() returns  -1000000 if player wins, +1000000 if computer wins.
  score = score + (checkMills()*1000);		  // checkMills() returns   -10000*mills_count if player has some mills formed,  or  +10000*mills_count otherwise.
  score = score + (checkBlockers()*500);	 // checkBlockers() returns  -1000*block_count if player has bloked some tokens,  or  +1000*block_count otherwise
  score = score + (checkFormers()*50);	   // etc.
  score = score + (checkTokenDifference()*10);
  return score;
}



and the generateNextPossibleBoards() function wich chooses "default" switch case here :


public ArrayList<board> generateNextPossibleBoards(boolean bIsMax)
{
  // copy current board
  board currentBoard  = new board(this);

  // instantiate a empty array.
  ArrayList<board> boardsArray = new ArrayList<board>();

  // current team is unknown.
  short team = -1;
  short game_status = -1;

  // update current team (can be "computer" or "human" player).
  if(bIsMax)
  {
   team = renderer.TEAM_OPPONENT;
   game_status = (short) renderer.gOpponent.gGameStatus;
  }
  else
  {
   team = renderer.TEAM_PLAYER;
   game_status = (short) renderer.gPlayer.gGameStatus;
  }

  // according to current player's (computer/human) game-status:
  switch(game_status)
  {
	...

   default:
  
	// generate a move (a board) and save this new board to boardsArray.
	// if no moves left, return all boards so far!
	for(short i=0;i<24;i++)
	{
	 // go thru all tokens in this board.
	 if(currentBoard.gNode[i].gOccupiedByTeam==team)
	 {
		 // check if any empty neighbours for this token
		 for(short n=0;n<4;n++)
		 {
			   // get neighborur node id
				 short id = currentBoard.gNode[i].gNeighbourId[n];
	  
				// check if neighbour is empty spot (empty node)
				if(id!=-1 && currentBoard.gNode[id].gOccupiedByTeam==renderer.TEAM_NOTEAM)
				{
					 // make move for this token to the empty neighbour found above.
					 currentBoard.makeMoveToken( i , id );
					 boardsArray.add(new board(currentBoard));
	  
					 // undo previous move
					currentBoard.undoMoveToken( id , i );
				}
		 }
	
	
	 }
	
	}
  
	break;
}//end switch.

  // return all possible boards so far.
  return boardsArray;
}


thak you.

P.S: sorry for the bad view of some of the code.
just could not succeded making it "java" (using [source lang="java"]) because it was "eating" my code and the functions were displayed
partially .

Edited by whitenose, 08 October 2012 - 01:25 PM.


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 10685

Like
0Likes
Like

Posted 08 October 2012 - 02:14 PM

I don't really know Java, but I don't see any obvious bugs in the code you posted.

It's easier to get things right by adopting the negamax convention (in every node positive score means good for the player to move, so you flip the sign of the result of the recursive call and treat every node as a max node).

#3 whitenose   Members   -  Reputation: 124

Like
0Likes
Like

Posted 08 October 2012 - 03:11 PM

I don't really know Java, but I don't see any obvious bugs in the code you posted.

It's easier to get things right by adopting the negamax convention (in every node positive score means good for the player to move, so you flip the sign of the result of the recursive call and treat every node as a max node).


Thank you for replying mr. alvaro.
The algorithm should be the same, and it looks ok to me so far also ... but i still dont get it Posted Image
Why it returns some invalid moves when depth is greater or equal than 3 (i.e depth >= 3) ??

With depth>2 the algorithm should "see" ahead further moves and return them.
I only get moves from "player"'s team (not the "computer" team as it should).

But with depth = 1 or 2 all is just Fine ! (it returns best board/move from all of possible boards'/moves)
But this has a disadvantage: it cannot anticipate further moves of the "player" . Only the "power" of the evaluateBoard() function is used then (a local board evaluation).

Edited by whitenose, 08 October 2012 - 03:14 PM.


#4 Álvaro   Crossbones+   -  Reputation: 10685

Like
0Likes
Like

Posted 08 October 2012 - 03:15 PM

You have a bug, but it is not obvious to me what it is, from the code posted here and my understanding of Java. It is perfectly possible that your bug is in some other part of the code. I'm afraid you are going to have to debug it yourself. Find a position where the problem occurs and try to step through the code with a debugger. Unfortunately, recursive code is notoriously confusing to debug unless you have experience.

#5 Druzil   Members   -  Reputation: 510

Like
0Likes
Like

Posted 08 October 2012 - 03:36 PM

when adding features to a minimax search I've found it useful to compare the output of the newly enhanced search with the output of the basic search (in this case the pure minimax). For most new features the results should be the same (not always, in rare cases of transposition tables, obviously things like razoring will change the output).

If that checks out then you need to step through (depth three shouldn't be too hard for this) maybe with a contrived starting position.Remember that your fist branch should return at least some value (not an invalid board), so your step iteration will only be 2 + branching factor to at least see the first leaf branch return something.

#6 Druzil   Members   -  Reputation: 510

Like
0Likes
Like

Posted 08 October 2012 - 03:43 PM

I think I see the problem in the code. The chosenBoard looks like a global variable, and it will be overwritten at a lower level from a future cutoff from the current 'best board'. Avoid global variables in recursive code, rather return the best board and let the parent decide what to do with it.

#7 Álvaro   Crossbones+   -  Reputation: 10685

Like
0Likes
Like

Posted 08 October 2012 - 03:44 PM

Wow, I just noticed something crazy in your code. What are you calling showBoard in the middle of the search?

EDIT: If you expect that code to only be executed at the root, but it turns out it's getting executed in internal nodes as well, the output will be very confusing. Perhaps that's what you are experiencing...

I recommend separating the search of the root of the tree into its own function. There is a little bit of code duplication, but there are enough differences to justify it (returning a move, reporting progress, iterative deepening, time control, looking up the opening book, clearing the hash tables or at least making them obsolete...).

Edited by alvaro, 08 October 2012 - 03:46 PM.


#8 whitenose   Members   -  Reputation: 124

Like
0Likes
Like

Posted 08 October 2012 - 10:06 PM

I think I see the problem in the code. The chosenBoard looks like a global variable, and it will be overwritten at a lower level from a future cutoff from the current 'best board'. Avoid global variables in recursive code, rather return the best board and let the parent decide what to do with it.


That is correct mr Druzil, and i think you are right !
I must return chosenBoard recursively (as a reference parameter) to the previously function call and decide what to do with it.
So far the chosenBoard is overwritten and the last value is always the value of "last best board" found at lower levels.

#9 whitenose   Members   -  Reputation: 124

Like
0Likes
Like

Posted 08 October 2012 - 10:12 PM

Wow, I just noticed something crazy in your code. What are you calling showBoard in the middle of the search?

EDIT: If you expect that code to only be executed at the root, but it turns out it's getting executed in internal nodes as well, the output will be very confusing. Perhaps that's what you are experiencing...

I recommend separating the search of the root of the tree into its own function. There is a little bit of code duplication, but there are enough differences to justify it (returning a move, reporting progress, iterative deepening, time control, looking up the opening book, clearing the hash tables or at least making them obsolete...).


showBoard() function only displays the current board to the debug window, it's just a debbugging function - i dont care about performance now :).
I was trying to see what happens when getBestMove() calls itself recursively - so i've made this function (wich doesn't modify the content of the board)

I will keep this in mind - separating the search of the root (level 0) into its own function.
But also must say that i'm thinking using negamax algorithm if i will not figure where i go wrong.... Posted Image

#10 Druzil   Members   -  Reputation: 510

Like
0Likes
Like

Posted 09 October 2012 - 03:42 PM

But also must say that i'm thinking using negamax algorithm if i will not figure where i go wrong.... Posted Image

certainly negamax is a simpler and cleaner algorithm. One thing to keep in mind is if you intend to reuse your algorithm for >2 player games or games with multisteps per play you won't be able to use negamax as there is an assumption built into the call that the next level down is your single opponent. if this is not the case then I would suggest moving to negamax will save you potential headaches later on

#11 Álvaro   Crossbones+   -  Reputation: 10685

Like
0Likes
Like

Posted 09 October 2012 - 04:25 PM

[deleted]

Edited by alvaro, 09 October 2012 - 04:28 PM.


#12 Álvaro   Crossbones+   -  Reputation: 10685

Like
0Likes
Like

Posted 09 October 2012 - 04:27 PM


But also must say that i'm thinking using negamax algorithm if i will not figure where i go wrong.... Posted Image

certainly negamax is a simpler and cleaner algorithm. One thing to keep in mind is if you intend to reuse your algorithm for >2 player games or games with multisteps per play you won't be able to use negamax as there is an assumption built into the call that the next level down is your single opponent. if this is not the case then I would suggest moving to negamax will save you potential headaches later on

It's not obvious how to use minimax in games with more than 2 players anyway. I would look into Monte Carlo Tree Search if you want to write engines for a larger class of games.

Edited by alvaro, 09 October 2012 - 04:27 PM.


#13 Emergent   Members   -  Reputation: 967

Like
0Likes
Like

Posted 09 October 2012 - 07:19 PM

It's not obvious how to use minimax in games with more than 2 players anyway.


It stops being minimax, but it is clear what the generalization is, right? You drop the zero-sum requirement, you give each player his own reward function (rather than having one be the negative of the other), and everybody does a MAX on his own function.

(Granted, this gets impractical for large numbers of players.)

How alpha-beta search generalizes is less clear to me. You can do a generic branch-and-bound, with upper and lower bounds for each player's reward (so you'd keep cutting chunks off a bounding cube), but there are other options; I don't know if the game structure lets you keep just one number instead of two per player, or if non--axis-aligned planes (I haven't thought this through) are useful. This has got to be standard, but I don't know.

#14 Álvaro   Crossbones+   -  Reputation: 10685

Like
0Likes
Like

Posted 09 October 2012 - 07:57 PM

I have thought multiple-player minimax a bit myself, and also how to extend minimax and alpha-beta to games with randomness (e.g., games with dice). You can think of games with randomness as having an extra player that doesn't have any preference for one result or another, so will pick a "move" at random.

I have never actually tried to implement any of it, but I think alpha-beta is going to be of very limited value in those more complicated scenarios.

MCTS on the other hand extends very naturally to multiplayer games and to games with randomness. You still need to evaluate the result of the game as one number (the utility) per player. The rest of the MCTS scheme works basically without modification from the 2-player deterministic version of the technique.

#15 Druzil   Members   -  Reputation: 510

Like
0Likes
Like

Posted 10 October 2012 - 04:06 PM

I have implementations of multiplayer minimax and there certainly are papers out there going into detail of the issues. The easiest algoriths are paranoid search which basically treats all opponents essentially as one (they are all out to get you) which is the most pessimistic view as apposed to maxn which is the most optmistic method (everyone just tried to max their own score).

#16 whitenose   Members   -  Reputation: 124

Like
0Likes
Like

Posted 11 October 2012 - 12:25 PM

Hello,

I'm so sorry for the late reply. I had a car accident, all it's ok...
Thanks for all your answers !

As i said first, i'm trying to do a simple 9Men's Morris game (for Android).
This game has 3 phases: PlacingTokens, PlayGame, EndGame as you well know.

I have switched to negaMax() but the problem persists, and i think I know where/when it happens (i will explain) but dont know how to fix it. Posted Image
The problem is returning final "bestMove" to the main calling function .

Had some time today to implemeent negaMax() and i hope didn't made mistakes - and it seems that at "depth=1" when PlacingTokens phase is active -> all it's OK. (meaning that the "computer" plays good enough).
The calling code for this looks like this now for negaMax():




// as you can see "depth=1" (MAX = "computer" turn !)
case renderer.GAMESTATUS_PLACETOKEN:
  
					 //PARAMETERS:   simulationBoard, depth, alpha, beta,  sign (or color wich wil alternate between +1 or -1)
					negaMax(simulationBoard, 1,		Integer.MIN_VALUE+1, Integer.MAX_VALUE-1,   1);

My "negaMax()" function looks like this:


private int negaMax(board board, int depth, int alpha, int beta, int sign)
{
		// if depth reached or we DO have a winner on "this" board
		if(depth<=0 || board.checkWinner(sign))
		{	
			// evaluateBoard() evaluates board from "sign"'s point of view and returns a positive or negative score.
			return sign*board.evaluateBoard(sign);  
		}
		
		int max = Integer.MIN_VALUE+1; // -VERY SMALL value.
		
		// generate all available possible moves
		ArrayList<board> boardsArray = board.generateNextPossibleBoards(sign);
	
		// loop thru all generated boards
		for(board nextBoard: boardsArray)
		{
			// recurse and store result to "x" .
			int x = -negaMax(nextBoard, depth-1, -beta, -alpha,  -sign); // sign alterantes between +1 & -1 (computer & human)
			
			// got a "better/best" move ?
			if (x>max)
			{
				// save the "best" move so far.
				max = x;
				chosenBoard = nextBoard;
				chosenBoard.showBoard("FOUND BEST MOVE");
			}
			
			// alpha-beta pruning
			if (x>alpha) alpha = x;
			if (alpha>=beta) return alpha;
		}

		// clear boards when done.
		boardsArray.clear();
		
		// return best move so far.
		return max;
	}


allright, and now the checkWinner(sign) method (wich returns true or false if a team is Winning)


public boolean checkWinner(int sign)
{
  board board = this;

  // ---------------------------------------------------------------------------------
  // 0. get current team
  // ---------------------------------------------------------------------------------
  int team = renderer.TEAM_NOTEAM;
  int opponent = renderer.TEAM_NOTEAM;

  if(sign==1)
  {
   // if "computer" turn
   team = renderer.TEAM_OPPONENT;
   opponent = renderer.TEAM_PLAYER;
  }
  else
  {
   // if "human" turn
   team = renderer.TEAM_PLAYER;
   opponent = renderer.TEAM_OPPONENT;
  }


  // ---------------------------------------------------------------------------------
  // 1. check if only 2 tokens (from opponent team!) are left  on current board.
  // ---------------------------------------------------------------------------------
  int counter = 0;

  for(short i=0;i<24;i++)
  {
   if(board.gNode[i].gOccupiedByTeam!=renderer.TEAM_NOTEAM && board.gNode[i].gOccupiedByTeam==opponent) counter++; // count opponent tokens
  }

  switch(team)
  {
   case renderer.TEAM_OPPONENT:
	if(counter<=2 && renderer.gPlayer.gTokensToPlaceOnTable==0)
	{
	 return true;  // opponent has won.
	}
	break;
   case renderer.TEAM_PLAYER:
	if(counter<=2 && renderer.gOpponent.gTokensToPlaceOnTable==0)
	{
	 return true; // player has won.
	}
	break;
  }


  // ---------------------------------------------------------------------------------
  // 2. all opponent tokens are blocked! (no more possible moves, all tokens are blocked)
  // ---------------------------------------------------------------------------------
  for(short i=0;i<24;i++)
  {
   // go thru all tokens in this board, if opponent is not blocked -> return false.
   if(board.gNode[i].gOccupiedByTeam==opponent)
   {
	// check if any empty neighbours for this token
	for(short n=0;n<4;n++)
	{
	 // get neighborur id
	 int id = board.gNode[i].gNeighbourId[n];
	
	 // check if neighbour is empty space.
	 if(id!=-1 && board.gNode[id].gOccupiedByTeam==renderer.TEAM_NOTEAM)
	 {
	  // still having empty spaces to move. (NOT BLOCKED!)
	  return false;
	 }
	}
   }
  }

  // opponent is BLOCKED! so this team is winning -> return true!
  return true;
}



and this is the evaluateBoard(sign) function wich evaluates current board from --> "sign" ("computer" or "human") point of view :


  // evaluate current board.
	public int evaluateBoard(int sign)
	{
		int score 		= 0;
		int formers 	= checkFormers(sign);	// if 2 tokens on same row/column has been found, then a former has been found.
		int blockers 	= checkBlockers(sign);	// if 1 token blocks a token form other team, then we have a blocker.  
		int mills 		= checkMills(sign);		// if a mill has been formed (3 tokens in a row/column), then we have a mill.
		int tkcount 	= checkTokenCount(sign);// count token (team - opponent) difference on this board.
		
		score = score + formers*250;			// multiplier is 10 here for the "formers"
		score = score + blockers*500;			// multiplier is 500 here for the "blockers"
		score = score + mills*1000;				// etc.
		score = score + tkcount*10;

		if(sign==1) // is it "computer" turn?
		{
			return -score; 	// return score for "computer"
		}
		else
		{
			return +score;	// return score for "human"
		}
		
	}



OK, now generating boards in 1st phase (PlacingTokens) is done like so:


public ArrayList<board> generateNextPossibleBoards(int sign)
	{
		// copy current board
		board currentBoard 	= new board(this);
		
		// instantiate a empty array.
		ArrayList<board> boardsArray = new ArrayList<board>();
		
		// current team is unknown.
		int team = -1;
		int opponent = -1;
		int game_status = -1;
		
		// update current team (can be "computer" or "human" player).
		if(sign==1)
		{
			team = renderer.TEAM_OPPONENT;
			opponent = renderer.TEAM_PLAYER;
			game_status = renderer.gOpponent.gGameStatus;
		}
		else
		{
			team = renderer.TEAM_PLAYER;
			opponent = renderer.TEAM_OPPONENT;
			game_status = renderer.gPlayer.gGameStatus;
		}
		
		// according to current player's (computer/human) game-status:
		switch(game_status)
		{	
			case renderer.GAMESTATUS_PLACETOKEN:
				// simulate a single place token (on this board) and save this new board to boardsArray.
				for(short i=0;i<24;i++)
				{
					// go thru all empty spaces on this board , and if not occupied by any token
					if(currentBoard.gNode[i].gOccupiedByTeam==renderer.TEAM_NOTEAM)
					{
						// place (a opponent) token to the empty neighbour found
						currentBoard.makePlaceToken( i , opponent ); // place a opponent token !!!

						boardsArray.add(new board(currentBoard));
						
						// undo previous move
						currentBoard.undoPlaceToken( i , renderer.TEAM_NOTEAM);
					}
				}
				
				break;
...
... return boardsArray; // return all generated boards.

OK , all it's good so far, the "computer" finds best moves and plays good against "human" .
--------------------------------------------------------------------------------------------------------------------------------------------------


The problem is when swithcing to phase2 -> i.e the "PlayGame" phase !
The code doesn't change EXCEPT generating "moves" when in "PlayGame" phase.
Now, generating "moves" is done like so:

public ArrayList<board> generateNextPossibleBoards(int sign)
	{
		// copy current board
		board currentBoard	 = new board(this);
	  
		// instantiate a empty array.
		ArrayList<board> boardsArray = new ArrayList<board>();
	  
		// current team is unknown.
		int team = -1;
		int opponent = -1;
		int game_status = -1;
	  
		// update current team (can be "computer" or "human" player).
		if(sign==1)
		{
			team = renderer.TEAM_OPPONENT;
			opponent = renderer.TEAM_PLAYER;
			game_status = renderer.gOpponent.gGameStatus;
		}
		else
		{
			team = renderer.TEAM_PLAYER;
			opponent = renderer.TEAM_OPPONENT;
			game_status = renderer.gPlayer.gGameStatus;
		}
	  
		// according to current player's (computer/human) game-status:
		switch(game_status)
		{
			 ....
			 ....
case renderer.GAMESTATUS_PLAYNORMAL:
  
	// generate a move (a board) and save this new board to boardsArray.
	// if no moves left, return all boards so far!
	for(short i=0;i<24;i++)
	{
	 // go thru all tokens in this board.
	 if(currentBoard.gNode[i].gOccupiedByTeam==team)
	 {
	  // check if any empty neighbours for this token
	  for(short n=0;n<4;n++)
	  {
	   // get neighborur id
	   int id = currentBoard.gNode[i].gNeighbourId[n];
	  
	   // check if neighbour is empty spot.
	   if(id!=-1 && currentBoard.gNode[id].gOccupiedByTeam==renderer.TEAM_NOTEAM)
	   {
		// make move for this token to the empty neighbour found above.
		currentBoard.makeMoveToken( i , id );
		boardsArray.add(new board(currentBoard));
	  
		// undo previous move
		currentBoard.undoMoveToken( id , i );
	   }
	  }
	
	
	 }
	
	}
  
	break; // end PLAYNORMAL.

....
}   // end function generateNextPossibleBoards()
	

I have done a picture for you to see what happens when depth>1 and why the chosenBoard is not correctly chosen.

http://imageshack.us...0/mistaker.jpg/

step1) As you can see, token from node=3 (depth=3) generates a new "move" to node=4 (depth=2).
step2) After that negaMax() recurses itself again and the token from node=4 generates again a new move to node=7(depth=1).
step3) Similar, from node=7 a token is generated again and set to node=8 (when negaMax() recurses again).

Now token from node=8 is selected as the "chosen" best move --> and return it to main function, right ? (because we have Win or good score)
But...when moving from starting node=3 to node=8 there's a problem (because node=8 token is "far" away from token=3)
This is what i must solve, how do i return best move when depth > 1 ?



Im sorry for the long post.
I hope you accept my appologies !

LE:
@alvaro : OK, ill try that and get back to you. THANKS!

Edited by whitenose, 11 October 2012 - 11:21 PM.


#17 Álvaro   Crossbones+   -  Reputation: 10685

Like
0Likes
Like

Posted 11 October 2012 - 02:31 PM

Please re-read this thing I wrote:

I recommend separating the search of the root of the tree into its own function. There is a little bit of code duplication, but there are enough differences to justify it (returning a move, reporting progress, iterative deepening, time control, looking up the opening book, clearing the hash tables or at least making them obsolete...).


NegaMax should return a score and a score only. Then write another function that looks at each move available at the current position on the board and calls NegaMax for each one of them. This function is really very similar to NegaMax but, among other differences, it returns a move.

#18 Emergent   Members   -  Reputation: 967

Like
1Likes
Like

Posted 14 October 2012 - 10:56 AM

I have implementations of multiplayer minimax and there certainly are papers out there going into detail of the issues. The easiest algoriths are paranoid search which basically treats all opponents essentially as one (they are all out to get you) which is the most pessimistic view as apposed to maxn which is the most optmistic method (everyone just tried to max their own score).


This is interesting. "Paranoid search" hadn't occurred to me.

The "paranoid" concept is cool for a couple reasons.

The first is that it turns an N-player game into N separate two-player games. And if your game is zero-sum across all the players, then each of these two-player games will also be zero-sum, and so easy to solve (in mixed strategies, when turns are simultaneous).

The second is that, with this solution concept, we can understand "how we got there." With Nash equilibrium, it's unclear how you actually arrive at an equilibrium, or which equilibrium you arrive it. You've got things like Condorcet dynamics and "fictitious play," but these are kind of unsatisfying; there's no great reason to expect players to use these mental processes.

One question to ask about "paranoid" solutions is how "consistent" they are. Are the solutions you get even Nash equilibria? I expect not. Whereas I think(?) that maxn solutions are Nash equilibria. (But if not -- do we care? Why do we want Nash equilibria in the first place?)

#19 whitenose   Members   -  Reputation: 124

Like
0Likes
Like

Posted 15 October 2012 - 09:15 AM

It seems to work now , final functions here:

negamax (returing a best score):
[source lang="java"]public int negamax(board board, int depth, int alpha, int beta, int sign){ if (depth == 0 || board.checkWinner(sign)) { return board.evaluateBoard(sign); } int max = Integer.MIN_VALUE+1; ArrayList<board> boardsArray = board.generateNextPossibleBoards(sign); for(board nextBoard: boardsArray) { int score = -negamax(nextBoard, depth-1, -beta, -alpha, -sign); if (score>max) max = score; if (score > alpha) alpha = score; if (score >= beta) return score; } return max;}[/source]

and function returing a move (helped by negamax() function) :

[source lang="java"] public board returnBestMove(board board, int depth, int sign) { board bestMove = null; int best = Integer.MIN_VALUE+1; ArrayList<board> boardsArray = board.generateNextPossibleBoards(sign); for(board nextBoard: boardsArray) { if (bestMove == null) bestMove = nextBoard; int score = -negamax(nextBoard, depth, Integer.MIN_VALUE+1, Integer.MAX_VALUE-1, sign); if (score > best) { bestMove = nextBoard; best = score; } } return bestMove; }[/source]


bestMove = it's a board also, when function returnBestMove() finishes I just compare bestMove board with initial Board and
identify wich token has been moved (from wich pos, to wich destination pos).


If i've made some mistakes - your suggestions will be highly appreciated !
Thanks .

#20 Álvaro   Crossbones+   -  Reputation: 10685

Like
0Likes
Like

Posted 15 October 2012 - 09:32 AM

int score = -negamax(nextBoard, depth, Integer.MIN_VALUE+1, Integer.MAX_VALUE-1, sign);


Following the general alpha-beta algorithm, that should actually be
int score = -negamax(nextBoard, depth, Integer.MIN_VALUE+1, -best, sign);

Other than that, the only thing I would have done differently is not having a variable `max' at all: You can simply remove all mentions of it and return alpha at the end instead.

EDIT: Wait, why are you passing a sign around?

Edited by alvaro, 15 October 2012 - 09:36 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS