* Example 5
echo Example 5
* Example 5a:
* Transient and steady-state analysis sf an mm1k queue
* For details of the model see Chapter 8 of Trivedi's book
* For a closed-form transient solution as well as the details
* of the numerical transient solution methods see
* Reibman and Trivedi; Computers and Operations Research; 1988.

* the keyword readprobs flags to sharpe that the transient analysis
* is desired and hence even if the Markov chain is irreducible,
* initial state probs will be specified.

markov mm110 readprobs
0 1 lam
1 2 lam
2 3 lam
3 4 lam
4 5 lam
5 6 lam
6 7 lam
7 8 lam
8 9 lam
9 10 lam
10 9 mu
9 8 mu
8 7 mu
7 6 mu
6 5 mu
5 4 mu
4 3 mu
3 2 mu
2 1 mu
1 0 mu
* Assign to state i a reward rate equal to the number of jobs in the
* the system in that state
reward
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
end
* Initial state probs.
0 1.0
end

bind
lam 1.0
mu 2.0
end

* Steady state prob of being in the buffer full state
expr prob(mm110,10)


* Transient prob of being in the buffer full state at time t
loop t,10,100,10
expr tvalue(t;mm110,10)

end

* Compute the server idle prob at time t
loop t,0,10,1
expr tvalue (t;mm110,0)
end

* Compute the server idle prob at steady state
expr prob(mm110,0)

* Compute the transient average queulength
loop t,0,10,1
expr sum(i,0,10,i*tvalue (t;mm110,$i))
end

* Compute the steady state average queulength
expr exrss(mm110)


* Example 5b:
* Now if we let k be infinite then the Markov model
* will be impossible to solve within SHARPE;
* Fortunately, there is a closed-form solution
* we can use in this case.
* This example illustrates the use
* SHARPE to synthesize some models using
* functions

* We build a model of an M/M/1 queue
* Refer to Chapter 8 of Trivedi's book for details
* lam is the rate of arrivals
* mu is the service rate
* utt is the traffic intensity or the server utilization, 
* the average number of jobs in the system in the steady-state
* is computed by the function congest
* while the function resptime computes the average response time.
* the function resptimedist computes the response time distribution.

func congest(rho) rho/(1-rho)
func utt(lam,mu) lam/mu
func meanresptime(lam,mu) congest(lam/mu)/lam

* Now we model the distribution function of the response time
graph resptimedist(lam,mu)
1 2
end
dist 1 exp(mu*(1-utt(lam,mu)))
dist 2 zero
end


echo plot 0.0 10.0
cdf(resptimedist;0.8,1)
echo plot
eval (resptimedist;0.8,1) 0 10 1

loop lam,0.1,0.9,0.1
  expr utt(lam,1), congest(utt(lam,1)), meanresptime(lam,1)
end

end
