-
Notifications
You must be signed in to change notification settings - Fork 0
/
NiMC.py
55 lines (40 loc) · 1.65 KB
/
NiMC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class NiMC(object):
"""Nondeterministically intialized Markov Chains."""
def __init__(self):
super(NiMC, self).__init__()
# increases by 1 every time the simulate function gets called
self.cnt_queries = 0
def transition(self):
raise NotImplementedError('The new model file\
has to implement the transition() function.')
def is_unsafe(self):
raise NotImplementedError('The new model file\
has to implement the is_unsafe() function.')
def set_Theta(self, Theta):
Theta = np.array(Theta).astype('float') # [[l1,u1],[l2,u2],[l3,u3],...]
self.Theta = Theta
def set_k(self, k):
self.k = k
def simulate(self, initial_state, k=None):
if not (hasattr(self, 'Theta') and hasattr(self, 'k')):
raise NotImplementedError('The new model file\
has to specify Theta and k by calling set_Theta() and set_k()')
# increases by 1 every time the simulate function gets called
self.cnt_queries += 1
if k is None:
k = self.k
state = initial_state
unsafe = self.is_unsafe(state)
for step in range(int(k)):
if unsafe:
break
else:
state = self.transition(state)
unsafe = self.is_unsafe(state)
reward = 1 if unsafe else 0
return reward
def __call__(self, initial_state, k=None):
return self.simulate(initial_state, k)