# How to avoid a negative solution with fmincon, including an external equation?

조회 수: 9(최근 30일)
Drilon 2021년 4월 28일
편집: Drilon 2021년 4월 28일
Hello community,
I want to create an optimization script with fmincon, including an equation (f2), which is not solved by fmincon. This equation will be solved with a function (see bottom of the script in the function 'kostfunktion'), but it contains the parameters x(1) and x(2) from the optimization problem. The problem is, that f2 and therefore fval are always negative. How can I set these parameters to a minimum of 0 or at least let them be positive?
The involving case is this one:
• f1 = 10*x1 + 2*x2 - 30 f2 = - 2*x1 - 10*x2 + 30
Bounds: 1<=x1<=5 1<=x2<=5 0<=f1<=inf 0<=f2<=inf
The optimization goal is to get the minimum of: obj = f1 + f2
f1 will be solved by fmincon. f2 will be solved in the function 'kostfunktion'.
clc, clear, clear global;
global f2 %f2 appears in the workspace, just to be visible
x0 = [2; 2; 1]; % [x1, x2, f1]
A = [];
b = [];
Aeq = [-10 -2 1];
beq = [-30];
lb = [1; 1; 0];
ub = [5; 5; inf];
options = optimoptions('fmincon', 'Display', 'iter');
[x fval] = fmincon(@kostfunktion,x0,A,b,Aeq,beq,lb,ub,[],options);
function obj = kostfunktion(x)
global f2
f2 = -2*x(1)-10*x(2)+30;
obj = x(3) + f2; % obj = f1 (solved with fmincon) + f2 (solved with this function)
end
The optimal solution should be obj = 0 with x1 = 2.5 and x2 = 2.5, but in this script, the solution is obj = -24, because I can't set bounds for f2. How can I set the bounds for f2 or fval?

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

### 채택된 답변

Bruno Luong 2021년 4월 28일
편집: Bruno Luong 2021년 4월 28일
The condition
f2 = - 2*x1 - 10*x2 + 30 >= 0
is equivalen to
2*x1 + 10*x2 + 0*f1 <= 30.
So set
A = [2 10 0]
b = 30
If you don't want to use A/b arguments (since f2 can be changed to more generic non linear function), just use non linear constraint nonlcon argument and program c output that handles that constraint.
##### 댓글 수: 6표시숨기기 이전 댓글 수: 5
Drilon 2021년 4월 28일
Thanks Bruno, this solution works too.
I will try to understand your solution too and test it for another case.

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

### 추가 답변(2개)

Matt J 2021년 4월 28일
편집: Matt J 2021년 4월 28일
Since your problem is linear, let's just use linprog,
%Reformulate with x=[x1,x2,f1,f2]
A = [];
b = [];
Aeq = [-10 -2 1 0;
2 10 0 1 ];
beq = [-30;+30];
lb = [1; 1; 0; 0];
ub = [5; 5; inf; inf];
obj=[0 0 1 1];
x=linprog(obj,A,b,Aeq,beq,lb,ub)
Optimal solution found.
x = 4×1
2.5000 2.5000 0 0
##### 댓글 수: 9표시숨기기 이전 댓글 수: 8
Drilon 2021년 4월 28일
Missunderstanding. I have only one optimization problem with different constraints. The constraints will change for many cases and all these cases need to fit into one optimization. There will be million cases, therefore I do not want to do it without parameters, I need them to create call all the cases.

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

Matt J 2021년 4월 28일
편집: Matt J 2021년 4월 28일
You could consider something like this:
x0 = [2; 2; 1]; % [x1, x2, f1]
A = [];
b = [];
Aeq = [-10 -2 1];
beq = [-30];
lb = [1; 1; 0];
ub = [5; 5; inf];
options = optimoptions('fmincon', 'Display', 'iter','Algorithm', 'sqp','MaxFunctionEvaluations',inf,...
'OptimalityTolerance',1e-12,'FunctionTolerance',1e-12,'StepTolerance',1e-12);
[x fval] = fmincon(@kostfunktion,x0,A,b,Aeq,beq,lb,ub,[],options)
Iter Func-count Fval Feasibility Step Length Norm of First-order step optimality 0 4 7.000000e+00 7.000e+00 1.000e+00 0.000e+00 1.000e+01 1 13 1.816455e+00 5.824e+00 1.681e-01 5.219e-01 8.824e+00 2 26 1.018196e+00 5.589e+00 4.035e-02 1.066e-01 7.560e+01 3 30 8.273146e-01 3.553e-15 1.000e+00 9.951e-01 1.012e+01 4 34 7.326886e-01 3.553e-15 1.000e+00 1.005e-02 1.001e+01 5 38 2.595588e-01 0.000e+00 1.000e+00 5.026e-02 1.001e+01 6 48 1.874546e-02 0.000e+00 1.176e-01 2.957e-02 2.062e+01 7 57 1.274881e-02 0.000e+00 1.681e-01 3.347e-03 1.001e+01 8 61 2.463475e-03 3.553e-15 1.000e+00 1.093e-03 1.000e+01 9 70 1.949447e-03 0.000e+00 1.681e-01 9.182e-04 6.375e+00 10 74 3.999204e-04 3.553e-15 1.000e+00 3.575e-04 2.855e+00 11 78 3.656302e-07 0.000e+00 1.000e+00 2.899e-04 8.573e-02 12 82 5.079048e-11 0.000e+00 1.000e+00 8.976e-06 1.200e-03 13 86 5.079048e-11 0.000e+00 1.000e+00 1.939e-21 1.108e-03 Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
x = 3×1
2.5000 2.5000 0.0000
fval = 5.0790e-11
function obj = kostfunktion(x)
global f2
f2 =-2*x(1)-10*x(2)+30;
c=10;
if f2<0, f2=exp(-c*f2)+c*f2-1; end %apply a penalty
obj = x(3) + f2; % obj = f1 (solved with fmincon) + f2 (solved with this function)
end
##### 댓글 수: 8표시숨기기 이전 댓글 수: 7
Drilon 2021년 4월 28일
Bruno, it was not his code. I showed him an old code where the problem appeared, because he wanted to understand the problem about beq. Because of this problem, I was looking for another solution, for example like the suggestion of Matt or yours.
However, thanks guys. Both solutions are great, but I think that nonlcon fits the most for my problem.
Thanks for the help. I appreciate it :)

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

R2020b

### Community Treasure Hunt

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

Start Hunting!