- Notifications
You must be signed in to change notification settings - Fork441
Closed
Milestone
Description
I am using the python optimal control library to optimise the trajectory of a 2D rocket. Please find attached a minimum reproducible sample code of the optimisation process of the problem. Whenever I am trying to set constraints for only some of the states, I get the error described after the minimum reproducible example. Is there an appropriate way to define multiple separate constraints passed in the "constraints= [..., ..., ...]" argument of the solve.ocp() function? Also, instead of using range -np.inf to np.inf for state parameters that I do not want to bound, is there a value that makes the code ignore the specific state parameter constraints?
T_max_Single = 7607000/9g = 9.81 m = 114239.00k = 18.74I = 16071705.59import numpy as npimport mathimport control as ctimport control.optimal as optimport matplotlib.pyplot as pltimport loggingimport timeimport osplt.style.use("default")from IPython.display import displayfrom colorama import *import sympy as sfrom scipy.integrate import odeint#Definition of the statesdef rocket_update(t, x, u, params): xpos = x[0] U = x[1] z = x[2] W = x[3] θ = x[4] q = x[5] T = u[0] a = u[1] # Return the derivative of the state dydt = np.array([U, T*np.cos(θ-a)/m, W, T*np.sin(θ-a)/m-g, q, T*k*np.sin(a)/I]) return(dydt)def rocket_output(t, x, u, params): return x # return (full state)Tf = 8# Define the rocket movement dynamics as an input/output systemrocket = ct.NonlinearIOSystem( rocket_update, rocket_output, states=6, name='rocket', inputs=("T", "α"), outputs=("x", "U", "z", "W", "θ", "q")) # Define the time horizon (and spacing) for the optimizationhorizon = np.linspace(0, Tf, Tf, endpoint=True) #############IMPORTANT# Provide an intial guess (will be extended to entire horizon)bend_left = [T_max_Single, 0.01] # slight left veer #############IMPORTANT# Optimal Control ProblemInitial_x = 0 #mFinal_x = 5 #mInitial_z = 0 #mFinal_z = 200 #mInitial_θ = np.pi/2 #mFinal_θ = np.pi/3 #mFinal_Thrust = T_max_Singlex0 = [Initial_x, 0, Initial_z, 0, Initial_θ, 0]; u0 = [bend_left[0], bend_left[1]]xf = [Final_x, 0, Final_z, 0, Final_θ, 0]; uf = [Final_Thrust, 0]#T_max_SingleMin_T = 1 * T_max_SingleMax_T = 9 * T_max_Singlea_min = -20* np.pi/180a_max = 20* np.pi/180# Approach 3: terminal constraints## We can also remove the cost function on the state and replace it# with a terminal *constraint* on the state. If a solution is found,# it guarantees we get to exactly the final state.#print("Initialisation of optimisation") # print("Checkpoint_1")# Input cost and terminal constraintsR = np.diag([0, 0]) # minimize applied inputscost3 = opt.quadratic_cost(rocket, np.zeros((6,6)), R, u0=uf)con1 = [ opt.input_range_constraint(rocket, [Min_T, a_min], [Max_T, a_max]) ]terminal = [ opt.state_range_constraint(rocket, [Final_x, -np.inf, Final_z, -np.inf, -np.inf, -np.inf], [Final_x, np.inf, Final_z, np.inf, np.inf, np.inf]) ]# Reset logging to its default valueslogging.basicConfig( level=logging.DEBUG, filename="./steering-terminal_constraint.log", filemode='w', force=True)# Compute the optimal controlstart_time = time.process_time()result3 = opt.solve_ocp( rocket, horizon, x0, cost3, con1, terminal_constraints=terminal, initial_guess=bend_left, log=False, method="trust-constr", options={'eps': 0.01})print("* Total time = %5g seconds\n" % (time.process_time() - start_time))
Error:
/opt/anaconda3/lib/python3.8/site-packages/scipy/optimize/_constraints.py:386: OptimizeWarning: At least one constraint is unbounded above and below. Such constraints are ignored. warn("At least one constraint is unbounded above and below. Such "/opt/anaconda3/lib/python3.8/site-packages/scipy/optimize/_constraints.py:432: OptimizeWarning: Equality and inequality constraints are specified in the same element of the constraint list. For efficient use with this method, equality and inequality constraints should be specified in separate elements of the constraint list. warn("Equality and inequality constraints are specified in the same "