Possible bug in coneprog.m function

169 views (last 30 days)
chicken vector
chicken vector on 28 Oct 2025 at 10:22
Edited: Torsten on 31 Oct 2025 at 19:06
I am running a trajectory optimisation problem.
The formulation is convexified via slack variables and linearisation and I was testing coneprog as solver.
I noticed some issues with the function that I would like to report and verify if it is my mistake or a bug.
My code is completely deterministic. There is no randomisation and the execution should be 100% reproducible.
Nevertheless, running the same code multiple times with the same input and options leads to different results.
Unlinke fmincon, with coneprog it is impossible to access internal calculation and to understand what is going on, therefore I do not have many insights.
My guess is that a random noise is applied to the initial guess.
In theory this should not be a problem since a convex problem has only one minimum.
In practice, the solver sometimes finds an optimal solution and other times declares the problem unfeasible.
I can solve the same exact problem with an NLP formulation using fmincon, so I know for sure the proble is solvable.
I tried to change the initial conditions and the problem persists.
Sometimes, different minima are found in different calls of the solver.
This is not due to the presence of a flat in the solution space because the fval differ by 1 to 2 orders of magnitude.
Another issue I am having is connected to the iterative procedure I have implemented.
At first I obtain an anlytical initial guess that is numerically propagated.
The constraints are linearised around this reference trajectory and the coneprog is called to solve the problem.
Once the optimal control is obtained, the solution is again propagated and if the error is not below the specified tolerance the process is repeated iteratively.
For some initial conditions the solver works smoothly, but for other it stops at the 5-th or 6-th iteration (declaring the problem unfeasible) when it is really close to the finalise the solution.
Again, it is important to highlight that I am 100% confident that the problem is feasible.
It is important to highlight that some degree of reproducibility is still present.
Even if the solver behaves differently, the number of possible behaviours is limited to just a few outcomes, depending on the initial conditions.
In general, for every problem there are 2 or 3 possible outcomes.
Moreover, some outcomes may occur more frequently than others.
I have spent days trying to find the issue in my code, but now, after testing with fmincon, I am starting to be confident that the problem is in coneprog.
I have also noticed that the solver behaves differently depending on the optimalityTolerance.
This was later confirmed when I found out that the problem has already been reported on this forum here.
An other bug I noticed in some solutions is that both Primal and Dual Feasibility report a value, but if I check manually the constraint satisfaction, the feasibility is off by several order of magnitudes.
I am 100% confident that the check I performed is correct because it was performed with a breakpoint placed right after the call to coneprog as shown below.
I have also stored the inputs of coneprog right before the function was called.
In my opinion, if the problem is coneprog as I think it is, the bug is related to the update step of the current guess.
Have you experienced similar problem with coneprog, or are you aware of some limitations it may have?
Did you already encountered such problem and understood where it is coming from?
The main motivations why I think coneprog is the problem are the different outputs produced from a deterministic code, and the fact that, when the problem converges, the solution is correct.
As a final comment, I think it would be really useful to have the freedom of specifying an initial guess.
If I know already a solution that is both an interior point and very close to the optimal one, this would cut down considerably the computational time.
----------------------------------------------------------------------------------------------------------------------------
Below you can find an example of the solver converging to an unfeasible point after some iterations.
The target value of poc is 1e-10.
The formulation of the optimsiation problem is based on a direct transcription to discretise a continuous trajectory into discrete nodes.
The solution vector x is built as:
x = [ux1 uy1 uz1 u1 ... uxN uyN uzN uN b1 b2]
where:
  • [uxi uyi uzi] is the control vector at the i-th node.
  • ui is the magnitude of the control vector at the i-th node.
  • [b1 b2] are some end conditions that must satisfy a linear constraint
The only cone constraints present are the one that impose:
  • sqrt(uxi^2 + uyi^2 + uzi^2) <= 1
  • sqrt(uxi^2 + uyi^2 + uzi^2) <= ui
The linear objective function minimises the control effort defined as:
In the example below, the trajectory is discretised into 852 nodes.
This is clear from the initial Fval of every iteration that corresponds to the number of nodes, as the initial guess is probably all ones.
The last call of coneprog declares the problem unfeasible and it is the only iteration where Fval increases after the initial guess.
The problem with the constraint satisfication can be understood from the values of the Infeasibilities.
To help the problem converges I defined a lower bound to uxi, uyi, and uzi of -1, and a lower bound to ui of 0.
Similarly, I defined an upper bound to uxi, uyi, uzi, and ui of 1.
An Fval of 1.118894e+03 in the last line of the example below, clearly goes against the upper bound constraint, as the maximum value of Fval allowed by the upper bound is 852.
Instead the sovler is analysing a solution with Fval of approximately 1119, but it thinks that the constraints are satisfied, which is impossible.
Maybe my interpretation of Primal and Dual Feasibility is wrong, but my intuition is suggesting me that coneprog may have some problems.
Iter Fval Primal Infeas Dual Infeas Duality Gap Time
0 8.520000e+02 3.351509e-01 2.500000e-01 8.338790e-02 0.43
1 2.903535e+02 5.653968e-02 4.217479e-02 1.406747e-02 0.46
2 5.760140e+01 1.155766e-02 8.621236e-03 2.875627e-03 0.50
3 1.826360e+01 3.567321e-03 2.660981e-03 8.875744e-04 0.54
4 1.587390e+01 2.115580e-03 1.578080e-03 5.263711e-04 0.58
5 1.496351e+01 1.979895e-03 1.476868e-03 4.926117e-04 0.61
6 1.477297e+01 1.930240e-03 1.439829e-03 4.802573e-04 0.65
7 1.407791e+01 1.740965e-03 1.298643e-03 4.331644e-04 0.69
8 1.195150e+01 1.139652e-03 8.501032e-04 2.835533e-04 0.73
9 1.118074e+01 9.249616e-04 6.899590e-04 2.301369e-04 0.76
10 9.263514e+00 3.932808e-04 2.933610e-04 9.785102e-05 0.80
11 8.435984e+00 1.662211e-04 1.239898e-04 4.135699e-05 0.84
12 8.137082e+00 8.259246e-05 6.160841e-05 2.054958e-05 0.89
13 7.834045e+00 4.811473e-06 3.589036e-06 1.197128e-06 0.93
14 7.817072e+00 4.211285e-07 3.141435e-07 1.047797e-07 0.97
15 7.815469e+00 1.297491e-08 9.741467e-09 3.228250e-09 1.02
16 7.815429e+00 2.773558e-09 3.724366e-09 6.900807e-10 1.05
Optimal solution found.
poc =
4.7669e-05
Iter Fval Primal Infeas Dual Infeas Duality Gap Time
0 8.520000e+02 4.148309e-01 2.500000e-01 8.339003e-02 0.24
1 2.936365e+02 7.220152e-02 4.351262e-02 1.451408e-02 0.28
2 3.227398e+01 9.133979e-03 5.504640e-03 1.836128e-03 0.32
3 2.999204e+01 5.626122e-03 3.390612e-03 1.130973e-03 0.36
4 2.891739e+01 5.422358e-03 3.267812e-03 1.090012e-03 0.40
5 2.734221e+01 4.962369e-03 2.990597e-03 9.975440e-04 0.44
6 2.355320e+01 3.776002e-03 2.275627e-03 7.590585e-04 0.48
7 1.765904e+01 1.858229e-03 1.119871e-03 3.735444e-04 0.53
8 1.493406e+01 9.914144e-04 5.974810e-04 1.992958e-04 0.57
9 1.321215e+01 4.284890e-04 2.582311e-04 8.613560e-05 0.61
10 1.247209e+01 1.862607e-04 1.122510e-04 3.744245e-05 0.65
11 1.203916e+01 4.726695e-05 2.848568e-05 9.501685e-06 0.68
12 1.190390e+01 4.898257e-06 2.951962e-06 9.846561e-07 0.73
13 1.188906e+01 1.900382e-07 1.145391e-07 3.820181e-08 0.77
14 1.188847e+01 9.042867e-09 5.942560e-09 1.817813e-09 0.81
Optimal solution found.
poc =
1.7472e-08
Iter Fval Primal Infeas Dual Infeas Duality Gap Time
0 8.520000e+02 3.740554e-01 2.500000e-01 8.338857e-02 0.23
1 2.549188e+02 4.172979e-02 2.789011e-02 9.302867e-03 0.26
2 9.392526e+01 1.561744e-02 1.043792e-02 3.481612e-03 0.30
3 2.667086e+01 4.410425e-03 2.947708e-03 9.832206e-04 0.34
4 2.407913e+01 2.549053e-03 1.703660e-03 5.682632e-04 0.39
5 2.355160e+01 2.499060e-03 1.670247e-03 5.571180e-04 0.43
6 2.324912e+01 2.449195e-03 1.636919e-03 5.460015e-04 0.47
7 2.249923e+01 2.308274e-03 1.542735e-03 5.145858e-04 0.50
8 2.040632e+01 1.851989e-03 1.237777e-03 4.128657e-04 0.54
9 1.970151e+01 1.692941e-03 1.131477e-03 3.774090e-04 0.59
10 1.744126e+01 1.126199e-03 7.526951e-04 2.510647e-04 0.62
11 1.620420e+01 8.023922e-04 5.362790e-04 1.788781e-04 0.66
12 1.474391e+01 3.945190e-04 2.636768e-04 8.795053e-05 0.70
13 1.427557e+01 2.644519e-04 1.767465e-04 5.895455e-05 0.73
14 1.370019e+01 1.011430e-04 6.759892e-05 2.254791e-05 0.77
15 1.348178e+01 3.984925e-05 2.663325e-05 8.883635e-06 0.81
16 1.336507e+01 7.537767e-06 5.037870e-06 1.680402e-06 0.85
17 1.334002e+01 6.477134e-07 4.329037e-07 1.443954e-07 0.88
18 1.333772e+01 1.179424e-08 7.999240e-09 2.629302e-09 0.93
19 1.333768e+01 1.381986e-10 8.290199e-09 3.080877e-11 0.99
Optimal solution found.
poc =
5.3035e-10
Iter Fval Primal Infeas Dual Infeas Duality Gap Time
0 8.520000e+02 3.472650e-01 2.500000e-01 8.338749e-02 0.23
1 2.309201e+02 2.520373e-02 1.814445e-02 6.052081e-03 0.27
2 9.405942e+01 1.047608e-02 7.541850e-03 2.515584e-03 0.31
3 2.721726e+01 3.116075e-03 2.243298e-03 7.482520e-04 0.35
4 2.416710e+01 1.805118e-03 1.299525e-03 4.334564e-04 0.40
5 2.375956e+01 1.781739e-03 1.282694e-03 4.278426e-04 0.43
6 2.358202e+01 1.764206e-03 1.270072e-03 4.236324e-04 0.47
7 2.304355e+01 1.703461e-03 1.226341e-03 4.090461e-04 0.52
8 2.181656e+01 1.527846e-03 1.099914e-03 3.668761e-04 0.56
9 2.124914e+01 1.448597e-03 1.042861e-03 3.478463e-04 0.60
10 1.948615e+01 1.170737e-03 8.428266e-04 2.811248e-04 0.64
11 1.783054e+01 8.717316e-04 6.275695e-04 2.093258e-04 0.68
12 1.708765e+01 7.336925e-04 5.281936e-04 1.761789e-04 0.72
13 1.550449e+01 3.894897e-04 2.803981e-04 9.352676e-05 0.76
14 1.514484e+01 3.155406e-04 2.271613e-04 7.576962e-05 0.80
15 1.441642e+01 1.496860e-04 1.077607e-04 3.594357e-05 0.85
16 1.411738e+01 7.908921e-05 5.693722e-05 1.899141e-05 0.90
17 1.383348e+01 1.063875e-05 7.658959e-06 2.554645e-06 0.95
18 1.380209e+01 2.769706e-06 1.993947e-06 6.650794e-07 0.99
19 1.379178e+01 1.701668e-07 1.225354e-07 4.086155e-08 1.03
20 1.379114e+01 8.779631e-09 6.671732e-09 2.108221e-09 1.07
Optimal solution found.
poc =
1.8216e-10
Iter Fval Primal Infeas Dual Infeas Duality Gap Time
0 8.520000e+02 3.384156e-01 2.500000e-01 8.338731e-02 0.24
1 2.283493e+02 2.314639e-02 1.709909e-02 5.703387e-03 0.29
2 9.475565e+01 9.793019e-03 7.234463e-03 2.413050e-03 0.33
3 2.755188e+01 2.925713e-03 2.161331e-03 7.209104e-04 0.37
4 2.438422e+01 1.697792e-03 1.254221e-03 4.183445e-04 0.41
5 2.398865e+01 1.677057e-03 1.238903e-03 4.132353e-04 0.45
6 2.383017e+01 1.662889e-03 1.228437e-03 4.097444e-04 0.49
7 2.333142e+01 1.612151e-03 1.190955e-03 3.972422e-04 0.53
8 2.217540e+01 1.460099e-03 1.078629e-03 3.597758e-04 0.56
9 2.173362e+01 1.404707e-03 1.037709e-03 3.461270e-04 0.61
10 2.035602e+01 1.210646e-03 8.943487e-04 2.983093e-04 0.65
11 1.865148e+01 9.312689e-04 6.879625e-04 2.294694e-04 0.68
12 1.727485e+01 7.016610e-04 5.183427e-04 1.728928e-04 0.72
13 1.622825e+01 5.067634e-04 3.743647e-04 1.248691e-04 0.76
14 1.539423e+01 3.289696e-04 2.430219e-04 8.105977e-05 0.80
15 1.498430e+01 2.416941e-04 1.785483e-04 5.955466e-05 0.83
16 1.446995e+01 1.222391e-04 9.030248e-05 3.012032e-05 0.87
17 1.418069e+01 5.599477e-05 4.136539e-05 1.379739e-05 0.91
18 1.397083e+01 5.693174e-06 4.205758e-06 1.402827e-06 0.95
19 1.394958e+01 5.174181e-07 3.822538e-07 1.274944e-07 0.99
20 1.394766e+01 4.663196e-08 3.460641e-08 1.149035e-08 1.03
21 1.394748e+01 2.032849e-09 2.212183e-09 5.009042e-10 1.07
Optimal solution found.
poc =
1.0958e-10
Iter Fval Primal Infeas Dual Infeas Duality Gap Time
0 8.520000e+02 3.341137e-01 2.500000e-01 8.338698e-02 0.24
1 9.059917e+02 1.639868e-01 1.227028e-01 4.092728e-02 0.28
2 1.017406e+03 7.906474e-02 5.916006e-02 1.973271e-02 0.31
3 1.124331e+03 4.121461e-03 3.083876e-03 1.028620e-03 0.35
4 1.118894e+03 6.591392e-06 4.931998e-06 1.645058e-06 0.39
5 1.118894e+03 9.882157e-09 7.394312e-09 2.466356e-09 0.43
6 1.118894e+03 1.481584e-11 1.109217e-11 3.698267e-12 0.46
Problem is infeasible.
This was tested on R2025a and R2023a.
  26 Comments
Torsten
Torsten on 30 Oct 2025 at 18:27
Edited: Torsten on 30 Oct 2025 at 18:33
but I never get the NaNs inside my original code. only when I run them as a script from the .mat file.
That's what I did. I ran it here on MATLAB answers using R2025a (green arrow RUN) as a script with the data from the .mat-file. And the result is always the same (independent of the numer of times I start it).
load data.mat
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.40 1 0.000000e+00 nan 3.316314e-01 nan 0.55 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.32 1 0.000000e+00 nan 3.316314e-01 nan 0.43 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.32 1 0.000000e+00 nan 3.316314e-01 nan 0.43 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.32 1 0.000000e+00 nan 3.316314e-01 nan 0.43 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.33 1 0.000000e+00 nan 3.316314e-01 nan 0.44 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.32 1 0.000000e+00 nan 3.316314e-01 nan 0.42 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.32 1 0.000000e+00 nan 3.316314e-01 nan 0.42 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.36 1 0.000000e+00 nan 3.316314e-01 nan 0.47 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.33 1 0.000000e+00 nan 3.316314e-01 nan 0.43 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
[x,~,exitflag,~] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316314e-01 nan 0.32 1 0.000000e+00 nan 3.316314e-01 nan 0.45 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
exitflag
exitflag = -7
chicken vector
chicken vector on 31 Oct 2025 at 9:36
I think the problem occurs during the compiling step and, probably, that is the reason why the bug does not show up in Matlab Answers.
To demonstrate this I first run the inputs in data.mat on Matlab Answers and, as expected, NaNs are returned.
Then I load the solution that my code finds and check its feasibility which is always below the ConstraintTolerance.
This is to show that an optimal solution exists and those NaN should not appear.
I didn't compute the OptimalityTolerance so I cannot demonstrate that is the optimal solution (this is an act of faith), but it is a feasible solution nonetheless.
It follows that coneprog should not return NaNs.
% Load coneprog input:
load data.mat;
% Run optimisation (expected NaN on Matlab Answers):
[x,~,exitflag] = coneprog(f, socConstraint, A, b, Aeq, beq, [], [], opts);
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 nan 3.316165e-01 nan 0.42 1 0.000000e+00 nan 3.316165e-01 nan 0.58 Search direction is small and current iterate is not within the specified constraint and/or optimality tolerance.
% NaN solution outputs x = zeros and exitflag = -7:
[all(x==0) exitflag]
ans = 1×2
1 -7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Load solution computed inside code when it converges:
load solution.mat;
% Check linear inequality constraint satisfaction:
linineq = all(A*x - b <= opts.ConstraintTolerance);
% Check linear equality constraint satisfaction:
lineq = all(abs(Aeq*x - beq) <= opts.ConstraintTolerance);
% Check SOC constraint satisfaction:
N = numel(socConstraint);
socs = false(1,N);
for j = 1 : N
socs(j) = norm(socConstraint(j).A*x - socConstraint(j).b) - socConstraint(j).d'*x + socConstraint(j).gamma <= opts.ConstraintTolerance;
end
soc = all(socs);
% Overall constraint satisfication:
all([linineq lineq soc])
ans = logical
1

Sign in to comment.

Accepted Answer

Torsten
Torsten on 30 Oct 2025 at 20:51
Moved: Torsten on 31 Oct 2025 at 11:40
This works for dense matrices/vectors, but in sparse mode, it gives exitflag = -7 as above. Maybe it's worth asking MATLAB support how this can be avoided.
n = 100;
index = 1;
bsoc = zeros(4*n+2,1);
f = zeros(4*n+2,1);
for i = 1:n
Asoc = zeros(4*n+2);
dsoc = zeros(4*n+2,1);
Asoc(index,index) = 1.0;
Asoc(index+1,index+1) = 1.0;
Asoc(index+2,index+2) = 1.0;
gamma = -1.0;
socConstraints(2*i-1) = secondordercone(Asoc,bsoc,dsoc,gamma);
%socConstraints(2*i-1) = secondordercone(sparse(Asoc),sparse(bsoc),sparse(dsoc),gamma);
dsoc(index+3) = 1.0;
gamma = 0;
socConstraints(2*i) = secondordercone(Asoc,bsoc,dsoc,gamma);
%socConstraints(2*i) = secondordercone(sparse(Asoc),sparse(bsoc),sparse(dsoc),gamma);
f(index+3) = 1;
index = index + 4;
end
options = optimoptions("coneprog",Display='iter');
[x,~,exitflag,~] = coneprog(f, socConstraints,[],[],[],[],[],[],options)
Iter Fval Primal Infeas Dual Infeas Duality Gap Time 0 0.000000e+00 5.000000e-01 3.333333e-01 4.975124e-03 9.04 1 0.000000e+00 7.496256e-04 4.997504e-04 7.458961e-06 14.05 2 -4.125786e-17 1.123877e-06 7.492513e-07 1.118286e-08 18.97 3 -6.119795e-20 1.684974e-09 1.123316e-09 1.676591e-11 24.10 Optimal solution found.
x = 402×1
1.0e-20 * 0 0 0 -0.0770 0 0 0 -0.0159 0 0 0 -0.0770 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
exitflag = 1
  3 Comments
Torsten
Torsten on 31 Oct 2025 at 11:44
Edited: Torsten on 31 Oct 2025 at 19:06
If you get an answer from support, it might be helpful for other users if you post it here. But I think it is already a known problem that switching to full matrices can help solving convergence issues. There are some allusions on the documentation side for coneprog:
"Switching to full data instead of sparse can help the solver to complete successfully."
Maybe directly contacting the coneprog developers is an option:
chicken vector
chicken vector on 31 Oct 2025 at 13:14
They are investigating the issue.
When I will get an answer I will post it here.
Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Quadratic Programming and Cone Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!