-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
66 lines (52 loc) · 2.2 KB
/
example.py
File metadata and controls
66 lines (52 loc) · 2.2 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
import ipopt
from pyomeca import Analogs3d
from matplotlib import pyplot as plt
from OpensimStaticOptimization import *
# ---- SETUP ---- #
# Paths
model_path = f"arm26.osim"
mot_path = f"arm26_InverseKinematics.mot"
low_pass_filter_param = None # (4, 6) is also usually performed
use_muscle_physiology = False
# Optimization type
# so_model = ClassicalStaticOptimization(model_path, mot_path, low_pass_filter_param=low_pass_filter_param)
so_model = ClassicalOptimizationLinearConstraints(model_path, mot_path,
low_pass_filter_param=low_pass_filter_param,
use_muscle_physiology=use_muscle_physiology)
# Optim options
activation_initial_guess = np.zeros([so_model.n_muscles])
lb, ub = so_model.get_bounds()
# --------------- #
prob = ipopt.problem(
n=so_model.n_muscles, # Nb of variables
lb=lb, # Variables lower bounds
ub=ub, # Variables upper bounds
m=so_model.n_dof, # Nb of constraints
cl=np.zeros(so_model.n_dof), # Lower bound constraints
cu=np.zeros(so_model.n_dof), # Upper bound constraints
problem_obj=so_model # Class that defines the problem
)
prob.addOption('tol', 1e-7)
prob.addOption('print_level', 0)
# Prepare the optimisation
activations = list()
for frame in range(so_model.nFrame):
so_model.upd_model_kinematics(frame)
# Optimize
try:
x, info = prob.solve(activation_initial_guess)
except RuntimeError:
print(f"Error while computing the frame {frame}.")
x = np.ndarray(activation_initial_guess.shape) * np.nan
# The answer is the initial guess for next frame
activation_initial_guess = x
activations.append(x)
print(f"time = {so_model.get_time(frame)}, Performance = {info.get('obj_val')}, "
f"Constraint violation = {np.linalg.norm(info.get('g'))}")
data_from_python = Analogs3d(np.array(activations))
data_from_GUI = Analogs3d.from_csv("arm26_StaticOptimization_activation.sto",
delimiter='\t', time_column=0, header=7, first_column=1, first_row=8)
data_from_python.plot()
data_from_GUI.plot()
(data_from_python - data_from_GUI).plot()
plt.show()