LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
mmap_sample.m
1function [T,A,LAST,FIRST,STS] = mmap_sample(MMAP, nSamples, pi, seed)
2% Generates samples from a Marked MAP.
3% Input:
4% - MMAP: representation of the Marked MAP
5% - nSamples: number of random samples to collect
6% - pi: (optional) initial probability. If not provided, steady state
7% probabilities are used.
8% - seed: (optional) seed for the random number generator
9
10% default: stationary inialization (as in single-class MAP)
11if (nargin < 3)
12 pi = map_pie(MMAP);
13end
14LAST=[];
15FIRST=[];
16
17% check if single-class
18% MAP has two matrices
19% MMAP with K=1 has three matrices
20if (length(MMAP) == 2 || length(MMAP) == 3)
21 if (nargin == 4)
22 [T,LAST,FIRST]=map_sample({MMAP{1}, MMAP{2}}, nSamples, pi, seed);
23 else
24 [T,LAST,FIRST]=map_sample({MMAP{1}, MMAP{2}}, nSamples, pi);
25 end
26 A = ones(nSamples,1);
27 return
28end
29
30% if provided, initialize RNG with the given seed
31if (nargin == 4)
32 s = RandStream('mt19937ar', 'seed', seed);
33 RandStream.setGlobalStream(s);
34end
35
36% number of states
37M = size(MMAP{1},1);
38
39% number of classes
40C = length(MMAP)-2;
41
42% single state -> exponential
43if M==1
44 T=exprnd(map_mean(MMAP),nSamples,1);
45 crate = zeros(1,C);
46 for c = 1:C
47 crate(c) = MMAP{c+2};
48 end
49 A=randp(crate,nSamples,1);
50 LAST=1;
51 FIRST=1;
52 return
53end
54
55% rates of the exponential distributions in each state
56D0 = MMAP{1};
57rates=-diag(D0);
58% row s contains the discrete probability distribution for the transitions
59% from state s (the first M are the embedded transitions, the following
60% M*C are the transitions that mark an arrival)
61targets=zeros(M,M+M*C);
62for i = 1:M
63 for j = 1:M
64 if i ~= j
65 targets(i,j) = D0(i,j) / rates(i);
66 end
67 end
68 for c = 1:C
69 for j = 1:M
70 targets(i,c*M+j) = MMAP{2+c}(i,j) / rates(i);
71 end
72 end
73end
74
75% pick a random initial state according to pi
76initState=find(rand()<cumsum(pi),1);
77
78T = zeros(nSamples,1);
79A = zeros(nSamples,1);
80
81% simulate
82s=initState;
83tTime=0;
84h = 1;
85batchSize=zeros(1,M);
86batchCounter=zeros(1,M);
87timeBatch=cell(1,M);
88tranBatch=cell(1,M);
89for p = 1:M
90 batchSize(p)=0;
91 batchCounter(p)=1;
92 timeBatch{p}=[];
93 tranBatch{p}=[];
94end
95STS = zeros(nSamples,1);
96while h <= nSamples
97 STS(h) = s;
98 % regenerate exponential batch if needed
99 if (h == 1 || batchCounter(s) > batchSize(s))
100 batchSize(s)=min(100,nSamples-h+1);
101 timeBatch{s}=exprnd(1/rates(s),batchSize(s),1);
102 tranBatch{s}=randp(targets(s,:),batchSize(s),1);
103 batchCounter(s)=1;
104 end
105 % pick from batch random values
106 time = timeBatch{s}(batchCounter(s));
107 transition = tranBatch{s}(batchCounter(s));
108 % increase batch counter usage
109 batchCounter(s)=batchCounter(s)+1;
110 % choose transition
111 % should be equivalent to but much faster than idivide
112 % type=idivide(int32(transition-1),int32(M),'floor');
113 type=int32(floor((transition-1)/M));
114 if (type) == 0
115 % no event, internal transition
116 tTime=tTime + time;
117 sNext=mod(transition,M);
118 if (sNext == 0)
119 sNext = M;
120 end
121 %fprintf('No event at time %0.2f, transition %d->%d\n', ...
122 % tTime, s, sNext);
123 % change state
124 s=sNext;
125 else
126 % event
127 c=type;
128 T(h)=tTime + time;
129 A(h)=c;
130 sNext = mod(transition,M);
131 if (sNext == 0)
132 sNext = M;
133 end
134 %fprintf('Event %2d at time %.2f, transition %d->%d, class %d\n', ...
135 % h, T(h), s, sNext, A(h));
136 % reset time accumulator
137 tTime=0;
138 % change state
139 s=sNext;
140 % next sample
141 h=h+1;
142 end
143end
144
145end