Commit 8ccea880 authored by Ballarini Paolo's avatar Ballarini Paolo
Browse files

Upload New File

parent 57b04836
%% Cell type:code id: tags:
``` python
from numpy import *
```
%% Cell type:code id: tags:
``` python
## MBMBMB...BM with two-state machines, generates description for prism
# input:
# f: vector of failure prob. for machines
# r: vector of repair prob. for machines
# b: vector of capacity buffer for buffers
# mi: initial state of machines (optional)
# bi: initial state of buffers (optional)
# prism: generate prism or not (optional)
# fn: filename for prism file (optional)
# sym: if true probs are written symbolic, otherwise numerical value
def two_state_to_prism(f,r,b,minit,binit,fn,sym):
NM=len(f)
prismfile = open(fn+".prism", 'w')
prismfile.write('dtmc\n\n')
for i in range(NM):
prismfile.write('const double p%d=%.12e;\n' % (i+1,f[i]))
for i in range(NM):
prismfile.write('const double r%d=%.12e;\n' % (i+1,r[i]))
for i in range(NM-1):
prismfile.write('const int n%d=%d;\n' % (i+1,b[i]))
for i in range(NM):
prismfile.write('const int m%dinit=%d;\n' % (i+1,minit[i]))
for i in range(NM-1):
prismfile.write('const int b%dinit=%d;\n' % (i+1,binit[i]))
prismfile.write('\nmodule ProductionLine\n')
for i in range(NM):
prismfile.write("m%d: [0..1] init m%dinit;\n" % (i+1,i+1))
for i in range(NM-1):
prismfile.write("b%d: [0..n%d] init b%dinit;\n" % (i+1,i+1,i+1))
prismfile.write('\n')
everysitu=[]
for i in range(NM):
everysitu.append([0,1])
for i in range(NM-1):
everysitu.append([0,1,2]) ## 0: empty, 1: neither empty nor full, 2: full
#print(everysitu)
t=tuple([len(everysitu[i]) for i in range(2*NM-1)])
for ind in ndindex(t):
actsitu=[everysitu[i][ind[i]] for i in range(2*NM-1)]
#print(actsitu)
prismfile.write(' [] ')
for i in range(NM):
prismfile.write('(m%d=%d)&' % (i+1,actsitu[i]))
for i in range(NM-1):
if actsitu[i+NM] == 0:
prismfile.write('(b%d=0)' % (i+1))
if actsitu[i+NM] == 1:
prismfile.write('(0<b%d)&(b%d<n%d)' % (i+1,i+1,i+1))
if actsitu[i+NM] == 2:
prismfile.write('(b%d=n%d)' % (i+1,i+1))
if i<NM-2:
prismfile.write('&')
else:
prismfile.write(' ->')
fm=[] ## blocked or non blocked per machine (true means non blocked)
# first machine
if actsitu[NM]<2: # non blocked
fm.append(True)
else:
fm.append(False)
# inside machines
for i in range(1,NM-1):
if actsitu[NM+i-1]>0 and actsitu[NM+i]<2: # non blocked
fm.append(True)
else:
fm.append(False)
# last machine
if actsitu[-1]>0: # non blocked
fm.append(True)
else:
fm.append(False)
#print(fm)
# possible next states and effect of machines
ns=[]
nspr=[]
nsprstr=[]
movepart=[]
for i in range(NM):
if fm[i]==False:
if actsitu[i]==1:
ns.append([1])
nspr.append([1.0])
nsprstr.append(["1.0"])
movepart.append([False])
else:
ns.append([0,1])
movepart.append([False,False])
nspr.append([1-r[i],r[i]])
nsprstr.append(['(1-r%d)'%(i+1),'r%d'%(i+1)])
else:
ns.append([0,1])
movepart.append([False,True])
if actsitu[i]==0:
nspr.append([1-r[i],r[i]])
nsprstr.append(['(1-r%d)'%(i+1),'r%d'%(i+1)])
else:
nspr.append([f[i],1-f[i]])
nsprstr.append(['p%d'%(i+1),'(1-p%d)'%(i+1)])
#print(ns)
#print(nspr)
#print(movepart)
t2=tuple([len(ns[i]) for i in range(NM)])
plusneeded=False
for ind2 in ndindex(t2):
nsi=[ns[i][ind2[i]] for i in range(NM)]
nspri=prod([nspr[i][ind2[i]] for i in range(NM)])
nspristr=""
for i in range(NM):
nspristr=nspristr+nsprstr[i][ind2[i]]
if i<NM-1:
nspristr=nspristr+"*"
for i in range(NM-1):
bl=0
if nsi[i]==1 and movepart[i][ind2[i]]==True:
bl=bl+1
if nsi[i+1]==1 and movepart[i+1][ind2[i+1]]==True:
bl=bl-1
nsi.append(bl)
# identify those that need update
update=[]
for i in range(NM):
if nsi[i]==actsitu[i]:
update.append(False)
else:
update.append(True)
for i in range(NM-1):
if nsi[i+NM]==0:
update.append(False)
else:
update.append(True)
if plusneeded:
prismfile.write('+')
plusneeded=True
if sym:
prismfile.write('\n %s:' % nspristr);
else:
prismfile.write('\n %.12e:' % nspri);
towrite=update.count(True)
if towrite==0:
prismfile.write('true')
else:
for i in range(NM):
if(update[i]):
towrite=towrite-1
prismfile.write('(m%d\'=%d)' % (i+1,nsi[i]))
if towrite>0:
prismfile.write('&')
for i in range(NM-1):
if(update[i+NM]):
towrite=towrite-1
prismfile.write('(b%d\'=b%d' % (i+1,i+1))
if nsi[i+NM] == -1:
prismfile.write('-1')
if nsi[i+NM] == +1:
prismfile.write('+1')
if towrite>0:
prismfile.write(')&')
else:
prismfile.write(')')
prismfile.write(';\n')
prismfile.write("endmodule\n")
prismfile.write('\n// THROUGHPUT\n')
for j in range(NM):
i=j+1
if i==1:
prismfile.write('rewards \"throughput_M%d\"\n ((m%d=1 & b%d<n%d)):1;\nendrewards\n'
% (i,i,i,i))
elif i==NM:
prismfile.write('rewards \"throughput_M%d\"\n ((m%d=1 & b%d>0)):1;\nendrewards\n'
% (i,i,i-1))
else:
prismfile.write('rewards \"throughput_M%d\"\n ((m%d=1 & b%d>0 & b%d<n%d)):1;\nendrewards\n'
% (i,i,i-1,i,i))
prismfile.close()
prismfile = open(fn+".props", 'w')
for j in range(NM):
prismfile.write('R{"throughput_M%d"}=? [ S ]\n' % (j+1))
prismfile.close()
```
%% Cell type:code id: tags:
``` python
## example with 4 machines
f=[0.01, 0.05, 0.02, 0.03]
r=[0.1, 0.2, 0.15, 0.18]
b=[8,10,6]
mi=[1,0,1,0]
bi=[2,3,4]
fn="4M"
two_state_to_prism(f,r,b,mi,bi,fn,True)
```
%% Cell type:code id: tags:
``` python
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment