Smash Vegas

Avatar

What is a monte carlo simulation?

With all this talk about pokerbots I’ve mentioned monte carlo simulations quite a lot.  But what are they?

They’re a method of computation which use repeated random sampling to produce results.  A simpler way of putting it would be “you try the thing lots of times to see what happens”.

A simple example would be calculating the probability of flipping two coins and getting two heads.  We know the probability is 1/2 * 1/2 = 1/4.  What if we couldn’t work that out?  A monte carlo method to calculate the probability would be to try the experiment many times, record the results and see what proportion of the time both were heads.  Obviously the more trials we perform, the more accurate a result we will get.  We will never get a perfectly accurate result but as the number of attempts increases, the result will get closer to 1/4.

This example probably seems silly because the answer is so obvious.  Where monte carlo methods come into their own are in problems which are too complex to calculate exactly.  This is why they are so crucial to pokerbots.

Consider a game of heads up holdem.  If we want to calculate our probability of winning a particular hand then we must compare it to all possible opponent hands.  Suppose we are preflop -

  • there are 50×49/2 = 1,225 possible opponent hands
  • there are 48×47x46×45x44/120 = 1,712,304 combinations of board card
  • that leaves us with 1,225×1,712,304 = 2,097,572,400 possibilities to consider

That’s not too bad for a modern computer to calculate, but this is the simplest case.  Things quickly get out of hand as the number of opponents increases - e.g. two opponents have 690,900 possible hand combinations while nine opponents have 6.2211×1020 (622 quintillion).

Much better would be to simulate the outcome of the hand many (10,000 - 1,000,000) times.  It is less computationally intensive and so will take less time.  100,000 trials will give us a winning percentage to an accuracy of several decimal places and we can adjust the number of trials to meet our desired accuracy.

Another advantage is that we can distribute the cards in the simulations in a non-random way.  Suppose we know that an opponent only goes to the flop 20% of the time, we could include this information in the simulation and only deal him the top 20% of hole cards.

Probably best to continue that train of thought in another post though.

Anatomy of a pokerbot - first steps in coding

Now that we have decided on our strategy it’s time to start coding the bot.  We’re going to use the default formulae, so I’d recommend you take a look at this.

We will need to be familiar with some of the symbols openholdem uses.  You can find a complete list here but these should suffice for now;

  • call tells us the current cost to call.
  • bet is the amount of a single bet for the current round.
  • pot is the current amount in the pot.
  • br is the current bet round (1=preflop,2=flop,3=turn,4=river).
  • prwin is our probability of winning the hand (estimated with a monte carlo simulation).
  • prtie is the probability of a tie for the win.
  • prlos is our probability of losing the hand.
  • callshort is the total amount which will be added to the pot if all players call.
  • raisshort is the total amount which will be added to the pot if all players still in the hand call one bet.  I.e. it is callshort + bet * (number of players still in the hand).

Let’s start with coding the formulae for the expected values of calling and raising; f$evcall and f$evrais.  Going with the strategy we decided last time we have;

##f$evcall##
0                         // start at 0
- call                    // my cost to call
+ (pot+call)           // the minimum possible pot
* (prwin+prtie/2)    // my chances of winning

[Read more]

Poker glossary for the winning player

To win at poker you need to think in the correct terms. Here are some concepts you should be familiar with.

Fundamental Theorem of Poker

The fundamental theorem of poker (as stated by David Sklansky) says that you gain whenever you play your hand the way you would if you could see your opponents’ cards or your opponent plays his cards differently from the way he would if he could see your cards.

Deception

There are really only two types of deception in poker - playing a strong hand weakly or a weak hand strongly.  Both may be tried on a single betting round (e.g. check-raise) or over several (e.g. slowplaying).  In either case you are attempting to make your opponent(s) commit an error under the fundamental theorem.

Pot Odds

Pot odds are the ratio of the amount of money in the pot to the size of bet required to stay in the pot. It is only rational to call a bet if your chance of winning is better than your pot odds. Put simply, would you pay $1 for a 50/50 chance to win $1? Of course not. How about for a 50/50 chance to win $10? Yes please. It would be rational to play that game as soon as the prize is greater than $2.

A skilled player will manipulate the pot odds to induce mistakes in opponents.  Say you think your opponent has a 25% chance of making a flush, by betting an amount which forces him to contribute more than a quarter of the final pot you put him in a difficult situation.  If he calls then he is in error and if he folds then you benefit.

[Read more]

Anatomy of a pokerbot - strategy

In this post we are going to design the logic we want our bot to follow.

Openholdem works by looking in turn at several functions which you design.  It stops to act on the first one which evaluates true.  First it checks the all in function, then the stated wager function, then the raise function, then the call function.  Since we are programming a limit bot we can ignore the first two (and make them always return false).  That leaves us with a raise function and a call function to design.

Our bot is going to play simple ABC poker.  We want it to evaluate the expected value of raising and calling, and then choose the greatest out of raising, calling and folding (the EV of folding is always 0).

A quick and dirty estimate of our EV of calling is:

(current pot + our call + other callers)*(our probability of winning) - (our call)

Similarly our estimate for the EV of raising is:

(current pot + our call + our bet + other callers)*(our probability of winning) - (our call + our bet)

Basically both cases are “what we could win multiplied by our chance of winning, minus what it costs us to call”.  They are analogous to looking at the pot odds and comparing them with our chance to win, but instead give us a numerical output of how much we expect to win or lose with a play in the long term.  These EV formulae are very basic, but later on we’ll look at ways to make them a bit better.

Next time we’ll look at how to code this as an Openholdem formula.

Anatomy of a pokerbot - blueprint

Over this series of posts I’m going to talk you through building a basic bot to play limit holdem. It won’t be a world beater, in fact it won’t even break even long term, but it should serve as a demonstration of the principals and a base to build on if you take up the hobby. I think you’ll be surprised how sophisticated a bot can be with very few lines of code.

Like building anything else a good plan helps immensely. We’re going to use a premade program to interface with our chosen poker site, so that we can concentrate on the logic the bot should follow.  The program in question is called Openholdem, and will take care of watching the poker site window to see what’s happening (screen scraping), evaluating the logic we have programmed and then pressing the relevant buttons in the poker window.  It will also provide us with many premade functions for evaluating the situation.  Certainly for our basic bot it’s all we’ll need.

Part of openholdem is called openscrape and is responsible for the screen scraping.  You provide it with a table map of the poker site which shows it where buttons and text are.  You can either download these table maps or create your own (it’s very easy but takes a little time and patience).  Openholdem also has the ability to import data from Pokertracker if you have it installed.  It’s easy to do but beyond the scope of this little project.

I would be remiss if I didn’t mention a topic of utmost importance to botting - stealth.  If you’re doing this for real then you need to use some clever tricks to prevent the poker window from knowing that it’s being operated by a bot.  The poker window can see what processes you’re running, where you’re clicking and gather all sorts of data on you to report back to the casino.  Since this is a basic tutorial I’m going to recommend “running naked” (as it’s known in the community) and using a disposable account.  Either pick a poker site you’re never going to use again or sign up for a play money only account.

Continue to Anatomy of a pokerbot - strategy.