Solved

Need Erlang C formula Flipped & converted to SQL

Posted on 2010-11-09
7
1,335 Views
Last Modified: 2013-04-16
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
Comment
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
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 18

Expert Comment

by:deighton
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

by:okacs
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

by:
deighton earned 500 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:okacs
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

by:okacs
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

Open in new window

0
 
LVL 18

Expert Comment

by:deighton
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

by:dstigue
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question