-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (71 loc) · 3.12 KB
/
Copy pathmain.py
File metadata and controls
91 lines (71 loc) · 3.12 KB
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
"""
main.py
Python script that calls all the functions for computing the optimal cost
and policy of the given problem.
Dynamic Programming and Optimal Control
Fall 2024
Programming Exercise
Contact: Antonio Terpin aterpin@ethz.ch
Authors: Maximilian Stralz, Philip Pawlowsky, Antonio Terpin
--
ETH Zurich
Institute for Dynamic Systems and Control
--
"""
import sys, os
import numpy as np
from ComputeExpectedStageCosts import compute_expected_stage_cost
from ComputeTransitionProbabilities import compute_transition_probabilities
from Solver import solution
from Constants import Constants
import time
if __name__ == "__main__":
# Set the following to True as you progress with the files
TRANSITION_PROBABILITIES_IMPLEMENTED = True
STAGE_COSTS_IMPLEMENTED = True
SOLUTION_IMPLEMENTED = True
# Compute transition probabilities
if TRANSITION_PROBABILITIES_IMPLEMENTED:
sys.stdout.write("[ ] Computing transition probabilities...")
# TODO implement this function in ComputeTransitionProbabilities.py
t = time.time()
P = compute_transition_probabilities(Constants)
print('get probs:', time.time()-t)
print("\r[X] Transition probabilities computed. ")
else:
print(
"[ ] Transition probabilities not implemented. If this is unexpected, check the boolean 'TRANSITION_PROBABILITIES_IMPLEMENTED'."
)
P = np.zeros((Constants.K, Constants.K, Constants.L))
# Compute expected stage costs
if STAGE_COSTS_IMPLEMENTED:
sys.stdout.write("[ ] Computing expected stage costs...")
# TODO implement this function in ComputeExpectedStageCosts.py
t = time.time()
Q = compute_expected_stage_cost(Constants)
print('get costs:', time.time()-t)
print("\r[X] Expected stage costs computed. ")
else:
print(
"[ ] Expected stage costs not implemented. If this is unexpected, check the boolean 'STAGE_COSTS_IMPLEMENTED'."
)
Q = np.ones((Constants.K, Constants.L)) * np.inf
# Solve the stochastic shortest path problem
if SOLUTION_IMPLEMENTED:
sys.stdout.write("[ ] Solving discounted stochastic shortest path problem...")
# TODO implement this function in Solver.py
t = time.time()
J_opt, u_opt = solution(P, Q, Constants)
print('solve:', time.time()-t)
assert J_opt.shape[0] == Constants.K, "J_opt dimensions do not match the world."
assert u_opt.shape[0] == Constants.K, "u_opt dimensions do not match the world."
print("\r[X] Discounted stochastic shortest path problem solved. ")
else:
print(
"[ ] Solution of the discounted stochastic shortest path problem not implemented. If this is unexpected, check the boolean 'SOLUTION_IMPLEMENTED'."
)
J_opt = np.inf * np.ones(Constants.K)
u_opt = np.zeros(Constants.K)
os.makedirs("workspaces", exist_ok=True)
np.save("workspaces/J_opt.npy", J_opt)
np.save("workspaces/u_opt.npy", (u_opt).astype(int))