from casadi import * import numpy as np from utilities import MPC import matplotlib.pyplot as plt from Dynamic_system_small import Bio_reactor as System #specifications, DAE_system, integrator_model # System = Bio_reactor # System_unc = Bio_reactor_unc N = 6 # This is the total steps #Initialize system Sys = System() F = Sys.integrator_model() # Construct MPC MPC_ = MPC(System, horizon=N, penalize_u=False) # Define system dt, x0, _, _, _ = Sys.specifications() # Initialize Matrices N_mc = 1 his_x = np.zeros([N_mc, 1000, len(x0)]) his_u = np.zeros([N_mc, 1000, 2]) u_apply = np.array([0.]) u1 = np.array([ 265.15893977, 269.47831514, 271.16360361, 220.6355221, 301.72600227, 120.00073468 ]).reshape((1, -1)) u2 = np.array([ 25.23242466, 20.31618808, 39.68463853, 39.999973, 39.99999722, 39.99999775 ]).reshape((1, -1)) * 0.8 uu = np.vstack((u1, u2)) for kk in range(N_mc):
T = 1. # Time horizon N = 12 # number of control intervals # nk, tf, x0, Lsolver, c_code = specifications() # # xd, xa, u, uncertainty, ODEeq, Aeq, u_min, u_max, states, \ # algebraics, inputs, nd, na, nu, nmp, modparval, ng, gfcn, Obj_M, Obj_L= DAE_system() # # Objective term # Formulate discrete time dynamics # Fixed step Runge-Kutta 4 integrator MPC_ = MPC(System, 100, penalize_u=False, collocation_degree=8) Sys = System() dt, x0, _, _, _ = Sys.specifications() F = Sys.integrator_model() his_x = np.zeros([1000, len(x0)]) his_u = np.zeros([1000, 1]) t = 0. u_apply = np.array([0.]) for i in range(1000): his_x[i] = x0 u_opt, x_opt, w_opt = MPC_.solve_MPC(x=x0, u=u_apply, ref=0.4, t=t) u_apply = np.array(u_opt)[:, 0] his_u[i] = u_apply x1 = F(x0=x0, p=u_apply)
from casadi import * import numpy as np from Dynamic_system import polymer_CSTR as System #specifications, DAE_system, integrator_model from utilities import MPC import matplotlib.pyplot as plt import time MPC_ = MPC(System, 1, penalize_u=False, collocation_degree=8) Sys = System() dt, x0, _, _, _ = Sys.specifications() F = Sys.integrator_model() his_x = np.zeros([1000, len(x0)]) his_u = np.zeros([1000, 1]) t = 0. u_apply = np.array([0.1675]) his_t = np.zeros([1000]) for i in range(1000): his_x[i] = x0 start = time.time() u_opt, x_opt, w_opt = MPC_.solve_MPC(x=x0, u=u_apply, ref=[ 3.0780178085943333, 0.14874773146466175, 0.019293038350734066 * 100, 292.54885691405192 / 100 ], t=t) # his_t[i] = time.time() - start u_apply = np.array(u_opt)[:, 0] his_u[i] = u_apply x1 = F(x0=x0, p=u_apply)
from casadi import * import numpy as np from Dynamic_system import Semi_Batch as System #specifications, DAE_system, integrator_model from utilities import MPC import matplotlib.pyplot as plt MPC_ = MPC(System, 20, penalize_u=True, collocation_degree=8) # solver = nlpsol('solver', 'ipopt', prob)#'bonmin', prob, {"discrete": discrete})#'ipopt', prob, {'ipopt.output_file': 'error_on_fail'+str(ind)+'.txt'})# # # # Solve the NLP # sol = solver(x0=w0, lbx=lbw, ubx=ubw, lbg=lbg, ubg=ubg, p = np.array([1.,100.,0.])) Sys = System() dt, x0, _, _, _ = Sys.specifications() F = Sys.integrator_model() his_x = np.zeros([1000, len(x0)]) his_u = np.zeros([1000, 2]) u_apply = np.array([0., 0.]) t = 0. for i in range(20): his_x[i] = x0 u_opt, x_opt, w_opt = MPC_.solve_MPC(x=x0, u=u_apply, t=t) u_apply = np.array(u_opt)[:, 0] his_u[i] = u_apply x1 = F(x0=x0, p=u_apply) x0 = np.array(x1['xf']).reshape((-1, )) t += dt plt.plot(his_x[:20, 0])
N = 6 # number of control intervals # nk, tf, x0, Lsolver, c_code = specifications() # # xd, xa, u, uncertainty, ODEeq, Aeq, u_min, u_max, states, \ # algebraics, inputs, nd, na, nu, nmp, modparval, ng, gfcn, Obj_M, Obj_L= DAE_system() # # Objective term # Formulate discrete time dynamics # Fixed step Runge-Kutta 4 integrator M = 4 # RK4 steps per interval MPC_ = MPC(System, 12, penalize_u=False) u_opt, x_opt, w_opt = MPC_.solve_MPC(np.array([1, 150, 0]), t=0.) # solver = nlpsol('solver', 'ipopt', prob)#'bonmin', prob, {"discrete": discrete})#'ipopt', prob, {'ipopt.output_file': 'error_on_fail'+str(ind)+'.txt'})# # # # Solve the NLP # sol = solver(x0=w0, lbx=lbw, ubx=ubw, lbg=lbg, ubg=ubg, p = np.array([1.,100.,0.])) Sys = System() dt, x0, _, _, _ = Sys.specifications() F = Sys.integrator_model() his_x = np.zeros([1000, len(x0)]) his_u = np.zeros([1000, 2]) t = 0. for i in range(6): his_x[i] = x0 u_opt, x_opt, w_opt = MPC_.solve_MPC(x=x0, t=t)