Image Image Image




Post new topic Reply to topic  [ 239 posts ]  Go to page 1, 2, 3, 4, 5 ... 12  Next
Author Message
 Post subject: Timmy's Poker Sim Library (PSim) Examples in C, Java, AutoIt
PostPosted: Sat Feb 09, 2008 4:15 am 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
Well, I'm going to make my simulation library open source. This started out as a static library for my own use, but I figured I could make it a DLL and help some folks out.

Anyway, here are the features.
-Written in C++
-Uses the poker-eval library for speed and low memory usage
-Exposes a raw hand rank method
-Includes enumeration functions and Monte Carlo functions
-Can tell you about your hand, such as pair, top pair, flush draw, can an opponent have a straight, etc
-Includes usage example in java
-Includes full source code, compiled DLL (for any real language), and compiled java DLL (uh, for java ;) )
-VERY simple interface
-Can be used for anything you want (although I would like if you gave me credit)

Here are usage examples to figure out what your odds of winning a showdown are for As 9d on a flop of 8c 2h Ks.
Code:
//C code:
SimResults r;
SimulateHand("As 9d     8c 2h Ks", &r);
std::cout << "This hand will beat " << (r.winSd * 100) << "% of opponents at showdown.\n";

//Java code:
SimResults r = new SimResults();
PSim.SimulateHand("As 9d     8c 2h Ks", r);
System.out.println("This hand will beat " + (r.winSd * 100) + "% of opponents at showdown.");

;AutoIt3 code - calling psim.dll
$simResultsStruct = "float win; float tie; float lose; float winSd; float tieSd; float loseSd; float d94; float d90; int evaluations"
$simResults = DllStructCreate($simResultsStruct)
$result = DllCall("psim.dll", "none", "SimulateHand", "str", "As 9d     8c 2h Ks", "ptr", DllStructGetPtr($simResults), "float", 0, "float", 1, "uint", 0)
MsgBox(0, "Simulation Results", "This hand will beat " & DllStructGetData($simResults, "winSd") * 100 & "% of opponents at showdown.")

Tell me that's not simple! As you can see, it interfaces all hands as text (hole cards, the board cards if any, all separated by spaces or tabs. This was natural for me, because I read dealer chat. If you don't like the test interface, it'd be pretty easy to change to the poker-eval hand interface.

Ok, so to get started, unzip and take a look at the psim.hpp file. It has most structures and functions commented pretty well. The SimResults structure and the SimulateHand() function are by far the two most important areas. Then hope on over to the java directory and fire up run.bat (you'll need java installed for this). Be aware that this'll run a benchmark, and may take a minute or two. Then, if you are a java programmer take a look at PSimTest.java. Everything you need to get started is there. If you use this for a bot you may want to rename the DLL (if you do change PSim.java line 28 too).

For C++ programmers I would recommend compiling the source as a static library. Note that you will have to link against the poker-eval code to do this. Just include psim.hpp in your program.

For AutoIt users, see the included example for information on calling psim.dll.

For programmers in other languages, just call the PSim.dll in the root folder (again, might want to rename the dll). The methods that are exported are all defined in psim.hpp. If you can't figure it out, let me know and I'll see if I can help.

I will probably be adding a lot to this in the future. I may add Alberta style positive potential stats in the future. I'm currently working on adding weight tables, but I'm not sure I'll release that to the public.

OK, please let me know what you guys think.


P.S.
Gc66 made a graphical interface in AutoHotKey. It may be a good way to get a feel for what PSim is capable of. You can get his script here. You'll need to copy PSim.dll into the same folder.
Attachment:
ahkinterface.PNG
ahkinterface.PNG [ 66.06 KB | Viewed 13825 times ]


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Sat Feb 09, 2008 4:15 am 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
Note that the DLL only exports the functions that take strings. The int interface only works for C++ and java right now, if someone really needs this for another language let me know.

March 2008 version 1.3
Fixed bug in GetHandState() not counting outs correctly for already strong hands.
Added IsStraightPossible and IsStraightDrawPossible to PostFlopState.
Added the hand state stuff into the java interface. I do believe it's complete now.

Update 28 February 2008 - version 1.2
Fixed bug in SimulateHandMulti() allowing opponents to share crads. (typical error of ~ 3%)
Removed the simulate draw function. Simulate hand already does 94%, 90%, and the current %.
Now including compiled DLL again.
Now including AutoIt example.

Update 10 February 2008 - version 1.1
Java stuff has its own package now (PSim)
All functions can be called with a string hand, or an int array hand
Added some more tests to the java sample.

Quote:
For the evaluation functions that take a hand, you can pass it in two different ways.
In either case, at least the first two cards must be present (your hole cards).

The first option would be to pass the hand as a string. Valid formats include "Ah Kh 9d 8s Th",
"AhKh9d8sTh", "AhKh 9d8sTh", etc... This would mean that the player holds Ah Kh while the board
contains 9d 8s Th.

The second option to pass cards is as an array of exactly 7 integers. Each integer represents a card as
0-12 2h-Ah, 13-25 2d-Ad, 26-38 2c-Ac, 39-51 2s-As, any other number means no card
The hand in the above example would look like: int hand[7] = {12, 11, 20, 45, 8, -1, -1};
Hands passed by integer will process slightly faster than hands passed by string (although both are very fast).
This way of passing hands is most useful for preforming enumeration outside of PSim.


Attachments:
psim.zip [303.57 KB]
Downloaded 2595 times
Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Sat Feb 09, 2008 10:00 am 
Offline
PokerAI fellow
User avatar

Posts: 7731
Favourite Bot: V12
Cool, my main remark is that better expose int[] APIs (i.e pass the hands with their ordinal values, not string). I myself convert the cards to ints immediately when I read them, and use that along. So if you want to implement HL function in java that use hand eval, you need to pass ints for speed. You can either expose the same functions with int[], or double-implement the string to int in java, and expose only the int methods.

I still have to give it a try.

Also the packaging structure is a bit plain now :-)

As for if Java is a real language - it's good for network/server progamming, it's good for embeded programming (isn't a pokerbot an embedded programming?), it's good for academic stuff, and it has some other goodies (no wonder .NET copied lots of concepts from Java). It's also portable, so whatever you do, threads, sockets, GUIs, anything will run on Linux with no modifications needed. IntelliJIdea, in comparison with MS VC 2005 is much better. On the other hand, Java is awful for GUIs (or at least for a GUI newbies like me in comparison to VC) and awful for win32/system programming.

_________________
indiana


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Sat Feb 09, 2008 3:17 pm 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
Yeah, I really figured that people would want an int interface. I just don't use that now myself, so that's why it's not there. I could probably do this pretty quick, so I might. The only way I would see the string interface as being an issue is if you're doing enumeration in java.

What do you mean by packaging structure?

As for the real language remark, I added that just to tweak you. I'd say it worked. :lol: I know that java is a real language, and it's the most popular.

On a serious note, I know that some people like java for embedded work, but I don't think that makes any sense. For most of the embedded work I've done it would be impossible anyway (you can't run a java VM with 8 bytes of RAM, AFAIK). For an embedded hand evaluator, I'd put it straight to logic, meaning you'd be using a hardware description language (VHDL or Verilog HDL are really the only choices).


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Sat Feb 09, 2008 4:36 pm 
Offline
PokerAI fellow
User avatar

Posts: 7731
Favourite Bot: V12
Timmy, have you looked at poker source / 121?

This is quite powerful already, but I never managed to compile it (the java wrappers). See bellow - what is the Java signature of SAIE, and the ative methods, for which I miss a windows dll.

As for "plain package sttructure" - I added that to tweak you :) I mean you Java classes now don't have a package, and you can't just add one in the java only, keeping the dll and the headers as is (while using classes without packages is not nice).

Is it possible to expose single method, evaluator of a single 7 cards hand? Yes, that means the enumeration is to be done in Java. Is some of the pokereval speed lost by doing this?

I already do some weird equity calculations for flop hand potential, e.g.
Code:
   
  outs = 0;
  for (int j = 0; j < 52; j++) if (losts2[j]>0) {
     outs += HandHelper.max(new double[]{0, (0.15 - losts[j]/possibleHands.length)}) / 0.15;
  }


so I need to be able to run proprietary enumerations.

On the other hand, for general purpose release, I think your approach is good, but consider comparison to pokersource ...
Code:
  /** Compute the subjective all-in equity of each player based on a
      belief distribution for each player's hands.  Typical usage is
      to fix one player's cards and allow the other players' cards to
      range over a distribution; however, it is valid for all players to
      have multiple possible hands.
      @param gameType One of Enumerate.GAME_HOLDEM, etc.
      @param nmatchups number of matchups to sample (if zero, full enumeration)
      [Note: matchups are counted before they are tested for feasibility, that
      is, whether they share cards.  So the total number of matchups that
      contribute to the SAIE estimate may be less than nmatchups.]
      @param noutcomes number of boards to sample (if zero, full enumeration)
      @param handDistribs the hand distribution belief vector for each player
      @param board bitmask of cards already dealt to board (can be zero)
      @param dead bitmask of cards that cannot appear in any hand or on
      the board (can be zero)
      @param ev output: ev[i] player i's all-in pot equity
      @param matchups output: map of {HandMatchup, MatchupOutcome}
      pairs, one for each matchup
  */
  public static void FlopGameSAIE(int gameType,
                                  int nmatchups, int noutcomes,
                                  BeliefVector[] handDistribs,
                                  long board, long dead,
                                  double ev[], Map matchups) {


Native methods used:

Code:
  // must match enum_game_t definitions in enumdefs.h
  public static int GAME_HOLDEM      = 0;
  public static int GAME_HOLDEM8   = 1;
  public static int GAME_OMAHA      = 2;
  public static int GAME_OMAHA8      = 3;
  public static int GAME_7STUD      = 4;
  public static int GAME_7STUD8      = 5;
  public static int GAME_7STUDNSQ   = 6;
  public static int GAME_RAZZ      = 7;
  public static int GAME_5DRAW      = 8;
  public static int GAME_5DRAW8      = 9;
  public static int GAME_5DRAWNSQ   = 10;
  public static int GAME_LOWBALL   = 11;
  public static int GAME_LOWBALL27   = 12;

  static {
    System.loadLibrary("poker");
    System.loadLibrary("pokerjni");
  }
  /**
     Compute all-in pot equity of each player's hand, either by complete
     enumeration of outcomes or by monte carlo sampling.
     @param gameType specifies game (holdem, omaha, etc)
     @param nsamples number of monte carlo samples; if 0, full enumeration
     @param pocketRanks pocketRanks[i][j] is rank of player i's jth card
     @param pocketSuits pocketSuits[i][j] is suit of player i's jth card
     @param boardRanks boardRanks[j] is rank of jth board card
     @param boardSuits boardSuits[j] is suit of jth board card
     @param deadRanks deadRanks[j] is rank of jth dead card
     @param deadSuits deadSuits[j] is suit of jth dead card
     @param ev output: ev[i] is pot equity of player i
   */
  public static native void PotEquity(int gameType,
                                      int nsamples,
                                      int[][] pocketRanks,
                                      int[][] pocketSuits,
                                      int[] boardRanks,
                                      int[] boardSuits,
                                      int[] deadRanks,
                                      int[] deadSuits,
                                      double[] ev);
 
  /**
     Compute all-in pot equity of each player's hand, either by complete
     enumeration of outcomes or by monte carlo sampling.
     @param gameType specifies game (holdem, omaha, etc)
     @param nsamples number of monte carlo samples; if 0, full enumeration
     @param pockets pockets[i] is bitmask of player i's cards
     @param board board is bitmask of board cards
     @param dead dead is bitmask of dead cards
     @param ev output: ev[i] is pot equity of player i
   */
  public static native void PotEquity(int gameType,
                                      int nsamples,
                                      long[] pockets,
                                      long board,
                                      long dead,
                                      double[] ev);


P.S. On the embedded stuff: Everything with less than 64Mb RAM I consider embedded device ;-)

_________________
indiana


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Sun Feb 10, 2008 7:57 am 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
Tweak me? :lol: You fail. :P Honesty, I don't know these things about java, so I went ahead and fixed it. It's now in a "PSim" package, the test imports it, the DLL has been changed, etc. Thanks for the heads up. If there is anything else I'm doing wrong/nonstandard let me know.

Anyway, to fix the package I had to move everything into another folder. To be organized I moved the DLL too, meaning you need to run java with a "-Djava.library.path=PSim" option, or move the DLL to the root folder. Is there a better or standard way of doing this?

On exposing a single evaluator function: RankHand() does that now, but takes a string (and can evaluate not only 7 cards, but 52 through 2, I believe). Currently this function is pretty much worthless because it uses a string to represent the hand. I'll look at overloading all the functions with an int array hand interface too. Then you should have no problem enumerating from inside java with low overhead. I'll try to post an update within a couple days.

What do you mean poker source / 121? poker-eval is part of poker source. It does not contain the high level functionality that you posted though (or I'm missing something). Can you post a link to what you're talking about?

As far as SAIE, being able to evaluate AK vs T9s (for example) isn't something I currently need to do (although it could be done easily enough). Evaluating on a weight table (like Loki or Poki uses) is something I have a need for, and will implement soon, however like I said above I don't know that I'd release that to the public.

In any case, I'll have an update soon with a java package (already coded) and an int array interface.


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Sun Feb 10, 2008 11:47 am 
Offline
PokerAI fellow
User avatar

Posts: 7731
Favourite Bot: V12
Hi,

Leaving the package psim is good. If someone wants other packages, he can then much more easily change it and recompile (as you noticed, the learning curve from no package to any package is not very small, and btw it's the same for java developers, they don't usually deal with jni). For java library path, yes, you have to set it (-Djava.library.path="./libs";) or you can not set it and have the dll in the root, or any folder in your path.

PokerSource: I think I mixed up the terms here, but here is what I meant. If you go to:
http://download.gna.org/pokersource/sources/
(this is the latest odebased for download, right?) you will see:
poker-eval-134.0.tar.gz
and
poker-eval-src-121.tar.gz
Now the last one has src in its filen ame, and and its last version is 121, hence I refered to this as pokersource/121, and to the other library as poker-eval. I think the poker-eval part in the first is newer, but what is missing is the java stuff, which is only in poker-eval-src-121. In there you find java.org. ... and the classes I pasted. As far as I know, poker-eval-src-121.tar.gz is the latest package that contain the java, although it's dated 2006 or so.

On SAIE, there might be different usages. It's used when you evaluate agaisnt opponent set of cards you believe he has (the belief vector). This is e.g. in nash equl. caluclations, or if you e.g. want to provide the exact same function you provided (give me my % chance that my hand is in top 94% at showdown) - but you want to do that not against random hand, but against hand in given range. Another is for luck reduction when analysing hand history.

_________________
indiana


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Sun Feb 10, 2008 6:35 pm 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
OK, I didn't realize that. In fact it makes no sense to me. Anyway, I grabbed 121 and the java part says:
poker-eval 121 wrote:
The PokerSource Java API gives Java programs the ability to use the fast poker hand evaluator and enumerators in the poker-eval C library. The Java layer also defines higher-level structures that aren't available in the C library and provides algorithms for working with these structures. The most significant concept available only in the Java layer is the probabilistic hand distribution. The packages provide algorithms for computing pot equity based on subjective belief probabilities of hand distributions.
So I'll take a better look at it for ideas, but it's not something that I can use.


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Sun Feb 10, 2008 7:35 pm 
Offline
PokerAI fellow
User avatar

Posts: 7731
Favourite Bot: V12
For me poker-eval-src is very interesting, but I never managed to compile that into the poker.dll and pokerjni.dll libraries, no MS VC project is included in there too.

_________________
indiana


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Mon Feb 11, 2008 1:54 am 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
OK, updated.

I'm a little disappointed in the speed from java. It looks like the call overhead takes about as long as it does to evaluate the hand. When enumerating in java, and calling the DLL to evaluate, I get about 6 million evaluations per second. When java doesn't do the enumeration I get well over 30 million eps.

Oh well, this library was made to allow an easy and fast way to calculate pot equity. The raw hand ranking method is just icing on the cake. ;)


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Thu Feb 28, 2008 12:01 am 
Offline
Senior member
User avatar

Posts: 131
It's incredibly generous of Timmy to make this available open-source. If I can just crack how to use it, its exactly what I need.

OK, I'm trying to figure out how to use this in AutoIT (Is this a 'real' language? Probably not). AutoIt allows you to call dlls, the basic syntax is:

DllCall ( "dll", "return type", "function" [, "type1", param1 [, "type n", param n]] )

Having played around with this for a while, I came to the conclusion that to make the function work I had to create a structure the same as SimResults to receive the values from the function. AutoIT has a function to do this:

DllStructCreate ( "Struct" [,Pointer] )

So, trying to put it all together, and looking at the info in psim.hpp, I've come up with the following code:

Code:
$str = "float win; float tie; float lose; float winSd; float tieSd; float loseSd;  float d94; float d90; long evaluations"
$result = DllStructCreate($str)

$test = DllCall ( "D:\My Documents\Poker\AutoIT\EvalDll\psim.dll", "ptr", "SimulateHand", "As 9d     8c 2h Ks", DllStructGetPtr($result))

MsgBox(0,"DllCall Error",@error)


It still doesn't work. The error value I get is 2, which the AutoIT help says means 'unknown "return type"'. I think my problem is that I don't really understand enough about how this is supposed to work, to figure out what I'm doing wrong.

Can anybody help? If you're interested, you can find the AutoIT help files here: http://www.autoitscript.com/autoit3/docs/functions/DllCall.htm It's a big ask, but I'm desperate! Many thanks.

_________________
http://www.pokerspreadsheets.com


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Thu Feb 28, 2008 2:19 am 
Offline
Senior member
User avatar

Posts: 131
OK, sorry to be a pain, slightly easier example. Let's go back to the basic AutoIT function:

DllCall ( "dll", "return type", "function" [, "type1", param1 [, "type n", param n]] )

Description of the function here: http://www.autoitscript.com/autoit3/docs/functions/DllCall.htm

The 'RankHand' function in Timmy's library should just deliver a number, correct? So here's my code:

Code:
$test = DllCall ( "D:\My Documents\Poker\AutoIT\EvalDll\psim.dll", "uint", 1, "str", "As9d8c2hKs")


(In this code, 1 = the ordinal for the "RankHand" function). When I run the code, AutoIT crashes. This is probably because I'm doing something wrong with one of the types, but I've tried all sorts of combinations and nothing helps.

If I can get this to work, I might be able to get more complex functions working. Can anyone help? Thanks!

_________________
http://www.pokerspreadsheets.com


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Thu Feb 28, 2008 6:16 am 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
Give this a try:

Code:
;PSim is a 32-bit dll, so you'll want to run this script with "Run Script (x86)".

$dll = DllOpen ("psim.dll") ;The path needs to be set here.

if $dll <> -1 then
   $hand = "AhKh Td5hJh";
   
   ;Test this out with RankHand, it just takes a string and returns an unsigned int. Easy.
   $result = DllCall($dll, "uint", "RankHand", "str", $hand)
   if @error <> 0 then
      MsgBox(0, "DllCall Error", @error)
   else
      MsgBox(0, "RankHand", "Your hand " & $hand & " ranks to " & $result[0] & ".");
   endif
   
   ;Now go for the real function: SimulateHand.
   $simResultsStruct = "float win; float tie; float lose; float winSd; float tieSd; float loseSd; float d94; float d90; int evaluations"
   $simResults = DllStructCreate($simResultsStruct)

   $result = DllCall($dll, "none", "SimulateHand", "str", $hand, "ptr", DllStructGetPtr($simResults), "float", 0, "float", 1, "uint", 0)

   if @error <> 0 then
      MsgBox(0, "DllCall Error", @error)
   else
      MsgBox(0, "Simulation Results", "Your hand " & $hand & " will win at showdown " & DllStructGetData($simResults, "winSd") * 100 & "% of the time.")
   endif
   
   DllClose($dll)
else
   MsgBox(0, "error", "Couldn't load the DLL.")
endif


If you have a 64-bit system, make sure to run AutoIt in 32-bit mode. Also, did you compile the psim.dll yourself, or are you using the one I posted the first time (I didn't do a DLL for the second update)?

Anyway, let me know if it works.


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Thu Feb 28, 2008 6:21 am 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
I forgot to mention, you said in your last post that the ordinal for RankHand was 1. If you're using the DLL I compiled I think it is 3 (because it's the third function in the file). I just want to make sure no one is confused there.


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Thu Feb 28, 2008 12:56 pm 
Offline
Senior member
User avatar

Posts: 131
Timmy,

I'm very grateful to you for looking at this for me. I feel like I'm really close, but its just outside my grasp. Because at the moment its not working. I used 'RankHand' the function as you wrote it, and I get a "DllCall error" of 3, which (according to the AutoIT help) means 'function not found in the DLL file'. If I substitute ordinal numbers AutoIt crashes.

My machine is running a 32-bit version of XP, so I don't think this is the problem.

Is it possible that I've got the wrong dll file? It was originally called psjw.dll and came as part of the psim.zip file attached to your 2nd post of 9 February. I couldn't see any others. It looks to me as though I've got the java dll. I think that you may have attached something to your original post, but if you did there is no file download attached to it now.

Many thanks once again for your help.

_________________
http://www.pokerspreadsheets.com


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Thu Feb 28, 2008 7:42 pm 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
You have the wrong file. psjw.dll is the java wrapper, and should only be used with java.

See, the first time I posted this I included a normal DLL, but the second time I didn't. Give me a bit and I'll update the OP with a normal DLL again.


EDIT. I just uploaded version 1.2 and it does include the DLL. I also included an AutoIt example.


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Thu Feb 28, 2008 10:16 pm 
Offline
Senior member
User avatar

Posts: 131
Brilliant! It works! This is going to save me soooo much time!

Timmy, you are a star! Thank you so much! One of the most generous acts I have ever encountered in poker circles.

_________________
http://www.pokerspreadsheets.com


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Thu Feb 28, 2008 11:33 pm 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
You're certainly welcome. Thanks for choosing my library. I'm glad that it may help.

A lot of the thanks should go to the people who wrote poker-eval. My library only makes their work a little easier to use and more accessible. I wrote the library for my own personal reasons, and it only cost me a little time to release it. It's no big deal really.


Top
 Profile  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Fri Feb 29, 2008 12:14 am 
Offline
Senior member
User avatar

Posts: 131
Can I ask one last thing? Advice on how to get the "GetHandState" function to work? I've added this code to the example you provided:

Code:
   $message = ""
   
   $PostFlopStateStruct = "ubyte UsesBoth; ubyte UsesOne; ubyte UsesNone; ubyte IsNoPair; ubyte IsOnePair; ubyte IsTwoPair; ubyte IsTrips; ubyte IsStraight; ubyte IsFlush; ubyte IsFullHouse; ubyte IsQuads; ubyte IsStFlush; ubyte IsTopPair; ubyte IsMiddlePair; ubyte IsBottomPair; ubyte IsOverPair; ubyte IsOverCards; ubyte IsFlushDrawPossible; ubyte IsFlushPossible; ubyte IsFullHousePossible; ubyte Is3Flush; ubyte Is4Flush; int FlushOuts; int StraightOuts; int BetterOuts"

   $PostFlopState = DllStructCreate($PostFlopStateStruct)

   $result = DllCall($dll, "none", "GetHandState", "str", $hand, "ptr", DllStructGetPtr($PostFlopState))
   
   if @error <> 0 then
      MsgBox(0, "DllCall Error", @error)
   else
     $message = $message & "IsOverCards = " & DllStructGetData($PostFlopState, "IsOverCards") & @LF
     $message = $message & "FlushOuts = " & DllStructGetData($PostFlopState, "FlushOuts")
      MsgBox(0, "Simulation Results", $message)
   endif


It just returns zeroes. I think the problem may be with the data types in $PostFlopStateStruct, but I've tried byte, ubyte, int, uint - same result, every time. In your C code most of the fields are bool.

Any ideas? Thanks again!

_________________
http://www.pokerspreadsheets.com


Top
 Profile E-mail  
 
 Post subject: Re: Timmy's Poker Simulation Library, with source and java inter
PostPosted: Fri Feb 29, 2008 1:25 am 
Offline
PokerAI fellow
User avatar

Posts: 686
Location: Midwest, USA
Favourite Bot: N/A
OK, this is going to be tricky, so bear with me. C is a really low level language, so it allows one to specify the size of variables. For the PostFlopState structure I made all the bools one bit, and the int only 4 bits. Back when I made this I was analyzing a lot of hands and wanted to store millions of these in memory, so it made a huge difference. If you count up all the bits in this structure you get 34 and that will get rounded up to 64 because it's a 32-bit program (originally I had it at 32-bits, but must have added the other 2 without thinking, now the whole thing is a little dumb. Oh well, lot's of free room to expand.)

So our structure is 64 bits total. In AutoIt we can just use $PostFlopState = DllStructCreate("uint a; uint b"). If AutoIt supported 1 and 4 bit sized integers this would be a lot easier. Now with this structure you can make the DLL function call just like you did before.

OK, so we've made the call and we're stuck with some awfully encoded 32-bit integers. Now to grab the information out we need to decode them. First, let's list the PostFlopState, and I'll comment where each member starts.

EDIT: These numbers are no longer valid for the current version.
Code:
struct PostFlopState
{
    bool UsesBoth: 1; //This will get stored in a bit 0
    bool UsesOne: 1; //This will get stored in a bit 1
    bool UsesNone: 1; //This will get stored in a bit 2
    bool IsNoPair: 1; //This will get stored in a bit 3
    bool IsOnePair: 1; //This will get stored in a bit 4
    bool IsTwoPair: 1; //This will get stored in a bit 5
    bool IsTrips: 1; //This will get stored in a bit 6
    bool IsStraight: 1; //This will get stored in a bit 7
    bool IsFlush: 1; //This will get stored in a bit 8
    bool IsFullHouse: 1; //This will get stored in a bit 9
    bool IsQuads: 1; //This will get stored in a bit 10
    bool IsStFlush: 1; //This will get stored in a bit 11
    bool IsTopPair: 1; //This will get stored in a bit 12
    bool IsMiddlePair: 1; //This will get stored in a bit 13
    bool IsBottomPair: 1; //This will get stored in a bit 14
    bool IsOverPair: 1; //This will get stored in a bit 15
    bool IsOverCards: 1; //This will get stored in a bit 16
    bool IsFlushDrawPossible: 1; //This will get stored in a bit 17
    bool IsFlushPossible: 1; //This will get stored in a bit 18
    bool IsFullHousePossible: 1; //This will get stored in a bit 19
    bool Is3Flush: 1; //This will get stored in a bit 20
    bool Is4Flush: 1; //This will get stored in a bit 21
    unsigned int FlushOuts: 4;  //This will get stored in a bit 22 through 25
    unsigned int StraightOuts: 4;  //This will get stored in a bit 26 through 29
    unsigned int BetterOuts: 4;  //This will get stored in a bit 30 through 32, and then b bit 0 through 1
};


So, if you're still with me, we now know where the information is, we just need to grab it. We can test a bit like so: BitAnd(DllStructGetData($PostFlopState, "a"), 2 ^ 0) will return 0 if bit 0 is 0. Another example: BitAnd(DllStructGetData($PostFlopState, "a"), 2 ^ 5) will return 0 if bit 5 is 0. The way this works is that you are making a binary number with a bit in the nth place set. So 2^5 would be 100000 in binary. 2^3 would be 1000 in binary. Then this number is ANDed against what were checking, and voilà, all bits are cleared except what we're checking.

So, I realize this is getting super clunky, but here is a complete example.
Code:
$PostFlopState = DllStructCreate("uint a; uint b")

$result = DllCall("psim.dll", "none", "GetHandState", "str", "8h Td  5c 6c 7c", "ptr", DllStructGetPtr($PostFlopState))
if BitAnd(DllStructGetData($PostFlopState, "a"), 2 ^ 16) then
   MsgBox(0, "state", "You have over cards!")
else
   MsgBox(0, "state", "You do not have over cards.")
endif

MsgBox(0, "state", "Straight outs: " & BitShift(BitAnd(DllStructGetData($PostFlopState, "a"), 2^26 + 2^27 + 2^28 + 2^29), 26))

$result = DllCall("psim.dll", "str", "GetHandStateBrief", "ptr", DllStructGetPtr($PostFlopState))
MsgBox(0, "state brief", $result[0])


Did you see the trickery for the 4 bit value of the straight outs? First block out everything other than bits 26 through 29. Then shift the int down so the LSBs line up. Finally, the better outs is half in "a" and half in "b". I'm going to leave that as an exercise to the reader. Clues: Block out the unwanted bits from both numbers, shift them, then OR the two numbers together.

OK, if you are still reading, I'll just taunt you a little with some C++ code. :lol: Here is how simple this is for me:
Code:
PostFlopState state;
GetHandState("ah kd   5c 6c 7c", &state);
std::cout << "We have " << state.flushOuts << " outs to a flush.\n";

See, I really didn't try to make this super hard on purpose.

Final notes:This part of my code is new. I've tested it a bit, but I haven't really used it. If you find any bugs please let me know. GetHandStateBrief is kind of clunky, but it should give you a pretty good idea in English of what the structure holds. And the important part, because this code is new, it'll probably change some. In my example I just hard coded in numbers, don't do that. Instead setup a file with stuff like:
$UsesBoth = 0
$UsesOne = 1
$UsesNone = 2
...
That way if I make some changes it'll be much easier update.

BTW, if someone points out now that AutoIt does indeed somehow directly support odd sized ints, or has a BitTest function, I'll feel really stupid. :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 239 posts ]  Go to page 1, 2, 3, 4, 5 ... 12  Next


Who is online

Users browsing this forum: No registered users and 14 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: