Linear constraints not satisfied during optimization using fmincon

Hi, I am relatively new to optimization and I am trying to optimize a nonlinear cost function with linear constraints. Using fmincon the optimization completes after a few iterations but the ans I recieved for my variables doesnt satisfy the constraints.
Optimization function
function [x, fval, iter] = myrun
x0 = [800;350;200;250];
A = [-1 -1 -1 -1;
1 1 1 1;
-0.59 -15.44 -7.12 -0.62;
-79.77 0 0 -16.15;
-6.65 -59.1 -25.94 -4];
b = [1360;
2268;
70;
560;
114.3];
Aeq = [];
beq = [];
lb = [360,250,150,120];
ub = [1080,500,300,360];
options = optimoptions(@fmincon,'Display','iter');
[x,fval,exitflag, output] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@nlcon,options);
end
and my objective function is
function f = myfun(x)
f = (0.0114*x(1)+0.01032*x(2)+0.0228*x(3)+0.0081*x(4))+15*((log(0.0114*x(1)))+(log(0.01032*x(2)))+(log(0.0228*x(3)))+(log(0.0081*x(4))));
end

댓글 수: 3

When you post a PICTURE of the code you wrote, anyone who might be tempted to help you must then retype your code from the picture.
If you paste in the TEXT of your code, then they can copy it, and then paste it directly into MATLAB. It is easier for you to paste in your code too. In this case, it becomes far easier for someone to offer to help you.
So is there a good reason why you want to make it more difficult to get help on your homework problem?
Sorry, I didnt realize it. Thanks alot for pointing that out. I have edited the question.
And now I can actually look at your question.

댓글을 달려면 로그인하십시오.

 채택된 답변

John D'Errico
John D'Errico 2022년 4월 9일
편집: John D'Errico 2022년 4월 9일
First, learn to use function handles.
myfun = @(x) (0.0114*x(1)+0.01032*x(2)+0.0228*x(3)+0.0081*x(4))+15*((log(0.0114*x(1)))+(log(0.01032*x(2)))+(log(0.0228*x(3)))+(log(0.0081*x(4))));
Test to see if your function is well posed. Does it return a scalar result?
x0 = [800;350;200;250];
myfun(x0)
ans = 105.0814
Next, I'll change the objective slightly. Does it change value when I do?
myfun(x0 + randn(size(x0)))
ans = 105.1873
So things seem to be well posed as an optimization problem. ALWAYS do tests like this.
A = [-1 -1 -1 -1;
1 1 1 1;
-0.59 -15.44 -7.12 -0.62;
-79.77 0 0 -16.15;
-6.65 -59.1 -25.94 -4];
b = [1360;
2268;
70;
560;
114.3];
Aeq = [];
beq = [];
lb = [360,250,150,120];
ub = [1080,500,300,360];
Next, does your function satisfy the constraints? Clearly, your start point satisfies the bound constraints.
[A*x0,b]
ans = 5×2
1.0e+04 * -0.1600 0.1360 0.1600 0.2268 -0.7455 0.0070 -6.7854 0.0560 -3.2193 0.0114
The constraints for fmincon require that A*xo <= b. In fact, that is again true. So you have made a good start.
How about the nonlinear constraints? Surprisingly, you have shown NO nonloinear constraints? So why pass what you don't have into fmincon, unless you have them, and just forgot to tell us? Sigh. I'll assume there are none, and that you think you need to pass something in as a placeholder.
options = optimoptions(@fmincon,'Display','iter');
[x,fval,exitflag, output] = fmincon(myfun,x0,A,b,Aeq,beq,lb,ub,[],options);
First-order Norm of Iter F-count f(x) Feasibility optimality step 0 5 1.050814e+02 0.000e+00 9.776e-02 1 10 1.050636e+02 0.000e+00 7.748e-02 1.323e-01 2 15 1.049739e+02 0.000e+00 7.767e-02 6.696e-01 3 20 1.045223e+02 0.000e+00 7.860e-02 3.354e+00 4 25 1.021971e+02 0.000e+00 8.370e-02 1.689e+01 5 30 9.519096e+01 0.000e+00 1.023e-01 4.705e+01 6 35 9.515364e+01 0.000e+00 7.782e-02 2.353e-01 7 40 9.513641e+01 0.000e+00 7.787e-02 1.808e-01 8 45 9.504002e+01 0.000e+00 7.810e-02 9.470e-01 9 50 9.455885e+01 0.000e+00 7.930e-02 4.724e+00 10 55 9.203830e+01 0.000e+00 8.603e-02 2.388e+01 11 60 8.005379e+01 0.000e+00 1.327e-01 9.460e+01 12 65 7.998254e+01 0.000e+00 1.331e-01 4.729e-01 13 70 7.997676e+01 0.000e+00 6.919e-02 1.152e-01 14 75 7.992363e+01 0.000e+00 6.933e-02 6.686e-01 15 80 7.977050e+01 0.000e+00 6.886e-02 1.993e+00 16 89 7.976987e+01 0.000e+00 3.155e-02 3.789e-02 17 94 7.976360e+01 0.000e+00 3.156e-02 1.969e-01 18 99 7.973257e+01 0.000e+00 3.158e-02 9.831e-01 19 104 7.957684e+01 0.000e+00 3.172e-02 4.920e+00 20 109 7.878461e+01 0.000e+00 3.242e-02 2.470e+01 21 114 7.442412e+01 0.000e+00 3.694e-02 1.263e+02 22 119 6.455385e+01 0.000e+00 5.292e-02 2.261e+02 23 124 6.449393e+01 0.000e+00 5.305e-02 1.131e+00 24 129 6.449558e+01 0.000e+00 1.798e-03 2.825e-02 25 134 6.449169e+01 0.000e+00 2.009e-04 3.288e-02 26 139 6.449090e+01 0.000e+00 2.326e-05 5.173e-03 27 144 6.449090e+01 0.000e+00 2.000e-06 2.445e-06 28 149 6.449089e+01 0.000e+00 4.368e-08 5.164e-05 Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x
x = 4×1
360.0000 250.0000 150.0000 120.0000
fval
fval = 64.4909
exitflag
exitflag = 1
Fmincon seems happy with the result, but I notice the solution lives at exactly the lower bounds. That won't necessarily be a good thing.
[A*x,b]
ans = 5×2
1.0e+04 * -0.0880 0.1360 0.0880 0.2268 -0.5215 0.0070 -3.0655 0.0560 -2.1540 0.0114
The inequality constraints are satisfied. I fail to see the problem. Perhaps you really have nonlinear constraints in some form, and you forgot to tell us.

댓글 수: 2

Thanks alot for your detailed explanation. I dont have any nonlinear constraints for this problem, I did think I needed to pass nlcon as a placeholder, however, your explanation cleared my doubt and I removed the placeholder.
As an alternative, I went ahead and perform the optimization in an optimization software and got logical values for x that satisfies the constraints. It still bugs me about what would have gone wrong when trying to solve by using fmincon. I will go ahead and try to review it in more depth. Thanks once again.
I never like to see an optimization where all of the parameters end up at exactly the bounds. That is often an issue in my eyes, since often, those bounds may have been chosen fairly arbitrarily.
Anyway, my first question is if you get a different result from some other code, but the solution is clearly at the lower bounds, then have you coded the SAME objective function? I would look at that VERY closely. I really cannot know. But you might check to see if you used the same log function? For example, might you have used log to the base 10 in the other tool?
Looking at your objective, the nice thing about Answers is it gives us a pretty printed result.
syms x y z w
(0.0114*x+0.01032*y+0.0228*z+0.0081*w)+15*((log(0.0114*x))+(log(0.01032*y))+(log(0.0228*z))+(log(0.0081*w)))
ans = 
Do you see that ALL of the coefficients are positive? As such, we can make it as small as possible, merely by making x,y,z,w ALL as small as possible. Think about it. Is the log function one that is STRICTLY monotone increasing? How about each of those linear terms? Can we make than all as small as possible, merely by using the lower bounds, so by making x,y,z,w all as small as possible? Anyway, the solution would have been identically the same if you used some other log base, since all logs are monotone increasing.
So as long as the lower bounds for your problem satisfy the linear inequality constraints, then the answer is trivial.
A = [-1 -1 -1 -1;
1 1 1 1;
-0.59 -15.44 -7.12 -0.62;
-79.77 0 0 -16.15;
-6.65 -59.1 -25.94 -4];
b = [1360;
2268;
70;
560;
114.3];
lb = [360,250,150,120];
A*lb' <= b
ans = 5×1 logical array
1 1 1 1 1
Given that, the answer is trivial. Your lower bounds are indeed the solution to your minimization problem.
Worse, since your objective is of a separable form, where x,y,z,w are all independent, we could have solved this by 4 separate 1 variable optimizations.
What you did in any other code, we cannot possibly guess. And why you got something that you consider reasonable, again, I have no clue. Perhaps you have the signs on the inequality constraints wrong.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by