LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
pfqn_mci.m
1%{
2%{
3 % @file pfqn_mci.m
4 % @brief Monte Carlo Integration (MCI) for normalizing constant.
5%}
6%}
7
8%{
9%{
10 % @brief Monte Carlo Integration (MCI) for normalizing constant.
11 % @fn pfqn_mci(D, N, Z, I, variant)
12 % @param D Service demand matrix.
13 % @param N Population vector.
14 % @param Z Think time vector.
15 % @param I Number of samples (default: 1e5).
16 % @param variant MCI variant ('mci', 'imci', 'rm', default: 'imci').
17 % @return G Normalizing constant estimate.
18 % @return lG Logarithm of normalizing constant.
19 % @return lZ Individual random sample log values.
20%}
21%}
22function [G,lG,lZ] = pfqn_mci(D,N,Z,I,variant)
23% [G,LG,LZ] = PFQN_MCI(D,N,Z,I,VARIANT)
24%
25% Normalizing constant estimation via Monte Carlo Integration
26%
27% Syntax:
28% [G,lG,lZ] = pfqn_mci(D,N,Z,I,VARIANT)
29% Input:
30% D - demands (queues x classes)
31% N - populations (1 x classes)
32% Z - think times (1 x classes)
33% I - samples
34% VARIANT - 'mci', 'imci', 'rm'
35%
36% Output:
37% lG - estimate of logG
38% lZ - individual random samples
39%
40% Note: if the script returns a floating point range exception,
41% double(log(mean(exp(sym(lZ))))) provides a better estimate of lG, but it
42% is very time consuming due to the symbolic operations.
43%
44% Implementation: Giuliano Casale (g.casale@imperial.ac.uk), 16-Aug-2013
45
46if nargin<3%~exist('Z','var')
47 Z=0*N;
48end
49if nargin<4%~exist('I','var')
50 I=1e5;
51end
52if nargin<5%~exist('variant','var')
53 variant='imci';
54end
55
56[M,R] = size(D);
57
58if isempty(D) || sum(D(:))<1e-4
59 lGn = - sum(factln(N)) + sum(N.*log(sum(Z,1)));
60 G=exp(lGn);
61 lZ=[];
62 return
63end
64
65%tput = N./(Z+sum(D)+max(D).*(sum(N)-1)); % balanced job bounds
66%tput = N./Z; % balanced job bounds
67
68%% IMCI
69if strcmpi(variant,'imci') % improved mci
70 tput = pfqn_bs(D,N,Z);
71 util = D*tput';
72 gamma = max( 0.01, 1-util )'; % MonteQueue 2.0 recommendation
73elseif strcmpi(variant,'mci') % original mci
74 tput = pfqn_bs(D,N,Z);
75 util = D*tput';
76 %% Original MCI
77 for i=1:length(util)
78 if util(i)>0.9
79 gamma(i) = 1/sqrt(max(N)); % MonteQueue 2.0 recommendation
80 else
81 gamma(i) = 1-util(i); % MonteQueue 2.0 recommendation
82 end
83 end
84elseif strcmpi(variant,'rm') % repairman problem
85 tput = N./(sum(D,1)+Z+max(D,1)*(sum(N)-1)); % a single queue
86 util = D*tput';
87 %% Original MCI
88 for i=1:length(util)
89 if util(i)>0.9
90 gamma(i) = 1/sqrt(max(N)); % MonteQueue 2.0 recommendation
91 else
92 gamma(i) = 1-util(i); % MonteQueue 2.0 recommendation
93 end
94 end
95end
96try
97 for r=1:R
98 logfact(r) = sum(log(1:N(r))); % log N(r)!
99 end
100
101 % uniform sampling
102 persistent VL
103 if isempty(VL) || size(VL,1) < I || size(VL,2) < M
104 VL = log(rand(I,M));
105 end
106 V = repmat(-1./gamma,I,1).*VL(1:I,1:M);
107 ZI = repmat(Z,I,1);
108 % importance sampling
109 lZ = -(ones(1,M) - gamma) * V' - sum(log(gamma)) - sum(logfact) + N*log(V*D+ZI)';
110
111
112 lG = logmeanexp(lZ); % return average
113 if isinf(lG)
114 % line_warning(mfilename,'Floating-point range exception, Monte Carlo integration will return an approximation.');
115 lG = max(lZ);
116 end
117 G=exp(lG);
118catch ME
119 getReport(ME,'basic')
120end
121end