SDSC6007 - Assignment 1
SDSC6007 - Assignment 1
#assignment #sdsc6007
Question 1
Ash Ketchum is preparing his next trip and packing up. His backpack has maximum weight capacity is z and he wants to fill it up with different quantities of N different items. Denote
-
: the value of the i th type of item
-
: the weight of i th item
-
: the number of items of type i that are loaded in the backpack
Ash Ketchum is trying to maximize the total value of all the items in his backpack, i.e. to maximize subject to the constraints and Formulate this problem and state the corresponding DP algorithm. P.S. You don’t need to solve the problem.
Answer:
For the problem:
-
Definition:
- is the maximum total value when the backpack has remaining capacity ().
- For ,
- For , initially set (assuming non-negative value).
-
Establish the Bellman equation
- For each capacity from 1 to , compute:
If all , then .
- For each capacity from 1 to , compute:
Thus, the maximum total value is .
Question 2
Ash Ketchum is currently staying in Hong Kong in order to capture a unique fierce creature,“Klint”; unfortunately, he run out of money and now he has to work in an inn to save up for the next trip. Everyday, he charges a different rate of a room as the day progresses, depending on the number of vacancies. His salary is proportional to the income during the day, and so his objective is to maximize the expected total income during the day. Denote
-
x: the number of empty rooms at the start of the day
-
y: the number of customers that will ask for a room during the day
After taking SDSC8004 during his free time, Ash Ketchum becomes a genius in statistics, and he now knows y with certainty. Upon arrival of a customer, Ash Ketchum quotes one of m prices , where . A quote of a rate is accepted with probability and is rejected with probability 1-p2, in which case the customer departs, never to return during that day. Formulate this problem and state the corresponding DP algorithm. P.S. You don’t need to solve the problem.
Answer
-
Define the state space and transition policy:
-
State: remaining number of empty rooms () and remaining number of customers ()
-
Decision: for each customer, quote a price ()
-
State transition:
- If the quote is accepted (probability ): gain income , state updates to
- If rejected (probability ): income 0, state updates to
-
Define the optimal value function:
- = maximum expected total income when there are empty rooms and customers remaining
-
Boundary conditions:
- (no customers, income is 0)
- (no empty rooms, income is 0)
-
Establish the Bellman equation (for , ):
The maximum expected total income is
Question 3
Find a shortest path from each node to node 6 for the graph of Figure 1 by using
the DP algorithm manually.
graph LR
1(("1")) -->|4| 2(("2"))
1 -->|1| 3(("3"))
1 -->|5| 4(("4"))
1 -->|8| 6(("6"))
2 -->|1| 4
3 -->|5| 5(("5"))
3 -->|9| 6
4 -->|2| 6
5 -->|0| 3
5 -->|5| 6
classDef target fill:#9f9,stroke:#090;
class 6 target;
Answer
| Iteration | Node | Open | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|---|
| 0 | - | 6 | 0 | |||||
| 1 | 6 | 1, 3, 4, 5 | 8(P=6) | 9(P=6) | 2(P=6) | 5(P=6) | 0 | |
| 2 | 1, 6 | 3, 4, 5 | 8(P=6) | 9(P=6) | 2(P=6) | 5(P=6) | 0 | |
| 3 | 1, 3, 6 | 4, 5 | 8(P=6) | 9(P=6) | 2(P=6) | 5(P=6) | 0 | |
| 4 | 1, 3, 4, 6 | 2, 5 | 7(P=4) | 3(P=4) | 9(P=6) | 2(P=6) | 5(P=6) | 0 |
| 5 | 1, 2, 3, 4, 6 | 5 | 7(P=4) | 3(P=4) | 9(P=6) | 2(P=6) | 5(P=6) | 0 |
| 6 | 1, 2, 3, 4, 5, 6 | - | 7(P=4) | 3(P=4) | 9(P=6) | 2(P=6) | 5(P=6) | 0 |
-
State definition: represents the minimum cost from node to the target node with remaining steps.
-
Initialization (): (target node cost is 0), (, meaning unreachable).
-
Recurrence relation ():
-
Convergence condition: stop when does not change for all (converges within steps, where is the number of nodes).
-
:
-
-
for
-
:
Compute :
-
: edge to 6 (path: )
-
: no edge to 6
-
: edge to 6 (path: )
-
: edge to 6 (path: )
-
: edge to 6 (path: )
-
Result:
-
:
Compute :
-
: edges to 2,3,4,6 (path: )
-
: edge to 4 (path: )
-
: edges to 5,6 (path: )
-
: edge to 6
-
: edges to 3,6 (path: )
-
Result:
-
:
Compute :
-
: edges to 2,3,4,6 (path: or )
-
: edge to 4
-
: edges to 5,6
-
: edge to 6
-
: edges to 3,6
-
Result: (same as , converged)
Optimal results
-
Node 1 6: minimum cost 7 (path: or )
-
Node 2 6: minimum cost 3 (path: )
-
Node 3 6: minimum cost 9 (path: )
-
Node 4 6: minimum cost 2 (path: )
-
Node 5 6: minimum cost 5 (path: )
-
Node 6 6: minimum cost 0
Question 4
Find a shortest path from node 1 to node 5 for the graph of Figure 2 by using (any) label correcting method manually.
graph LR
1(("1")) -->|2| 2(("2"))
1 -->|2| 3(("3"))
4(("4"))
5(("5"))
2 -->|1| 3
2 -->|1| 4
2 -->|0| 5
3 -->|1| 2
3 -->|3| 4
4 -->|0| 5
classDef target fill:#9f9,stroke:#090;
class 5 target;
Label correcting algorithm calculation table
| Iteration | Node | Open | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|
| 0 | - | 1 | 0 | ||||
| 1 | 1 | 2,3 | 0 | 2(P=1) | 2(P=1) | ||
| 2 | 1, 2 | 3,4,5 | 0 | 2(P=1) | 2(P=1) | 3(P=2) | 2(P=2) |
| 3 | 1, 2, 3 | 4,5 | 0 | 2(P=1) | 2(P=1) | 3(P=2) | 2(P=2) |
| 4 | 1, 2, 3, 4 | 5 | 0 | 2(P=1) | 2(P=1) | 3(P=2) | 2(P=2) |
| 5 | 1, 2, 3, 4, 5 | - | 0 | 2(P=1) | 2(P=1) | 3(P=2) | 2(P=2) |
Question 5.(a)
Confirm your answer in Question 3 by using Python to solve the shortest path problem. Please submit your Python code as part of your submission. You can submit either a .py or a .txt file. If you are using other formats, you can copy and paste your code to a .txt file for submission.
1 | def shortest_path_to_destination(graph, destination, max_steps=5): |
Output:
1 | Shortest path cost from each node to target node 6: |
