Advertisement

alpha-beta prunning

Started by June 25, 2005 08:21 AM
12 comments, last by RPGeezus 19 years, 4 months ago
Hi! I am programming a nine men morris AI. I have neary finished, but sometimes the computer do inrational things. I think something is wrong with my alpha-beta prunning. Could you have a look at it?


#define ALPHA_MIN -200000 //the starting alpha value
#define BETA_MAX -ALPHA_MIN //the starting beta value

#define LOST -10000 //score for loosing

long COMPUTER::Search(int depth, long alpha, long beta, MOVE *bestmove)
{
  /*
  detph- if it is 0 we must stop
  alpha- beta
  bestmove if it is not NULL it should return the best move
  */

  LIST_MOVE *moves, *t; //linked list of possible moves
  long val, best_val=ALPHA_MIN;
  
  if(depth==0)
    {
    val=Evaluate(); //evalute the board
    return val;
    }
      
  //bord is a global class
  moves=board->GetPossibleMoves();
  if(!moves) //we can not move, we have lost
    return LOST;
  
  //stop is a global variable
  while(moves && !stop)
    {
    board->MakeMove(moves->move);
    
    val=-Search(depth-1,-beta,-alpha,NULL);
    
    board->UndoMove();
    
    if(val>best_val)
      {
      best_val=val;
      if(bestmove)
        *bestmove=moves->move;
      }
    
    if(best_val>alpha)
      alpha=best_val;
    
    if(alpha>=beta)
      {
      //free the linked list
      while(moves)
       {
       t=moves;
       moves=moves->next;
       delete t;
       }

      return alpha;
      }
    
    //change to the next move
    t=moves;
    moves=moves->next;
    delete t;
    }

  while(moves)
    {
    //free the list
    t=moves;
    moves=moves->next;
    delete t;
    }
  
  return best_val;
}



I hope you understand the code. I have never written an alpha-beta prunning before. Thank you for your help! Lutyo
Try taking the pruning out and see if it makes a difference.

Alpha beta pruning in a minimax tree-search just speeds up the search. It doesn't improve the results. If the game still makes odd moves after taking out the prune, then the problem's elsewhere.

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

Advertisement
it would be in the eval function.

from
nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Thanks for your help!
Quote: Original post by Nice Coder
it would be in the eval function.

from
nice coder
That's an astonishlingly quick and precise answer. How do you do it?

I can't remember the NMM game, but perhaps what it does that looks odd to you is actually some weird strategy that you've never seen before, and actually does make for a likely win in the end.
How does it play without the pruning, as John asked?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote: Original post by iMalc
Quote: Original post by Nice Coder
it would be in the eval function.

from
nice coder
That's an astonishlingly quick and precise answer. How do you do it?

I can't remember the NMM game, but perhaps what it does that looks odd to you is actually some weird strategy that you've never seen before, and actually does make for a likely win in the end.
How does it play without the pruning, as John asked?


After looking at the code, he seems to have implemented ab correctly, therefore the problem is in the evaluation function. Also, he sais that the computer sometimes does irrasional things. These are characterised by a bad evaluation function.

Be sure to see if it beats you tho. (because it might just be another stratagy.)

From,
Nice coder

Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Advertisement
Well, all I wanted to know if I had written a correct alpha-beta prunning. My Evaluate() function is not perfect. It scores 4 things:
1. tokens
2. number of full mills
3. possible moves
4. centers

These values are differences, I mean: current_player_score-opponent_score


It works perfect from the middle to the end. But when tokens are placed, the computer is not the best. Any more things to consider in the evaluation function?
Game-Tree's do not work so well when there is no clear objective.

The start of your game probably does not offer up anything for your game-tree to work towards, so it's making bad or poor decisions.

There are a few ways to improve this. Unfortunately there is no solution, at least non that I am aware of.

1. Add more terms to your evaluation function.
Add some positional information and other strategic information. This will help your program decide between two moves that do not result in a capture. For example, in Chess I might score points to a side if they place their Knights near the center of the board.


2. Create an opening book.
Let the computer pick from a list of moves and responses early on, instead of having to think about them. Almost every chess program does this-- my own program, ChessPig, has at least 100 different opening lines of play. This can really improve the strength of your program.


3. Increase the search depth.
The further ahead your program sees the less stupid it looks. Try adding 'killer moves' and 'move transposition' to your program. These two will give you a significant speed increase. Killer Moves are very easy to implement (should take you 10 minutes or less) and are well worth it.


Either way, congrats on getting your program working!

Will






------------------http://www.nentari.com
Quote: Original post by Lutyo
Well, all I wanted to know if I had written a correct alpha-beta prunning. My Evaluate() function is not perfect. It scores 4 things:
1. tokens
2. number of full mills
3. possible moves
4. centers

These values are differences, I mean: current_player_score-opponent_score


It works perfect from the middle to the end. But when tokens are placed, the computer is not the best. Any more things to consider in the evaluation function?


This, is your problem. (IIRC)

You should just use the player_value when its the players turn, or the opponants_score if its the opponants turn.

This is all iirc of cource, i haven't tried what you did, but i think it should cause the problem.
From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Quote: Original post by RPGeezus
1. Add more terms to your evaluation function.

I will think about it.

Quote: 2. Create an opening book.

What is it actually? The first few possible and reasonable moves? And how do you store it? I mean in what order or logic?

Quote: 3. Increase the search depth.

I have already done iterative deeping.

Anyway If you want to download the program:
http://picasso.nejanet.hu/~ludanyi/mill.zip 1.1 M
Or just the source code (without data):
http://picasso.nejanet.hu/~ludanyi/mill_src.zip 18Kb

It has not been finished yet, there are some errors, but it is playable.

Lutyo

This topic is closed to new replies.

Advertisement