Image Image Image




Post new topic Reply to topic  [ 19 posts ] 
Author Message
 Post subject: Anyone in need of some C# poker evaluators?
PostPosted: Wed Feb 18, 2009 12:55 am 
Offline
Regular member
User avatar

Posts: 81
Favourite Bot: ICM
Hello, I stumbled upon this page when I was (still am) in the progress of creating my own evaluator for Sökö (for those unfammiliar with this game its like 5 card stud with 4-straight and 4-flush added between one pair and twopairs). I always been a fan of publishing my code for comments and where could be better than here?

The code (c#) is implemented after a quick glance on the pokereval library but I've focused on portability and readability. I havn't made a solution for Sökö yet but it will come. Implemented evaluators are: Omaha, SevenCards (Holdem, 7 card stud) and FiveCard. Hi / Lo support is there for each game variant. It should be rally easy to port (especially the evaluationpart) my code into the language you prefer ranging from assembler to java as long as the language implements AND, OR and XOR in any form. I rely on about (a total) of 100kb of tables and a heavily optimized evaluator for each type of game. The code is at this moment not fully tested but it should do ok.

The code scales very well if you multithread it (almost by the factor of cores!).

Performance:
On my P4 2.4Ghz I can evaluate about 4.5 million of hands per second (Holdem) and 700000 Omaha Hi / Lo hands per second...

How to use: Check the Console application bundled within the project.

Cardformat:
52 bits, 0-12 clubs 2-A, 13-25 hearts 2-A, 26-38 spades 2-A, 39-51 diamonds 2-A

Evaluationresult:
STRAIGHTFLUSH bit 63, bit 0-12 the five cards, 5-A is only four bits and the ace is ommitted
FOUROFAKIND bit 62, bit 0-12 the kicker, bit 13-25 the fok card
FULLHOUSE bit 61, bit 0-12 the pair, bit 13-25 the trips
FLUSH bit 60, bit 0-12 the five cards
STRAIGHT bit 59, bit 0-12 the five cards, 5-A is only four bits and the ace is ommitted
THREEOFAKIND bit 58, bit 0-12 the kickers, bit 13-25 the trips
TWOPAIR bit 57, bit 0-12 the kicker, bit 13-25 the pairs
NOT IMPLEMENTED: FOURFLUSH bit 56, bit 0-12 the kicker, bit 13-25 the FF
NOT IMPLEMENTED: FOURSTRAIGHT bit 55, bit 0-12 the kicker, bit 13-25 the FS, A234 will have the A ommitted
ONEPAIR bit 54, bit 0-12 the kickers, bit 13-25 the pair
HIGHCARD bit 53, bit 0-12 the fivecards

Comments / Questions?


Attachments:
PokerEvaluators.zip [78.34 KB]
Downloaded 192 times
Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Wed Feb 18, 2009 3:08 pm 
Offline
New member
User avatar

Posts: 5
Favourite Bot: none yet
There seems to be something amiss with your evaluation (at least for holdem), I just can't put my finger on what. It displays more fullhouses than 3 of a kinds, that can't be right.

I found 2 small errors:
"ulong holecards = omaha_holecards[b];" in the consoleapplication code, you want to use the holdem_holecards here for the holdem evaluations I assume.
"ulong ranks = clubs | hearts | spades | clubs;" in SevenCards.cs and FiveCards.cs, the last clubs should be diamonds I guess.

Care to explain how you do the evaluation exactly? I coulnd't figure it out by just reading your comments.

Cheers,

S.


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Wed Feb 18, 2009 4:34 pm 
Offline
Regular member
User avatar

Posts: 81
Favourite Bot: ICM
Of course you are right when it comes to the obvious bugs (I may have zipped the wrong folder by mistake).

The evaluation is easy (holdem explained):
1. Count the bits of each flush, if they are five or more you have a flush or a straightflush (found in table)
2. Check if the number of bits in ranks are seven, then you have a straight or a highcard, make an extra check for straights by checking the flush/straight-table.
3. Count the bits within ranks and if it is:
6 bits you have a pair
5 bits you have two pairs or three of a kind
4 bits or less you have four of a kind, three pairs or full house

To get kickers for the extracted pair and so on I also have a table consisting of the three best bits for each combination of 13 bits.

Example on how to extract four of a kind (the easiest to understand):
If we have four dueces then bit 0 of clubs, hearts, diamonds and spades are all set to 1. From our boolean classes we know that 1 & 1 = 1 and that 0 & 1 = 0. So, to check for FOK you simply perform an and on all different flushes (fok = clubs & hearts & diamonds & spades) and if any bit is 1 in all of them you will end up with that bit int all other cases you will end up with a 0. If fok == 0 then we have no four of a kind and if fok == 2 then we have four threes..

When I get home from work I'll revisit my uploaded file and add a new one with clearer comments maybe I even explain the bitmagic that is happening when a pair, four of a kind and so on is extracted.


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Wed Feb 18, 2009 9:35 pm 
Offline
Regular member
User avatar

Posts: 81
Favourite Bot: ICM
So now I fixed the sourcecode, it did not work due to my mistake and I've also added a better (eh, more detailed) set of comments in the fivecard evaluator.


Attachments:
PokerEvaluators.zip [32.5 KB]
Downloaded 144 times
Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Thu Feb 19, 2009 11:00 am 
Offline
New member
User avatar

Posts: 5
Favourite Bot: none yet
Nice work! It seems to work well. Thanks for the extra comments. I'm still struggling with the bit operations, but it's making more and more sense.

Oh, and if you do the result check in sequence of highest probability, you can still get a 10% increase in speed:

Code:
if( ( max & APokerEvaluationBaseClass.ONEPAIR ) == APokerEvaluationBaseClass.ONEPAIR )
      resulttable_holdem[ 7 ]++;
else if( ( max & APokerEvaluationBaseClass.TWOPAIR ) == APokerEvaluationBaseClass.TWOPAIR )
      resulttable_holdem[ 6 ]++;
else if( ( max & APokerEvaluationBaseClass.HIGHCARD ) == APokerEvaluationBaseClass.HIGHCARD && !( ( max & APokerEvaluationBaseClass.FLUSH ) == APokerEvaluationBaseClass.FLUSH ) )
       resulttable_holdem[ 8 ]++;
else if( ( max & APokerEvaluationBaseClass.THREEOFAKIND ) == APokerEvaluationBaseClass.THREEOFAKIND )
       resulttable_holdem[ 5 ]++;
else if( ( max & APokerEvaluationBaseClass.FULLHOUSE ) == APokerEvaluationBaseClass.FULLHOUSE )
       resulttable_holdem[ 2 ]++;
else if( ( max & APokerEvaluationBaseClass.FOUROFAKIND ) == APokerEvaluationBaseClass.FOUROFAKIND )
       resulttable_holdem[ 1 ]++;
else if( ( max & APokerEvaluationBaseClass.STRAIGHTFLUSH ) == APokerEvaluationBaseClass.STRAIGHTFLUSH )
       resulttable_holdem[ 0 ]++;
else if( ( max & APokerEvaluationBaseClass.FLUSH ) == APokerEvaluationBaseClass.FLUSH )
       resulttable_holdem[ 3 ]++;
else if( ( max & APokerEvaluationBaseClass.STRAIGHT ) == APokerEvaluationBaseClass.STRAIGHT )
       resulttable_holdem[ 4 ]++;


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Thu Feb 19, 2009 11:09 am 
Offline
Regular member
User avatar

Posts: 81
Favourite Bot: ICM
Quote:
Oh, and if you do the result check in sequence of highest probability, you can still get a 10% increase in speed:


There is a small problem with that since you have to check for flush before you check for highcard and straightflush before you check for straight. The reason is how the lookuptable is built and I never remove the bits representing highcard or straight in flush and straightflush situations. That is an easy fix but it will cost in performance.

* EDIT *
Saw now that you've made adjustements in your code for that, sorry.. But still it's easier to mask them bits away in the evaluator code...
* / EDIT *


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Fri Feb 20, 2009 7:01 pm 
Offline
Regular member
User avatar

Posts: 64
Location: Edinburgh, Scotland flounderhead@gmail.com
Favourite Bot: Kryten
I've recently ported the RayW LUT hand evaluator (cmp. viewtopic.php?f=3&t=16) to c#. Using unsafe code it actually for some reasons runs a little bit faster than the c version, though this could well be a measuring error.
Evaluates around 250 million hands/s on my 2.4 GHz Core but needs the 120MB lookup table.


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Fri Feb 20, 2009 7:15 pm 
Offline
PokerAI fellow
User avatar

Posts: 7731
Favourite Bot: V12
flounderhead wrote:
I've recently ported the RayW LUT hand evaluator to c#.

You should attach the source code of your port.

_________________
indiana


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Fri Feb 20, 2009 7:29 pm 
Offline
Regular member
User avatar

Posts: 64
Location: Edinburgh, Scotland flounderhead@gmail.com
Favourite Bot: Kryten
indiana wrote:
You should attach the source code of your port.

Well, just in case there's interest, I attached the source. (w/o lookup table because it's too big, it's the same as the normal RayW LUT) The port is trivial so nothing exciting here...


Attachments:
Evaluator.zip [7.95 KB]
Downloaded 283 times
Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Fri Feb 20, 2009 8:27 pm 
Offline
Regular member
User avatar

Posts: 81
Favourite Bot: ICM
I'm not really interesting in passing through big numbers of evaluated per second with my code, I am mostly fascinated by the problemsolving part. Also evaluating many hands doesn't really improve the mathematical analysis by that much, often you only affect the fourth or fifth decimal after a certain number of evaluated hands. Many percentages are accepted by the pros based on so little as 50-100 thousands of hands played and that is easily beaten even with my set of classes.

And now when my evaluator-classes are finished (or at least I hope they are, feel free to check them out) the next goal is to build some kind of intelligence to go with the classes. I aiming for a NN-bot bot I'll start off with a rule based bot, then move to screenscraping (and possibly some OCR), then move back to the intelligence.

BTW I've added evaluators for Badugi (lower result is better) (lousy implementation just to have it), Soko (higher result is better) and Razz (lower result is better) (not the greatest of implementations but it does the job and not to slow) to the evaluator but I havn't had time to verify them fully.


Attachments:
PokerEvaluators.zip [52.69 KB]
Downloaded 230 times
Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Fri Feb 12, 2010 5:52 am 
Offline
Regular member
User avatar

Posts: 57
Favourite Bot: ShankyBonusbotHoldem
Hello everyone.

For 16 hours I have tried to find a bug in my hand evaluator for a specific poker game.

I used the FiveCards.cs evaluator and I think I found a bug?
I am not really sure though since my mind is pretty messed up.

Anyway, when I supply this hand (0 = Ace, 12 = king):
0,12,11,10,9

I do not get a straight? According to the problem my hand is worth 0 (highcard).

Where should I look to fix this problem?

Thanks


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Fri Feb 12, 2010 5:48 pm 
Offline
Senior member
User avatar

Posts: 356
Favourite Bot: OpenHoldem
RiddareRalf wrote:
Hello everyone.

For 16 hours I have tried to find a bug in my hand evaluator for a specific poker game.

I used the FiveCards.cs evaluator and I think I found a bug?
I am not really sure though since my mind is pretty messed up.

Anyway, when I supply this hand (0 = Ace, 12 = king):
0,12,11,10,9

I do not get a straight? According to the problem my hand is worth 0 (highcard).

Where should I look to fix this problem?

Thanks


My naive take on this is that it's not recognizing that 0 also equals 13. See what happens if you change the representation of an Ace from "0" to "13." Does it give you a straight? If this fixes your problem, it means you've got an error somewhere in how your straight finding function handles Aces.


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Fri Feb 12, 2010 11:05 pm 
Offline
Senior member
User avatar

Posts: 360
Favourite Bot: Zander
Panik wrote:
My naive take on this is that it's not recognizing that 0 also equals 13. See what happens if you change the representation of an Ace from "0" to "13." Does it give you a straight? If this fixes your problem, it means you've got an error somewhere in how your straight finding function handles Aces.
+1

Aces need special handling because they have dual value: TJQKA *and* A2345.

_________________
http://www.bespokebots.com


Top
 Profile  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Sat Feb 13, 2010 2:47 am 
Offline
Regular member
User avatar

Posts: 57
Favourite Bot: ShankyBonusbotHoldem
Oh, and another bug that is linked to the other one.
A,A is counted as the lowest pair.

Do you think there might be other bugs that is similar to this?
Any other conditions?

Thanks for your response earlier, I had to fix this manually by adding un-optimized code.
But since the evaluator already is so fast there is no problem.


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Sat Feb 13, 2010 6:07 am 
Offline
Regular member
User avatar

Posts: 57
Favourite Bot: ShankyBonusbotHoldem
Just random spam by my part.

If anyone in the future decides to battle hell, then another interesting part is that:
0, 9, 10, 11, 12 == Straight flush
13(Ace from another colour)
9, 10, 11, 12, 13 == Highcard

Cheers


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Sun Feb 21, 2010 6:42 am 
Offline
Regular member
User avatar

Posts: 89
Favourite Bot: tbd
Thanks for the generous contribution. I'm looking forward to giving this a spin when I have a bit of time.


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Thu Mar 10, 2011 9:57 pm 
Offline
Regular member
User avatar

Posts: 69
Favourite Bot: own
flounderhead wrote:
I've recently ported the RayW LUT hand evaluator (cmp. viewtopic.php?f=3&t=16) to c#. Using unsafe code it actually for some reasons runs a little bit faster than the c version, though this could well be a measuring error.
Evaluates around 250 million hands/s on my 2.4 GHz Core but needs the 120MB lookup table.

Strange on my second system the java version gets 319 and the C# port only 175 million hands/s. :xx16
Has anyone else compared the java and c# version with the same testcode?


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Sat Mar 12, 2011 9:36 am 
Offline
Regular member
User avatar

Posts: 69
Favourite Bot: own
Nvm. After removing the "unsafe" code parts and some small adjustments in the c# version it is now also around 310 mio.

Anyone experimented with a multicore version? Would be interesting where the bottleneck of data transfer from ram fully occurs.
A primitiv testimplementation for this calculates around 700 million hands/s on my 4 core system.


Top
 Profile E-mail  
 
 Post subject: Re: Anyone in need of some C# poker evaluators?
PostPosted: Tue May 24, 2011 9:43 am 
Offline
New member
User avatar

Posts: 1
Favourite Bot: none
Could anyone tell me how to create the look up tables? This "trivial port" is hard for me.


Top
 Profile E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ] 


Who is online

Users browsing this forum: Google and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: