0
$\begingroup$

I am trying to write the code to solve a certain optimal control problem, but I keep running into an issue when I pass on my constraints to NMininimize[]. The following is my code:

(*PROBLEM DEFINITION*)
(*Inputs*)
t0 = 0;     (*Start Time*)
tf = 1;     (*End Time*)
time = {t0, tf};

nx = 2;     (*Number of States*)
nu = 1;     (*Number of Inputs*)

(*Initial conditions for system. Bounds if x0 is free s.t. x0l=< x0 <=x0u
If fixed, x0l == x0u*)
x0l = {0, 1};
x0u = {0, 1};

(*State bounds. xl=< x <=xu*)
limit = 1;
xl = {-Infinity, -Infinity};
xu = {limit, Infinity};

(*Terminal state bounds. xfl=< xf <=xfu. If fixed: xfl == xfu*)
xfl = {0, -1};
xfu = {0, -1};

(*Input Bounds*)
ul = -Infinity;
uu = Infinity;

(*Bounds on the first control action*)
u0l = -Infinity;
u0u = Infinity;

(*Define problem structures*)
states = <|"x0l" -> x0l, "x0u" -> x0u, "xl" -> xl, "xu" -> xu, "xfl" -> xfl,
 "xfu" -> xfu, "ul" -> ul, "uu" -> uu, "u0l" -> u0l, "u0u" -> u0u|>;
 
problem = <|"time" -> time, "nx" -> nx, "nu" -> nu, "states" -> states|>;

(*Define system dynamics*)
fx1[x_, u_, t_] := x[[2]];
fx2[x_, u_, t_] := u;
dynamics[x_, u_, t_] := {fx1[x, u, t], fx2[x, u, t]};

(*Define cost functions*)
lag[x_, u_, t_] := 0.5*(u^2); (*Define lagrangian cost*)
terminalCost[x_, u_, t_] := 0;             (*Terminal cost*)

nsteps = 10;
h = (tf - t0)/nsteps;
d = 5;
tau = Subdivide[t0, tf, nsteps]

(*Threshold*)
thresh = 1e-4

(*Problem related constants*)
Symbol["x"]
Symbol["beta"]

xarr = Array[x, {nx, Length[tau]}]
betaarr = Array[beta, Length[tau]]

(*Define basis vector and approximation function*)
phi[t_, taui_, h_, d_] := 1/Sqrt[Pi*d]*Exp[-(t-taui)^2/(D*h^2)];

Mhdx[t_, tau_, x_, h, d] := Dot[x, phi[t, tau, h, d]];

uhat[t_] := Mhdx[t, tau, betaarr, h, d];

uapp = {};
For[i = 1, i <= (nsteps + 1), i++, AppendTo[uapp, uhat[tau[[i]]]]];
uapp;
(*Calculating cost expressions*)
J = 0;
For[i = 1, i < nsteps+1, i++, J = J + lag[xarr[[;;, i]], uapp[[i]], tau[[i]]]*h];
J = J + terminalCost[xarr[[;;, -1]], uapp[[-1]], tau[[-1]]];

(*Constraints*)
xnextlist = {};
For[k = 1, k <nsteps+1, k++, 
xnext = xarr[[;;, k]] + dynamics[xarr[[;;, k]], uapp[[k]], tau[[k]]]*h;
AppendTo[xnextlist, xnext]];
xnextlist = Transpose[xnextlist];

xnextlist;
errlist = Abs[xnextlist - xarr[[;;, 2;;-1]]]
checkdynamicconstraints[err_] := AllTrue[err, # <= thresh &] (*Check is constraints based on dynamics satisfied*)

checkinitstateconstraints[x_] := x[[1, 1]] >= x0l[[1]] && x[[1, 1]] <= x0u[[1]] && x[[2, 1]] >= x0l[[2]] && x[[2, 1]] <= x0u[[2]];
checkfinalstateconstraints[x_] := x[[1, -1]] >= xfl[[1]] && x[[1, -1]] <= xfu[[1]] && x[[2, -1]] >= xfl[[2]] && x[[2, -1]] <= xfu[[2]]; 
checkstateconstraints[x_] := AllTrue[x[[1, 2;;-2]], (# >= xl[[1]] && # <= xu[[1]])&] && AllTrue[x[[2, 2;;-2]], (# >= xl[[2]] && # <= xu[[2]])&];

checkinitcontrolconstraints[u_] := u[[1]] >= u0l && u[[1]] <= u0u;
checkcontrolconstraints[u_] := AllTrue[u[[2;;-1]], # >= ul &] && AllTrue[u[[2;;-1]], # <= uu &];

solution = NMinimize[{J, checkcontrolconstraints[uapp], checkdynamicconstraints[errlist], checkfinalstateconstraints[xarr], checkinitcontrolconstraints[uapp], checkinitstateconstraints[xarr]}, Join[betaarr, xarr[[1]], xarr[[2]]]];

I'm facing the following error when I try to run this code:

NMinimize::bcons: The following constraints are not valid: {x[1,1]>=0,x[1,11]>=0,x[2,1]>=1,x[2,11]>=-1,Abs[x[1,1]-x[1,2]+1/10 x[2,1]]<=-4+e,<<18>>,Abs[1/10 (Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>]+Power[<<2>>] beta[<<1>>]+Power[<<2>>] Power[<<2>>] beta[<<1>>])+x[2,10]-x[2,11]]<=-4+e,x[1,1]<=0,x[1,11]<=0,x[2,1]<=1,x[2,11]<=-1}. Constraints should be equalities, inequalities, or domain specifications involving the variables.

I can see that all the constraints being passed on to NMinimize[] are inequalities, but I keep getting this error saying they aren't. How do I resolve this?

PS. This is my first time posting on StackExchange so forgive me for bad formatting.

$\endgroup$
3
  • 1
    $\begingroup$ Please show the NMinimize - code line too! $\endgroup$ Commented Mar 8, 2023 at 18:42
  • $\begingroup$ Is thresh = 1e-4 correct?!?!? Is using D as apparently a variable, when that name is a predefined function in Mathematica, correct ?!?! Or should that perhaps be d? If I change those to 10^-4 and d then the immediate error messages don't appear and it starts grinding away on the minimization. $\endgroup$ Commented Mar 8, 2023 at 20:05
  • $\begingroup$ Change thresh = 1e-4 to thresh = 10^-4 Change phi[..,d_] := ..-(t-taui)^2/(D*h^2) to phi[..,d_] := ..-(t-taui)^2/(d*h^2) Before NMinimize[{J,..},Join[..]] just display {J,...}//Simplify//InputForm Let e1,e2,... be your expressions. See you have {e1,e2,...}<= 1/10000 Change that to e1<1/10000&&e2<1/10000&&... Let c1,c2,... be your constraints. See you have {J,c1,c2,...} Change that to {J,c1&&c2&&...} After those changes see if your code is closer to working. $\endgroup$ Commented Mar 9, 2023 at 15:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.