-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvdp.py
More file actions
85 lines (68 loc) · 3.11 KB
/
vdp.py
File metadata and controls
85 lines (68 loc) · 3.11 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
import os
import numpy as np
import matplotlib.pyplot as plt
from wedoco_optimo.model import OptimoModel
def run_vdp(force_recompile=True, plot=True):
# Compile and transfer the Modelica model
mo = OptimoModel()
model_file = os.path.join(os.path.dirname(__file__), "vdp.mo")
mo.transfer_model(model="vdp", modelica_files=[model_file], force_recompile=force_recompile)
# Simulate
res_sim_df = mo.simulate()
# Plot
if plot:
_, axs = plt.subplots(3, 1)
res_sim_df[["x1","x2"]].plot(ax=axs[0], title="States", legend=True)
res_sim_df["objectiveIntegrand"].plot(ax=axs[1], title="Objective", legend=True)
res_sim_df["u"].plot(ax=axs[2], title="Input", legend=True)
plt.show()
# Optimize
mo.define_optimization(constraints={"u":(-1, 0.75)},
objective_terms=["objectiveIntegrand"])
res_ocp_df = mo.optimize(calculate_objective_terms_integrals=False)
# Plot
if plot:
_, axs = plt.subplots(3, 1)
res_ocp_df[["x1","x2"]].plot(ax=axs[0], title="States", legend=True)
res_ocp_df["objectiveIntegrand"].plot(ax=axs[1], title="Objective", legend=True)
res_ocp_df["u"].plot(ax=axs[2], title="Input", legend=True)
plt.show()
return res_sim_df, res_ocp_df
def run_vdp_simulate_with_input(force_recompile=False, plot=True):
# Compile and transfer the Modelica model
mo = OptimoModel()
model_file = os.path.join(os.path.dirname(__file__), "vdp.mo")
mo.transfer_model(model="vdp", modelica_files=[model_file], force_recompile=force_recompile)
# Simulate with an input trajectory
u_traj = np.sin(mo.tgrid)
res_sim_df = mo.simulate(u_sim=u_traj)
# Plot
if plot:
_, axs = plt.subplots(3, 1)
res_sim_df[["x1","x2"]].plot(ax=axs[0], title="States", legend=True)
res_sim_df["objectiveIntegrand"].plot(ax=axs[1], title="Objective", legend=True)
res_sim_df["u"].plot(ax=axs[2], title="Input", legend=True)
plt.show()
return res_sim_df
def run_vdp_optimize_with_input(force_recompile=False, plot=True):
# Compile and transfer the Modelica model
mo = OptimoModel()
model_file = os.path.join(os.path.dirname(__file__), "vdp_prescribed_input.mo")
mo.transfer_model(model="vdpPrescribedInput", modelica_files=[model_file], force_recompile=force_recompile)
# Optimize with an input trajectory
u_traj = np.sin(mo.tgrid)
mo.define_optimization(constraints={"u":(-1, 0.75)},
objective_terms=["objectiveIntegrand"],
prescribed_inputs={"u_prescribed": u_traj})
res_ocp_df = mo.optimize(calculate_objective_terms_integrals=False)
# Plot
if plot:
_, axs = plt.subplots(3, 1)
res_ocp_df[["x1","x2"]].plot(ax=axs[0], title="States", legend=True)
res_ocp_df["objectiveIntegrand"].plot(ax=axs[1], title="Objective", legend=True)
res_ocp_df["u"].plot(ax=axs[2], title="Inputs", legend=True)
res_ocp_df["u_prescribed"].plot(ax=axs[2], legend=True)
plt.show()
return res_ocp_df
if __name__ == "__main__":
run_vdp()