How to avoid a negative solution with fmincon, including an external equation?
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Drilon
el 28 de Abr. de 2021
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?
Thanks for your support.
0 comentarios
Respuesta aceptada
Bruno Luong
el 28 de Abr. de 2021
Editada: Bruno Luong
el 28 de Abr. de 2021
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 comentarios
Bruno Luong
el 28 de Abr. de 2021
Use A/b
x0 = [2; 2; 1]; % [x1, x2, f1]
A = [2 10 0]
b = 30;
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);
x
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
Más respuestas (2)
Matt J
el 28 de Abr. de 2021
Editada: Matt J
el 28 de Abr. de 2021
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)
9 comentarios
Matt J
el 28 de Abr. de 2021
I don't understand what the "MathWorks Service" refers to here. You said you have many optimization problems to solve and you are looping over them, each time calling fmincon to get a solution. Why can't you feed a different A,b,Aeq,beq to fmincon each time you call it?
Matt J
el 28 de Abr. de 2021
Editada: Matt J
el 28 de Abr. de 2021
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)
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 comentarios
Matt J
el 28 de Abr. de 2021
Editada: Matt J
el 28 de Abr. de 2021
Yes, but why would you be attempting to make beq(2) evolve throughout the optimization?
If you have a constraint of the general form Aeq*x=h(x) where h(x) is a non-constant, non-linear function of x, then this is a non-linear constraint, and should be handled using the nonlcon input.
Ver también
Categorías
Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!