Repository navigation
Set of constraints raises Error #702
Description
Activity
A couple of issues that might be relevant:
- You are using
input_range_constraintsto constrain some of the inputs and not others. Depending on the optimizer, this might now work (this is the first warning you are getting). You may need to useinput_poly_constraintsto just constrain the inputs that you care about. - You are using the
methodkeyword insolve_ocp, but there is no such keyword. You want to useminimize_method(andminimize_options). This one is tricky because there is no warning (I found it because I have put in a warning in a PR that I am working on).
- You are using
Thank you very much. The second comment described a keyword issue in my uploaded code, which should be fixed.
Regarding your first comment, is there an example where the constraint structures are used in practice?
This is not a very good answer, but you can find some examples in the unit test code: control/tests/optimal_test.py.
Thank you very much! I will be closing the issue now. In case there is any example of use of these constraints in similar examples, I will be delighted to learn about it.
I am reopening this issue since I have now improved my opt.solve_ocp() command and wish to fully comply with the documentation of the control library which is otherwise producing results very reliably. As described above, I get the same error messages about the constraints, but the "SLSQP" solver solves correctly. I am using SLSQP solver since my constraints are linear.
This is the part of the code that has changed:
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]) ] result = opt.solve_ocp( rocket, horizon, x0, cost, con1, terminal_constraints=terminal, initial_guess=bend_left, log=False, minimize_method="SLSQP", minimize_options={'ftol': 0.000001, 'eps': np.finfo(float).eps, "maxiter": 500, "disp":True})And this is the error message:
/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 "How can I correctly define multiple constraints on inputs only and then on states only, in the form: input_constraint_1, input_constraint_2, ... input_constraint_n , state_constraint_1, state_constraint_2, ..., state_constraint_n, and then pass them into the opt.solve_ocp().
I am attempting to promote this excellent solver in my thesis, that is the reason for my need to fully comply with the "grammar" structure of the library.
Thank you for your time.
The following should work
# Reformulation A = np.array([ [ 1, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 0], [ 0, 0, 1, 0, 0, 0], [ 0, 0, -1, 0, 0, 0], ]) b = np.array([Final_x, -Final_x, Final_z, -Final_z]) term_con2 = opt.state_poly_constraint(rocket, A, b) result2 = opt.solve_ocp( rocket, horizon, x0, cost3, con1, terminal_constraints=term_con2, initial_guess=bend_left, log=False, minimize_method="SLSQP", return_x = True, minimize_options={ 'ftol': 0.000001, 'eps': np.finfo(float).eps, "maxiter": 500, "disp":True}) print("* Final state:", result2.states[:, -1])The output that I get (using a slightly newer devleopment version of the code, so some details may be different) is:
Optimization terminated successfully (Exit mode 0) Current function value: 0.0 Iterations: 76 Function evaluations: 1628 Gradient evaluations: 76 Summary statistics: * Cost function calls: 1628 * Constraint calls: 1722 * System simulations: 2937 * Final cost: 0.0 * Final state: [ 5.00000044e+00 2.20695572e+01 2.00000000e+02 2.54755631e+01 5.57648576e-01 -1.04469522e-01]I think this addresses the issue. Re-open is not.
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?
Error: