From bf361bf08a5255c36cafc1099ee1155955224fa0 Mon Sep 17 00:00:00 2001
From: Tue Herlau <tuhe@dtu.dk>
Date: Thu, 13 Feb 2025 23:30:42 +0100
Subject: [PATCH] Solutions to week 2

---
 .../ex02/deterministic_inventory_TODO_1.py    |  1 +
 solutions/ex02/dp_TODO_1.py                   |  4 ++++
 solutions/ex02/dp_agent_TODO_1.py             |  1 +
 solutions/ex02/flower_store_TODO_1.py         | 23 +++++++++++++++++++
 solutions/ex02/flower_store_TODO_2.py         |  3 +++
 solutions/ex02/flower_store_TODO_3.py         |  3 +++
 solutions/ex02/inventory_TODO_1.py            |  1 +
 7 files changed, 36 insertions(+)
 create mode 100644 solutions/ex02/deterministic_inventory_TODO_1.py
 create mode 100644 solutions/ex02/dp_TODO_1.py
 create mode 100644 solutions/ex02/dp_agent_TODO_1.py
 create mode 100644 solutions/ex02/flower_store_TODO_1.py
 create mode 100644 solutions/ex02/flower_store_TODO_2.py
 create mode 100644 solutions/ex02/flower_store_TODO_3.py
 create mode 100644 solutions/ex02/inventory_TODO_1.py

diff --git a/solutions/ex02/deterministic_inventory_TODO_1.py b/solutions/ex02/deterministic_inventory_TODO_1.py
new file mode 100644
index 0000000..7e93e6c
--- /dev/null
+++ b/solutions/ex02/deterministic_inventory_TODO_1.py
@@ -0,0 +1 @@
+        return {k + 1: 1}
\ No newline at end of file
diff --git a/solutions/ex02/dp_TODO_1.py b/solutions/ex02/dp_TODO_1.py
new file mode 100644
index 0000000..a266a96
--- /dev/null
+++ b/solutions/ex02/dp_TODO_1.py
@@ -0,0 +1,4 @@
+            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
diff --git a/solutions/ex02/dp_agent_TODO_1.py b/solutions/ex02/dp_agent_TODO_1.py
new file mode 100644
index 0000000..18f9f78
--- /dev/null
+++ b/solutions/ex02/dp_agent_TODO_1.py
@@ -0,0 +1 @@
+        action = self.pi_[k][s] 
\ No newline at end of file
diff --git a/solutions/ex02/flower_store_TODO_1.py b/solutions/ex02/flower_store_TODO_1.py
new file mode 100644
index 0000000..4fec1d3
--- /dev/null
+++ b/solutions/ex02/flower_store_TODO_1.py
@@ -0,0 +1,23 @@
+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
diff --git a/solutions/ex02/flower_store_TODO_2.py b/solutions/ex02/flower_store_TODO_2.py
new file mode 100644
index 0000000..29eed84
--- /dev/null
+++ b/solutions/ex02/flower_store_TODO_2.py
@@ -0,0 +1,3 @@
+    model = FlowerStoreModel(N=N, c=c, prob_empty=False) 
+    J, pi = DP_stochastic(model)
+    u = pi[0][x0]  
\ No newline at end of file
diff --git a/solutions/ex02/flower_store_TODO_3.py b/solutions/ex02/flower_store_TODO_3.py
new file mode 100644
index 0000000..62bdb1d
--- /dev/null
+++ b/solutions/ex02/flower_store_TODO_3.py
@@ -0,0 +1,3 @@
+    model = FlowerStoreModel(N=N, prob_empty=True) 
+    J, pi = DP_stochastic(model)
+    pr_empty = -J[0][x0] 
\ No newline at end of file
diff --git a/solutions/ex02/inventory_TODO_1.py b/solutions/ex02/inventory_TODO_1.py
new file mode 100644
index 0000000..38ded4d
--- /dev/null
+++ b/solutions/ex02/inventory_TODO_1.py
@@ -0,0 +1 @@
+        return {0:.1, 1:.7, 2:0.2}
\ No newline at end of file
-- 
GitLab