Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating ErlangB formula from VBA to PHP

Tags:

php

vba

I have the following in VBA that works flawlessly:

Private Function MinMax(Val As Single, Min As Single, Max As Single) As Single
'Apply minimum and maximum bounds to a value
    MinMax = Val
    If Val < Min Then MinMax = Min
    If Val > Max Then MinMax = Max
End Function

'-----------------------------------------------------------------------
Public Function ErlangB(Servers As Single, Intensity As Single) As Single
'The Erlang B formula calculates the percentage likelyhood of the call
' being blocked, that is that all the trunks are in use and the caller
' will receive a busy signal.
' Servers = Number of telephone lines
' Intensity = Arrival rate of calls / Completion rate of calls
'   Arrival rate = the number of calls arriving per hour
'   Completion rate = the number of calls completed per hour
Dim Val As Single, Last As Single, B As Single
Dim Count As Long, MaxIterate As Long
On Error GoTo ErlangBError
     If (Servers < 0) Or (Intensity < 0) Then
          ErlangB = 0
          Exit Function
     End If
     MaxIterate = Fix(Servers)
     Val = Intensity
     Last = 1 ' for server = 0
     For Count = 1 To MaxIterate
          B = (Val * Last) / (Count + (Val * Last))
          Last = B
     Next Count
ErlangBExit:
     ErlangB = MinMax(B, 0, 1)
     Exit Function

ErlangBError:
     B = 0
     Resume ErlangBExit
End Function

I am attempting to recreate it in PHP with the following code:

function minmax($val, $min, $max)
{
    //Apply minimum and maximum bounds to a value
    $minmax = $val;
    if($val < $min){ $minmax = $min;}
    if($val > $max) { $minmax = $max;}

    Return $minmax;
}

function erlangb($servers, $intensity)
{
    //'The Erlang B formula calculates the percentage likelyhood of the call
    //' being blocked, that is that all the trunks are in use and the caller
    //' will receive a busy signal.
    //' Servers = Number of telephone lines
    //' Intensity = Arrival rate of calls / Completion rate of calls
    //'   Arrival rate = the number of calls arriving per hour
    //'   Completion rate = the number of calls completed per hour
    if($servers < 0 || $intensity < 0) {
        $erlangb = 0;
        }
    Else{
        $maxiterate = floor($servers);
        $val = $intensity;
        $last = 1;
        $count = 1;
        while($count < $maxiterate){
            $b = ($val * $last) / ($count + ($val * $last));
            $last = $b;
            $count ++;
        }
    }
    return minmax($b, 0, 1);
}

However I am not always getting the same results. For example a request like this:

erlangb(10, 5)

Produces a result of 0.01838457 in VBA, but in PHP a value of 0.037457785974194.

What did I miss in my transition that is causing the vastly different values?

like image 836
Joel Lewis Avatar asked Nov 20 '25 14:11

Joel Lewis


1 Answers

Change this

while($count < $maxiterate){

to

while($count <= $maxiterate){

like image 193
cmorrissey Avatar answered Nov 23 '25 05:11

cmorrissey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!