-
Notifications
You must be signed in to change notification settings - Fork 21
/
util.py
122 lines (92 loc) · 4.07 KB
/
util.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from do_mpc.data import save_results, load_results
import config
from mpc_cbf import MPC
from plotter import plot_path_comparisons, plot_cost_comparisons, plot_min_distance_comparison
def save_mpc_results(controller):
"""Save results in pickle file."""
if config.controller == "MPC-CBF":
filename = config.controller + '_' + config.control_type + '_gamma' + str(config.gamma)
else:
filename = config.controller + '_' + config.control_type
save_results([controller.mpc, controller.simulator], result_name=filename)
def load_mpc_results(filename):
"""Load results from pickle file."""
return load_results('./results/' + filename + '.pkl')
def compare_controller_results(N, gamma):
"""Compares the total cost and min distances for each method over N experiments."""
obs = [(1.0, 0.5, 0.1)] # The obstacles used when creating the experiments
# Get costs & min distances from results
costs_cbf = []
costs_dc = []
min_distances_cbf = []
min_distances_dc = []
for i in range(1, N+1):
# Filename prefix
if len(str(i)) == 1:
num = '00' + str(i)
elif len(str(i)) == 2:
num = '0' + str(i)
else:
num = str(i)
# Get cbf result
filename_cbf = num + "_MPC-CBF_setpoint_gamma" + str(gamma)
results_cbf = load_mpc_results(filename_cbf)
total_cost_cbf = sum(results_cbf['mpc']['_aux'][:, 1])
costs_cbf.append(total_cost_cbf)
positions = results_cbf['mpc']['_x']
distances = []
for p in positions:
distances.append(((p[0]-obs[0][0])**2 + (p[1]-obs[0][1])**2)**(1/2) - (config.r + obs[0][2]))
min_distances_cbf.append(min(distances))
# Get dc result
filename_dc = num + "_MPC-DC_setpoint"
results_dc = load_mpc_results(filename_dc)
total_cost_dc = sum(results_dc['mpc']['_aux'][:, 1])
costs_dc.append(total_cost_dc)
positions = results_dc['mpc']['_x']
distances = []
for p in positions:
distances.append(((p[0]-obs[0][0])**2 + (p[1]-obs[0][1])**2)**(1/2) - (config.r + obs[0][2]))
min_distances_dc.append(min(distances))
# Plot cost comparisons
plot_cost_comparisons(costs_dc, costs_cbf, gamma)
# Plot min distances comparison
plot_min_distance_comparison(min_distances_cbf, min_distances_dc, gamma)
print("Average cost over all experiments: cbf={}, dc={}".format(sum(costs_cbf)/len(costs_cbf), sum(costs_dc)/len(costs_dc)))
print("Average min distance over all experiments: cbf={}, dc={}".format(sum(min_distances_cbf)/len(min_distances_cbf),
sum(min_distances_dc)/len(min_distances_dc)))
def run_multiple_experiments(N):
"""Runs N experiments for each method."""
# Run experiments
cont_type = ["MPC-CBF", "MPC-DC"]
for c in cont_type:
config.controller = c
for i in range(N):
run_sim()
def run_sim():
"""Runs a simulation and saves the results."""
controller = MPC() # Define controller
controller.run_simulation() # Run closed-loop control simulation
save_mpc_results(controller) # Store results
def run_sim_for_different_gammas(gammas):
"""Runs simulation for the MPC-DC and for each gamma for the MPC-CBF."""
# Run simulation for the MPC-DC
config.controller = "MPC-DC"
run_sim()
# Run simulations for each gamma for the MPC-CBF
config.controller = "MPC-CBF"
for gamma in gammas:
config.gamma = gamma
run_sim()
def compare_results_by_gamma():
"""Runs simulations and plots path for each method and different gamma values."""
gammas = [0.1, 0.2, 0.3, 1.0] # Values to test
# Run simulations
run_sim_for_different_gammas(gammas)
# Load results
results = [load_mpc_results("MPC-DC_setpoint")]
for gamma in gammas:
filename_cbf = "MPC-CBF_setpoint_gamma" + str(gamma)
results.append(load_mpc_results(filename_cbf))
# Plot path comparison
plot_path_comparisons(results, gammas)