-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsimulation.py
104 lines (88 loc) · 3.87 KB
/
simulation.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
import dynamics
import numpy as np
import torch
import time
import os
from network import Actor, Critic
from config import DynamicsConfig
from datetime import datetime
from solver import Solver
from utils import step_relative
from plot import plot_comparison, plot_ref_and_state, plot_phase_plot
from config import GeneralConfig
def simulation(methods, log_dir, simu_dir):
'''
Args:
methods: list, methods to simulate
log_dir: str, name of log dir
simu_dir: str, name of log dir
Returns:
'''
config = GeneralConfig()
S_DIM = config.STATE_DIM
A_DIM = config.ACTION_DIM
policy = Actor(S_DIM, A_DIM)
value = Critic(S_DIM, A_DIM)
config = DynamicsConfig()
solver=Solver()
load_dir = log_dir
policy.load_parameters(load_dir)
value.load_parameters(load_dir)
statemodel_plt = dynamics.VehicleDynamics()
plot_length = config.SIMULATION_STEPS
# Open-loop reference
x_init = [0.0, 0.0, config.psi_init, 0.0, 0.0]
op_state, op_control = solver.openLoopMpcSolver(x_init, config.NP_TOTAL)
np.savetxt(os.path.join(simu_dir, 'Open_loop_control.txt'), op_control)
for method in methods:
cal_time = 0
state = torch.tensor([[0.0, 0.0, config.psi_init, 0.0, 0.0]])
state.requires_grad_(False)
x_ref = statemodel_plt.reference_trajectory(state[:, -1])
state_r = state.detach().clone()
state_r[:, 0:4] = state_r[:, 0:4] - x_ref
state_history = state.detach().numpy()
control_history = []
if methods != 'OP': print('\nCALCULATION TIME:')
for i in range(plot_length):
if method == 'ADP':
time_start = time.time()
u = policy.forward(state_r[:, 0:4])
cal_time += time.time() - time_start
elif method.startswith('MPC'):
pred_steps = int(method.split('-')[1])
x = state_r.tolist()[0]
time_start = time.time()
_, control = solver.mpcSolver(x, pred_steps)
cal_time += time.time() - time_start
u = np.array(control[0], dtype='float32').reshape(-1, config.ACTION_DIM)
u = torch.from_numpy(u)
else:
u = np.array(op_control[i], dtype='float32').reshape(-1, config.ACTION_DIM)
u = torch.from_numpy(u)
state, state_r, _ =step_relative(statemodel_plt, state, u)
state_history = np.append(state_history, state.detach().numpy(), axis=0)
control_history = np.append(control_history, u.detach().numpy())
if method == 'ADP':
print(" ADP: {:.3f}".format(cal_time) + "s")
np.savetxt(os.path.join(simu_dir, 'ADP_state.txt'), state_history)
np.savetxt(os.path.join(simu_dir, 'ADP_control.txt'), control_history)
elif method.startswith('MPC'):
pred_steps = method.split('-')[1]
state_fname, control_fname = 'MPC_' + pred_steps + '_state.txt', \
'MPC_' + pred_steps + '_control.txt'
print(" MPC {} steps: {:.3f}".format(pred_steps, cal_time) + "s")
np.savetxt(os.path.join(simu_dir, state_fname), state_history)
np.savetxt(os.path.join(simu_dir, control_fname), control_history)
else:
np.savetxt(os.path.join(simu_dir, 'Open_loop_state.txt'), state_history)
plot_comparison(simu_dir, methods)
plot_ref_and_state(log_dir, simu_dir, ref='pos')
plot_ref_and_state(log_dir, simu_dir, ref='angle')
plot_phase_plot(['MPC-50', 'MPC-10', 'MPC-5', 'ADP'], log_dir, simu_dir)
if __name__ == '__main__':
LOG_DIR = "./trained_results/2020-10-09-14-42-10000"
METHODS = ['MPC-10', 'ADP', 'OP'] #
simu_dir = "./Simulation_dir/" + datetime.now().strftime("%Y-%m-%d-%H-%M")
os.makedirs(simu_dir, exist_ok=True)
simulation(METHODS, LOG_DIR, simu_dir)