Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Need Erlang C formula Flipped & converted to SQL

Posted on 2010-11-09
Medium Priority
1,392 Views
Followup to Question 26596057

I'm writing a SQL query and need to calculate the # agents using the Erlang C formula but flipping it to solve for N.

Thanks.
0
Question by:okacs
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 18

Expert Comment

ID: 34094636
/*
N is an integer, so there may not be a whole value of N that gives the exact value.
The following attempts to find the best value of N available in terms of absolute error.
i.e. the value of N that gets closest

*/

/****** Object:  StoredProcedure [dbo].[Erlang2]    Script Date: 11/09/2010 16:32:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:        ANDY DEIGHTON
-- Create date: 9/11/2010
-- Description:    ERLANG FORMULA
-- =============================================
CREATE PROCEDURE [dbo].[Erlang2]

@A  float,
@ERRLANG  float

AS
BEGIN

SET NOCOUNT ON;

DECLARE @TESTVAL float;
CREATE TABLE #BestGuess (N int, ERRLANG float, absError float);

DECLARE @N int;
SET @N = 1;

DECLARE @ERROROUT int;
SET @ERROROUT = 0;

WHILE @ERROROUT = 0
BEGIN

BEGIN TRY

DECLARE @NFACT float;
DECLARE @I int;
SET @NFACT = 1;
SET @I = 1;
WHILE @I <= @N
BEGIN
SET @NFACT = @NFACT * @I;
SET @I = @I + 1;
END

DECLARE @NUMERATOR float;
SET @NUMERATOR = (POWER(@A, @N) / @NFACT) * @N;
--work out the summation
DECLARE @SIGMA float;
SET @SIGMA = 0;
DECLARE @IFACTORIAL float;
SET @I=0;
--0! = 1 as a start
SET @IFACTORIAL = 1;
WHILE @I < @N
BEGIN
SET @SIGMA = @SIGMA + POWER(@A, @I) / @IFACTORIAL;

--increment i and calculate the next i!
SET @I = @I + 1;
SET @IFACTORIAL = @IFACTORIAL * @I;

END

DECLARE @DENOM float;
SET @DENOM = (@N - @A) * @SIGMA + POWER(@A, @N) * @N / @NFACT;

SET @TESTVAL = @NUMERATOR / @DENOM;

INSERT #BestGuess VALUES(@N , @TESTVAL , ABS(@ERRLANG - @TESTVAL));
SET @N = @N + 1;

END TRY
BEGIN CATCH
--select ERROR_MESSAGE(), @NFACT;
SET @ERROROUT = 1;
END CATCH
END

DECLARE @RETVAL int;

SELECT TOP 1 * FROM #BestGuess ORDER BY abserror ASC;

SELECT TOP 1 @RETVAL = N FROM #BestGuess ORDER BY abserror ASC;

RETURN @RETVAL;

END

GO

0

Author Comment

ID: 34094760
Geez you're fast.

Why are there 2 parameters @A and @Errlang?  In the Erlang formula, the variable A is for the Erlangs.  THe other varaible should be for Pw (the % of service level expected) ?

Am I missing something?
0

LVL 18

Accepted Solution

deighton earned 2000 total points
ID: 34094958
the formula you showed me was a function of A and N, doesn't that give a probability?

P = f(A, N)

but now you are saying you want N such that P = f(A, N)

you know what A and P are

---------------------------------------------------

from Wikipedia - A is traffic and N is number of servers -

Pw = ErlangC(A,N)

Pw is the probability that a customer has to wait for service

so are you perhaps saying that you want Pw to be restricted to some sort of value, .1 for example?  That would mean there is a 10% chance of waiting for a resource, then you want to know what N is to achieve that probability
0

Author Comment

ID: 34095238
The original formula was (see screen-shot below) with inputs A (erlangs) and N (number of agents) and it returned probability Pw.  I want to flip the formula and input the probability Pw and the Erlangs, and return an INT N (the number of agents needed to maintain this probability at this erlang level of traffic).

ie:  N = (some formula of A and Pw)

ScreenShot210.jpg
0

Author Comment

ID: 34096761
Andy,

I found another way.  I just set my values out of spec and call your ErlangC SP with incremented values until I come withing spec.  See below.

``````GO
/****** Object:  StoredProcedure [dbo].[Erlang_C2]    Script Date: 11/09/2010 09:17:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:			OKACS
-- Create date:		11/9/2010
-- Description:		Finds the # agents necessary to reach a given service level
--
--					Loop Through Andy's Erlang C proc until the value meets the goal
--					and then display the resulting N
-- =============================================
ALTER PROCEDURE [dbo].[Erlang_C2]
@A  float,		-- Amount of traffic expressed in Erlangs (call arrival rate/sec  X average call time)
@P  float		-- Service Level Goal (Change of call going to hold)

AS
BEGIN

DECLARE @S float	-- Resultant Service Level
DECLARE @I int		-- Number agents required
SET @S = 99
SET @I = 1

IF (@P < 1 or @P > 99)
BEGIN
-- Error, ServiceLevel Goal cant be less than 1% or greater than 99%
SET @I = 0
END
ELSE

--	SELECT 'Looking for goal : ' + cast(@P as varchar)
WHILE ((@S >= 100-@P) and (@I < 35))	-- To a max of 35 so the function does not run forever
BEGIN
EXEC @S = [Erlang_C] @A, @I
SET @I = @I + 1;
--			SELECT 'TRYING ' + cast(@I as varchar) + ' agents for SL of '
--			SELECT @S as SL
END

SELECT @I as AgentsNeeded

END

--					Erlangs	ServiceLevel
-- EXEC [Erlang_C2]    23.31,      99
``````
0

LVL 18

Expert Comment

ID: 34100494
BTW - I noticed if N=A, the formula returns an error due to division by zero, but this is not necessary!  It is possible to multiply the numerator and denominator by (N-A) to give the formula in the computer friendlier form

[((A^N) / N!) N] / [(N-A) sigma A^i / i! + N (A^N) / N!]

which for N = A gives the value 1

the modified form is mathematically the same as the original. but avoids asking a computer to tell us what infinity divided by infinity is.
0

Expert Comment

ID: 39083828
This solution gave me overflows when trying to do the factorial part of the equation. This solution does not. Posting here for Anyone else looking for Erlang C in SQL..
ErlangC.sql
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that witâ€¦
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaacâ€¦
###### Suggested Courses
Course of the Month10 days, 22 hours left to enroll