-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiapers2.py
150 lines (135 loc) · 3.97 KB
/
diapers2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from __future__ import print_function,division
# duner. using numbers and sample.
"""
q +-----+ r +-----+
---->| C |---->| D |--> s
^ +-----+ +-+---+
| |
+-----------------+
C = stock of clean diapers
D = stock of dirty diapers
q = inflow of clean diapers
r = flow of clean diapers to dirty diapers
s = out-flow of dirty diapers
"""
class o:
def has(i) : return i.__dict__
def keys(i) : return i.has().keys()
def items(i) : return i.has().items()
def __init__(i,**d) : i.has().update(d)
def copy(i) : return o(**i.has().copy())
def __getitem__(i,k): return i.has()[k]
def __setitem__(i,k,v): i.has()[k] = v
def __repr__(i) : return 'o'+str(i.has())
class Thing(object):
def __init__(i,name='',lo=0,hi=1e32,init=0,
goal=None,step=1,prec=0):
i.name,i.lo,i.hi= name,lo,hi
i.init,i.goal = init,goal
i.step,i.prec = step,prec
def show(i,x):
x= round(i.step*round(x/i.step),i.prec)
if i.prec == 0:
x = int(x)
return x
def restrain(i,x):
if x < i.lo: return i.lo
elif x > i.hi: return i.hi
else:
return x
def __repr__(i): return '%s=%s' % (i.name, i.show(i.init))
class Flow(Thing): pass
class Stock(Thing): pass
class Aux(Thing): pass
class Time(Thing):
def __init__(i,lo=0,hi=1e32,step=1,prec=0):
super(Time,i).__init__(name='time',lo=lo,
hi=hi,step=step,prec=prec)
F,A,S,T=Flow,Aux,Stock,Time
class Things:
def __init__(i,**things):
i.keys = i.order(things.keys(),'T')
i.things = things
def order(i,keys,first):
assert first in keys, "state needs time 'T'"
return [first] + sorted(k for k in keys if k != first)
def show(i, state):
return [i.things[k].show(state[k])
for k in i.keys]
def init(i):
return o(**{k:v.init
for k,v in i.things.items()})
def restrain(i,state):
for k,v in state.items():
thing = i.things[k]
if v < thing.lo: state[k] = thing.lo
elif v > thing.hi: state[k] = thing.hi
return state
def duration(i):
assert 'T' in i.things
t0 = t = i.things['T'].lo
while t < i.things['T'].hi:
t += i.things['T'].step
yield t - t0, t
t0 = t
class Log:
def __init__(i,things,steps=20):
i.log,i.things,i.steps = [],things,steps
def __iadd__(i,state):
if i.steps:
i.log += [i.things.show(state)]
if len(i.log) % i.steps == 0:
i.dump()
return i
def dump(i):
if i.log:
print("")
printm([i.things.keys] + i.log)
i.log = []
def printm(matrix):
s = [[str(e) for e in row] for row in matrix]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = ' | '.join('{{:{}}}'.format(x) for x in lens)
for row in [fmt.format(*row) for row in s]:
print(row)
def sim(things,spy=0):
state0 = things.init()
log = Log(things,spy)
for dt,t in things.duration():
state0.T = t
log += state0
state1 = state0.copy()
yield dt,t,state0,state1
state1 = things.restrain(state1)
state0 = state1
log.dump()
class Simulation:
def things(i):
return Things(T= A('time',init=0,lo=0,hi=100))
def step(i,dt,t,u,v): pass
def earlyStop(i,state): return False
def run(i):
for dt,t,u,v in sim(i.things(),spy=20):
i.step(dt,t,u,v)
if i.earlyStop(v):
break
return v
class Diapers(Simulation):
def things(i): return Things(
T= A('time', init=0, lo=0, hi=100),
C= S('clean', init=20,lo=0, hi=1000),
D= S('dirty', init=0, lo=0, hi=1000),
q= F('new clean', init=0, lo=0, hi=200),
r= F('clean2dirty', init=8, lo=0, hi=100),
s= F('departing dirty',init=0, lo=0, hi=100)
)
def step(i,dt,t,u,v):
def saturday(t):
return t % 7 == 6
v.C += dt*(u.q - u.r)
v.D += dt*(u.r - u.s)
v.q = 70 if saturday(t) else 0
v.s = u.D if saturday(t) else 0
if int(t) == 27: # special case (the day i forget)
v.s = 0
#Diapers().run()