Skip to content
Snippets Groups Projects
Commit bf361bf0 authored by tuhe's avatar tuhe
Browse files

Solutions to week 2

parent dc792356
No related branches found
No related tags found
No related merge requests found
return {k + 1: 1}
\ No newline at end of file
Qu = {u: sum(pw * (model.g(x, u, w, k) + J[k + 1][model.f(x, u, w, k)]) for w, pw in model.Pw(x, u, k).items()) for u in model.A(x, k)}
umin = min(Qu, key=Qu.get)
J[k][x] = Qu[umin] # Compute the expected cost function
pi[k][x] = umin # Compute the optimal policy
\ No newline at end of file
action = self.pi_[k][s]
\ No newline at end of file
class FlowerStoreModel(InventoryDPModel):
def __init__(self, N=3, c=0., prob_empty=False):
self.c = c
self.prob_empty = prob_empty
super().__init__(N=N)
def g(self, x, u, w, k): # Cost function g_k(x,u,w)
if self.prob_empty:
return 0
return u * self.c + np.abs(x + u - w)
def f(self, x, u, w, k): # Dynamics f_k(x,u,w)
return max(0, min(max(self.S(k)), x + u - w))
def Pw(self, x, u, k): # Distribution over random disturbances
pw = {0: .1, 1: .3, 2: .6}
return pw
def gN(self, x):
if self.prob_empty:
return -1 if x == 1 else 0
else:
return 0
\ No newline at end of file
model = FlowerStoreModel(N=N, c=c, prob_empty=False)
J, pi = DP_stochastic(model)
u = pi[0][x0]
\ No newline at end of file
model = FlowerStoreModel(N=N, prob_empty=True)
J, pi = DP_stochastic(model)
pr_empty = -J[0][x0]
\ No newline at end of file
return {0:.1, 1:.7, 2:0.2}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment