Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.

24 views (last 30 days)
The following is the script which I run the fmincon solver in. I am not sure whether the error is resulting because the non-linear constraint is incorrectly defined or whether my initial value x0 does not satisfy the constraints.
X = [0,0,0];
u = [0,0,0];
v_fb = [-4.2426,0,-0.7854];
n = 9;
deltaT = 0.1;
n_t = 1;
X_h_o = [2,2,0,0];
pos = safetyModule(X, u, v_fb, n, deltaT, n_t, X_h_o)
Array indices must be positive integers or logical values.

Error in solution>CBFcon (line 88)
c(i) = -(((X(i) - x_curr)/sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 )) * X(i + 3*n) + ((X(i+n) - y_curr)/sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 )) * X(i + 4*n) + alpha(sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 ) - D_obs));

Error in solution>@(X_h)CBFcon(X_h,n,deltaT,X_h_o,alpha,D_obs) (line 56)
nonlcon = @(X_h) CBFcon(X_h, n, deltaT, X_h_o, alpha, D_obs);

Error in fmincon (line 650)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});

Error in solution>safetyModule (line 65)
dec = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon);

Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
function pos = safetyModule(X, u, v_fb, n, deltaT, n_t, X_h_o)
%extend system state to match number of discretized time points in
%horizon including t = 0
X_h = [X, u, zeros(1, 6 * (n-1))];
%initial guess (may not be feasible)
x0 = X_h;
%define constants
D_obs = 0.5; %margin of safety
alpha = 1;
%adjust variables
n_t = n_t + 1;
%decompose v_fb
xdot_fb = v_fb(1);
ydot_fb = v_fb(2);
w_fb = v_fb(3);
%objective function
fun = @(X_h) 0;
for i = 1:n
fun1 = abs(X_h(i+3*n) - xdot_fb) + abs(X_h(i+4*n) - ydot_fb) + abs(X_h(i+5*n) - w_fb);
fun = @(X_h) fun(X_h) + fun1;
end
%equality constraints
Aeq = zeros(3*n, 6 * n);
Aeq(1,1) = 1;
Aeq(2,1+n) = 1;
Aeq(3,1+2*n) = 1;
for i = 1:n-1
Aeq(3+i, i) = -1; Aeq(3+i,i+1) = 1; Aeq(3+i,i+n*3) = -deltaT;% x dynamics
Aeq(2 + n + i, n + i) = -1; Aeq(2 + n + i, n + i + 1) = 1; Aeq(2 + n + i, 4 * n + i) = -deltaT;% y dynamics
Aeq(1 + 2*n + i, 2* n + i) = -1; Aeq(1 + 2*n + i, 2 * n + i + 1) = 1; Aeq(1 + 2*n + i, 5 * n + i) = -deltaT;% z dynamics
end
beq = zeros(3*n, 1);
beq(1)=X_h(1);
beq(2)=X_h(2);
beq(3)=X_h(3);
%inequality constraints
nonlcon = @(X_h) CBFcon(X_h, n, deltaT, X_h_o, alpha, D_obs);
A = [];
b = [];
%bounds
ub = [inf(3*n, 1), 2.5 * ones(3*n, 1)];
lb = [-inf(3*n, 1), -2.5 * ones(3*n, 1)];
%run the solver
dec = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon);
%extract xdot, ydot, and w for first n_p timesteps
pos = zeros(1, (n_t - 1)* 3);
j = 1;
for i = 2:n_t
pos(j) = dec(i);
pos(j+1) = dec(i+n);
pos(j+2) = dec(i+2*n);
j = j + 3;
end
end
I define my nonlinear constraint in a seperate file:
function [c, ceq] = CBFcon(X, n, deltaT, X_o, alpha, D_obs)
x_curr = X_o(1);
y_curr = X_o(3);
x_vel = X_o(2);
y_vel = X_o(4);
for i = 1:n
c(i) = -(((X(i) - x_curr)/sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 )) * X(i + 3*n) + ((X(i+n) - y_curr)/sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 )) * X(i + 4*n) + alpha(sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 ) - D_obs));
x_curr = x_curr + x_vel * deltaT;
y_curr = y_curr + y_vel * deltaT;
end
ceq = [];
end
  4 Comments
Dhruv
Dhruv on 23 Mar 2024
Instead of providing you with the script in which safety Module is called, I think it will be more beneficial to share the values of the input values when safety module is called. Attached is a picture of the workspace variable values.
Catalytic
Catalytic on 23 Mar 2024
Edited: Catalytic on 23 Mar 2024
Yes, but providing the values as a screenshot will not work because -
  1. It cannot be copy-pasted.
  2. It doesn't gives us the values accurately, because the screenshot only displays them to four decimal places
Please attach a .mat file with the values.

Sign in to comment.

Answers (1)

Torsten
Torsten on 23 Mar 2024
Most probably you mean
c(i) = -(((X(i) - x_curr)/sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 )) * X(i + 3*n) + ((X(i+n) - y_curr)/sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 )) * X(i + 4*n) + alpha*(sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 ) - D_obs));
instead of
c(i) = -(((X(i) - x_curr)/sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 )) * X(i + 3*n) + ((X(i+n) - y_curr)/sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 )) * X(i + 4*n) + alpha(sqrt((X(i) - x_curr)^2 + (X(i+n) - y_curr)^2 ) - D_obs));
  25 Comments
Dhruv
Dhruv on 28 Mar 2024
Edited: Dhruv on 28 Mar 2024
I have attached the most up to date versions of my program. Here is a brief summary of how it works:
**velocities are represented as xdot, ydot
1) Field constraints are set for the particle, goal and obstacles
2) depending on distance between particle and goal, trajectoryController.m returns v_fb
3)v_fb then needs to be refined before being applied to the particle, because v_fb did not take obstacles into account
3) safetyModule includes the objectve function aiming to reduce the difference between v_fb and the actual v applied to the particle over the next n horizon timesteps (horizon timesteps are used for the optimization solver while actual timesteps are referenced for actually plotting the robot position on the graph. constraints include:
  • equality constraint- initial position and velocity must be equal to current position and velocity of particle
  • equalilty constraint- basic equations of motion (d= r*t) describing evolution of position over the n hoirzon timesteps
  • inequality constraint- acceleration (how much velocity variables can vary from one horizon timestep to the next)
  • upper bounds and lower bounds for velocity
  • nonlinear constraint- safety condition ensuring no colliisions with the obstalces occur
4) Once the optimization program has run the, the x,y posiiton of the robot at the "next" horizon timestep can be plotted in trajectoryTracker, and the x,y velocity at the "next" horizon timestep can be stored to be fed into the safety module at the next actual timestep.
5) The timestep is iterated and the procedure is repeated until the particel reached the goal or simulation times runs out
Now if you run my code, you will be able to see the following data for each timestep in the simulation (h represents value of the expression included in the nonlinear constraint). In certain timesteps (18 in this case) the equality constraint is randomly violated as you can see the x,xdot,y,ydot values for t(horizon) = 2, which were applied to the robot in the previous timestep 17 do not match the corresponding values for t(horizon) = 1 for timestep 18.
I HAVE NO IDEA WHY THIS CONSTRAINT IS RANDOMLY VIOLATED. It seems like often times but not always when, I reduce the number of horizon timesteps (n), the issue is fixed. Still don't understand why... I will gladly explain the code more, but I am just lost since I have no previous experience with solver such as fmincon.
Torsten
Torsten on 28 Mar 2024 at 11:40
Edited: Torsten on 28 Mar 2024 at 11:41
Here are the meanings of the exitflags from the documentation available under
exitflag — Reason fmincon stopped
integer
Reason fmincon stopped, returned as an integer.
All Algorithms:
1
First-order optimality measure was less than options.OptimalityTolerance, and maximum constraint violation was less than options.ConstraintTolerance.
0
Number of iterations exceeded options.MaxIterations or number of function evaluations exceeded options.MaxFunctionEvaluations.
-1
Stopped by an output function or plot function.
-2
No feasible point was found.
All algorithms except active-set:
2
Change in x was less than options.StepTolerance and maximum constraint violation was less than options.ConstraintTolerance.
trust-region-reflective algorithm only:
3
Change in the objective function value was less than options.FunctionTolerance and maximum constraint violation was less than options.ConstraintTolerance.
active-set algorithm only:
4
Magnitude of the search direction was less than 2*options.StepTolerance and maximum constraint violation was less than options.ConstraintTolerance.
5
Magnitude of directional derivative in search direction was less than 2*options.OptimalityTolerance and maximum constraint violation was less than options.ConstraintTolerance.
interior-point, sqp-legacy, and sqp algorithms:
-3
Objective function at current iteration went below options.ObjectiveLimit and maximum constraint violation was less than options.ConstraintTolerance.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!