HolyC0w

lab6_pt1_MPC_usingCasADi

Apr 12th, 2023
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.48 KB | None | 0 0
  1. #####  Python program for Model Predictive Control (MPC) using CasADi library:
  2.  
  3. import casadi as cs
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6.  
  7. # Define the system dynamics
  8. A = np.array([[1, 1], [0, 1]])
  9. B = np.array([[0], [1]])
  10. C = np.array([[1, 0], [0, 1]])
  11. D = np.array([[0], [0]])
  12.  
  13. # Define the MPC parameters
  14. N = 5
  15. dt = 0.1
  16. Q = np.diag([1, 1])
  17. R = np.array([[1]])
  18.  
  19. # Define the optimization problem
  20. opti = cs.Opti()
  21.  
  22. # Define the state variables
  23. x = opti.variable(2, N+1)
  24. x0 = opti.parameter(2, 1)
  25.  
  26. # Define the control variables
  27. u = opti.variable(1, N)
  28.  
  29. # Define the reference trajectory
  30. x_ref = opti.parameter(2, N+1)
  31. u_ref = opti.parameter(1, N)
  32.  
  33. # Define the initial state constraint
  34. opti.subject_to(x[:,0] == x0)
  35.  
  36. # Define the dynamic constraints
  37. for k in range(N):
  38.     x_next = cs.mtimes(A, x[:,k]) + cs.mtimes(B, u[:,k])
  39.     opti.subject_to(x[:,k+1] == x_next)
  40.  
  41. # Define the cost function
  42. J = 0
  43. for k in range(N):
  44.     J += cs.mtimes([(x[:,k] - x_ref[:,k]).T, Q, (x[:,k] - x_ref[:,k])])
  45.     J += cs.mtimes([(u[:,k] - u_ref[:,k]).T, R, (u[:,k] - u_ref[:,k])])
  46. opti.minimize(J)
  47.  
  48. # Define the control constraints
  49. opti.subject_to(u <= 1)
  50. opti.subject_to(u >= -1)
  51.  
  52. # Set the initial state parameter
  53. x0_val = np.array([[0], [0]])
  54. opti.set_value(x0, x0_val)
  55.  
  56. # Define the reference trajectory and control inputs
  57. x_ref_val = np.zeros((2, N+1))
  58. x_ref_val[0,:] = np.linspace(0, 1, N+1)
  59. u_ref_val = np.zeros((1, N))
  60. opti.set_value(x_ref, x_ref_val)
  61. opti.set_value(u_ref, u_ref_val)
  62.  
  63. # Simulate the system and plot the results
  64. x_val = np.zeros((2, N+1))
  65. u_val = np.zeros((1, N))
  66.  
  67. for i in range(N):
  68.     # Update the optimization problem with the current state
  69.     opti.set_initial(u, u_val)
  70.     opti.set_initial(x, x_val)
  71.  
  72.     # Solve the optimization problem
  73.     sol = opti.solve()
  74.  
  75.     # Extract the control input
  76.     u_val = opti.value(u[:,0])
  77.  
  78.     # Update the system state
  79.     x_val[:,i+1] = np.squeeze(cs.mtimes(A, x_val[:,i]) + cs.mtimes(B, u_val))
  80.  
  81. # Plot the results
  82. plt.plot(x_ref_val[0,:], x_ref_val[1,:], 'r--', label='Reference')
  83. plt.plot(x_val[0,:], x_val[1,:], 'b', label='MPC')
  84. plt.legend()
  85. plt.xlabel('x1')
  86. plt.ylabel('x2')
  87. plt.show()
  88.  
  89. #####################################################################
  90. #####################################################################
  91. import casadi as cs
  92. import numpy as np
  93. import matplotlib.pyplot as plt
  94.  
  95. # Define the system dynamics
  96. A = np.array([[1, 1], [0, 1]])
  97. B = np.array([[0], [1]])
  98. C = np.array([[1, 0], [0, 1]])
  99. D = np.array([[0], [0]])
  100.  
  101. # Define the MPC parameters
  102. N = 5  
  103. dt = 0.1
  104. Q = np.diag([1, 1])
  105. R = np.array([[1]])
  106.  
  107. # Define the optimization problem
  108. opti = cs.Opti()
  109.  
  110. # Define the state variables
  111. x = opti.variable(2, N+1)
  112. x0 = opti.parameter(2, 1)
  113.  
  114. # Define the control variables
  115. u = opti.variable(1, N)
  116.  
  117. # Define the reference trajectory
  118. x_ref = opti.parameter(2, N+1)
  119. u_ref = opti.parameter(1, N)
  120.  
  121. # Define the initial state constraint
  122. opti.subject_to(x[:,0] == x0)
  123.  
  124. # Define the dynamic constraints
  125. for k in range(N):
  126.     x_next = cs.mtimes(A, x[:,k]) + cs.mtimes(B, u[:,k])
  127.     opti.subject_to(x[:,k+1] == x_next)
  128.  
  129. # Define the cost function
  130. J = 0
  131. for k in range(N):
  132.     J += cs.mtimes([(x[:,k] - x_ref[:,k]).T, Q, (x[:,k] - x_ref[:,k])])
  133.     J += cs.mtimes([(u[:,k] - u_ref[:,k]).T, R, (u[:,k] - u_ref[:,k])])
  134. opti.minimize(J)
  135.  
  136. # Define the control constraints
  137. opti.subject_to(u <= 1)
  138. opti.subject_to(u >= -1)
  139.  
  140. # Set the initial state parameter
  141. x0_val = np.array([[0], [0]])
  142. opti.set_value(x0, x0_val)
  143.  
  144. # Define the reference trajectory and control inputs
  145. x_ref_val = np.zeros((2, N+1))
  146. x_ref_val[0,:] = np.linspace(0, 1, N+1)
  147. u_ref_val = np.zeros((1, N))
  148. opti.set_value(x_ref, x_ref_val)
  149. opti.set_value(u_ref, u_ref_val)
  150.  
  151. # Simulate the system and plot the results
  152. x_val = np.zeros((2, N+1))
  153. u_val = np.zeros((1, N))
  154.  
  155. for i in range(N):
  156.     # Update the optimization problem with the current state
  157.     opti.set_initial(u, u_val)
  158.     opti.set_initial(x, x_val)
  159.  
  160.     # Solve the optimization problem
  161.     opti.solver('ipopt')
  162.     sol = opti.solve()
  163.  
  164.     # Extract the control input
  165.     u_val = opti.value(u[:,0])
  166.  
  167.     # Update the system state
  168.     x_val[:,i+1] = np.squeeze(cs.mtimes(A, x_val[:,i]) + cs.mtimes(B, u_val))
  169.  
  170. # Plot the results
  171. plt.plot(x_ref_val[0,:], x_ref_val[1,:], 'r--', label='Reference')
  172. plt.plot(x_val[0,:], x_val[1,:], 'b', label='MPC')
  173. plt.legend()
  174. plt.xlabel('x1')
  175. plt.ylabel('x2')
  176. plt.show()
  177.  
Advertisement
Add Comment
Please, Sign In to add comment