Open Side Menu Go to the Top
Register
ICM Algorithm ICM Algorithm

11-18-2008 , 11:21 AM
(Xposted from the STT strategy forum)

Hi,

I was wondering whether anyone had the algorithm for implementing ICM written down and could share it with me. I understand how to calculate it probabilistically, but I'm finding it difficult to convert this into code. (Just a general algorithm would be good, as i will be probably writing it in C and MATLAB)

Basically I'm working on an improved model of ICM and can do the maths side of it, but I'm not too great when it comes to the coding. Thanks.
ICM Algorithm Quote
11-20-2008 , 02:14 AM
Let

N = Number of opponents
S(n; n = [0, N]) = The stack of player n, n=0 for Hero
ITM = Number of positions in the money
M(n) = Prize money for the (n+1):th position

Pseudo code:

Code:
Define S as Array with N+1 elements
Define M as Array with ITM elements

Get input, such that
  S(o) = Hero's stack
  S(1) = First villain's stack
  S(2) = Second villain's stack, and so on
  M(n) = Prize money for position n+1
(A bit uncodeish, but never mind)

Function ICM (S, M)
{
  Count elements of S, store in Integer Players

  If Players > 1 THEN
  {
    Define Value as Double (or whatever best contains a monetary value)
    Count elements of M, store in Integer Prizes
    Define SS as Array with (Players-1) elements
    Define MM as Array with (Prizes-1) elements
    Copy all elements from M EXEPT M(0) into MM

    Value == M(0) * S(0) / [Sum of all elements in S]
    Count Integer b from 1 to Players-1
    {  
      Copy all elements from S EXEPT S(b) into SS (replacing any old values)
      Value ++ ICM(SS, MM) * S(b) / [Sum of all elements in S]
    }
    Return Value
  }
  ELSE
  {
    Return M(0)
  }
}
That should work, right?

Haven't looked it through properly, so there may be flaws.

It's self-iterative, so it branches out quite a bit. Don't use it for large N's without some threading, because that will most probably freeze the computer. But should work fine for 10-man SnGs.

Last edited by Klyka; 11-20-2008 at 02:23 AM.
ICM Algorithm Quote
11-20-2008 , 04:28 AM
Better version:

Code:
Define S as Array with N+1 elements
Define M as Array with ITM elements

Get input, such that
  S(o) = Hero's stack
  S(1) = First villain's stack
  S(2) = Second villain's stack, and so on
  M(n) = Prize money for position n+1
(A bit uncodeish, but never mind)

Function ICM (S, M)
{
  Count elements of S, store in Integer Players

  If Players > 1 THEN
  {
    Define Value as Double (or whatever best contains a monetary value)
    Count elements of M, store in Integer Prizes
    Define SS as Array with (Players-1) elements
    Define MM as Array with (Prizes-1) elements
    Copy all elements from M EXEPT M(0) into MM

    Value == M(0) * S(0) / [Sum of all elements in S]
    If Prizes > 1 THEN
    {
      Count Integer b from 1 to Players-1
      {  
        Copy all elements from S EXEPT S(b) into SS (replacing any old values)
        Value ++ ICM(SS, MM) * S(b) / [Sum of all elements in S]
      }
    }
    Return Value
  }
  ELSE
  {
    Return M(0)
  }
}
Added If Prizes > 1 THEN {} in order to exclude some unnecessary iterations (there's no point in calculating for positions that don't give a prize).
ICM Algorithm Quote
11-20-2008 , 08:40 AM
Thanks! thats exactly what I needed
ICM Algorithm Quote
11-20-2008 , 08:53 AM
If you decide to test this algorithm, please report back whether it works or not. You may just compare the figures produced with those produced from one of the plenty ICM sites available.

I think it should work, but not sure without testing it.
ICM Algorithm Quote
11-20-2008 , 10:32 AM
Sure, I'm writing it up into MATLAB now.
ICM Algorithm Quote
11-24-2008 , 12:45 PM
Ok got it to work, I think a few bits were a bit wrong but it was good enough to allow me to understand what was going on. FYI heres the code I used:

function[value]=ICM2(s,m)
%s=stack sizes
%m=payouts
%p=players
%k=no of prizes in current iteration
p=length(s);
if p>1
k=length(m);
%calculates your first place prob
mm=m(2:k);
value=m(1)*s(1)/sum(s);
%%mm is the payouts for the second iteration
for b=2

ss=[s(1:b-1),s(b+1)];
%ss is the stack sizes with 1 player removed, assuming he is in
%first place
value=value+((ICM2(ss,mm)*s(b))/sum(s));
end
else
value=m(1);
end
ICM Algorithm Quote
10-21-2010 , 03:35 AM
C# Code:

Code:
        public static float GetEquity(float[] stacks, float[] payouts, int player)
        {
            float total = 0;
            for (int i = 0; i < stacks.Length; i++)
                total += stacks[i];
            return GetEquity(stacks, payouts, total, player, 0);
        }

        //Recursive method doing the actual calculation.
        static float GetEquity(float[] stacks, float[] payouts, float total, int player, int depth)
        {
            float eq = stacks[player] / total * payouts[depth]; 

            if (depth + 1 < payouts.Length)
                for (int i = 0; i < stacks.Length; i++)
                    if (i != player && stacks[i] > 0.0)
                    {
                        float c = stacks[i];
                        stacks[i] = 0.0F;
                        eq += GetEquity(stacks, payouts, total - c, player, depth + 1) * c / total;
                        stacks[i] = c;
                    }

            return eq;
        }
ICM Algorithm Quote
11-22-2017 , 12:24 PM
Produced independently of the above, but here are Excel macro functions for ICM and bubble factors.
p1, p2 is 1st prize, 2nd prize
s1, s2 is 1st stack, 2nd stack

Ordering of prizes obviously matters, ordering of stacks doesn't except that if you have e.g. 3 stacks, they must be s1, s2, s3 (in any order) and s4-s10 be assigned zeros.

Rem Attribute VBA_ModuleType=VBAModule
Option VBASupport 1
Public Function CalcBubbleFactor(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 As Currency) As Currency
If s1 = 0 Or s2 = 0 Then
Result = 0
ElseIf s1 > s2 Then
Result = ((EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) _
- EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1 - s2, s2 * 2, s3, s4, s5, s6, s7, s8, s9, s10)) _
/ (EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1 + s2, s3, s4, s5, s6, s7, s8, s9, s10, 0) _
- EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10)))
ElseIf s1 = s2 Then
Result = ((EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) _
- EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, 0.0001, s2 + s1, s3, s4, s5, s6, s7, s8, s9, s10)) _
/ (EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1 * 2, s3, s4, s5, s6, s7, s8, s9, s10, s2 - s1) _
- EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10)))
Else
Result = ((EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10) _
- EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, 0.0001, s2 + s1, s3, s4, s5, s6, s7, s8, s9, s10)) _
/ (EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1 * 2, s2 - s1, s3, s4, s5, s6, s7, s8, s9, s10) _
- EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10)))
End If
CalcBubbleFactor = Result
End Function

Public Function EvaluateICM(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 As Currency) As Currency
If p1 = 0 Or s1 = 0 Then
Result = 0
ElseIf p2 = 0 Then
Result = p1 * s1 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)
Else
Result = (p1 * s1 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10))
If s2 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s3, s4, s5, s6, s7, s8, s9, s10, 0) * (s2 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
If s3 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s2, s4, s5, s6, s7, s8, s9, s10, 0) * (s3 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
If s4 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s2, s3, s5, s6, s7, s8, s9, s10, 0) * (s4 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
If s5 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s2, s3, s4, s6, s7, s8, s9, s10, 0) * (s5 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
If s6 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s2, s3, s4, s5, s7, s8, s9, s10, 0) * (s6 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
If s7 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s2, s3, s4, s5, s6, s8, s9, s10, 0) * (s7 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
If s8 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s2, s3, s4, s5, s6, s7, s9, s10, 0) * (s8 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
If s9 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s2, s3, s4, s5, s6, s7, s8, s10, 0) * (s9 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
If s10 > 0 Then
Result = Result + (EvaluateICM(p2, p3, p4, p5, p6, p7, p8, p9, p10, 0, s1, s2, s3, s4, s5, s6, s7, s8, s9, 0) * (s10 / (s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10)))
End If
End If
End If
End If
End If
End If
End If
End If
End If


End If
EvaluateICM = Result
End Function
ICM Algorithm Quote
11-22-2017 , 02:42 PM
Nice bump!
ICM Algorithm Quote
11-22-2017 , 03:55 PM
Not totally out of the blue:

https://forumserver.twoplustwo.com/3...t-icm-1695705/

One thing I forgot to mention about is that s1 is the stack we are actually evaluating.

if a mod sees this could they change the current
"Ordering of prizes obviously matters, ordering of stacks doesn't except that if you have e.g. 3 stacks, they must be s1, s2, s3 (in any order) and s4-s10 be assigned zeros."
to
"Ordering of prizes obviously matters. s1 is the stack we are evaluating otherwise ordering of stacks doesn't matter except that if you have e.g. 3 stacks, they must be s1, s2, s3 and s4-s10 should be assigned zeros."
ICM Algorithm Quote
10-23-2020 , 08:28 AM
Hi @LektorAJ,

Thank you for your work.
I've been trying to implement your macros but can't make it work because I'm a noob with Excel.
Could you or someone explain to me how to do it or even better post upload an Excel file with the macros already working?

Thanks a lot.
ICM Algorithm Quote

      
m