Help with fmincon, non-linear constraints and binary variables
5 views (last 30 days)
Show older comments
Hello, I'm working on a problem that requires me to find the solution of a system subject to several inequalities so I decided to use fmincon to solve it.
One of the kinds of constraint is
x = min{c1, c2}
x is a variable part of a set of variables and c1 and c2 are dependent on the same set.
To implement this constraint I added the additional constraints:
x <= c1
x <= c2
x >= c1 + M*y1
x >= c2 + M*y2
y1 + y2 = 1
Where M is an adequately big number and y1 and y2 are additional binary variables (I know i could have used one, it seems to be working better with 2).
To impose y1 and y2 to be binary i used as lower bound 0 and as upper bound 1 in addition to the non linear condition mod([y1 y2],1) = [0 0].
Trying to figure out why it isn't working I tested it with the following code:
A = [1;1];
B = [5;1.6];
M = 10e5;
A_en = [A, [0 0;0 0];
-A,[M 0;0 M]];
B_en = [B;-B];
Aeq = [0 1 1];
Beq = 1;
obj = @(x) [-1 0 0]*x';
nf = [0 1 1];
nonlcon = @con;
OPTIONS = optimoptions('fmincon','Algorithm','interior-point');
fmincon(obj,[0 0 0],A_en,B_en,Aeq,Beq,[0 0 0],[Inf 1 1],nonlcon,OPTIONS)
where con is
function [c, ceq] = con(x)
c = [];
ceq = mod([x(2) x(3)],1);
end
I tried removing the non linear constraints too but the result never satisfies all the constraints.
Why shouldn't it work?
0 Comments
Accepted Answer
Torsten
on 20 Feb 2023
Edited: Torsten
on 20 Feb 2023
It doesn't work because fmincon only handles continuous variables and functions.
Use "intlinprog" instead. Here you can define variables to be binary.
Another option is to run your code twice: Once with x = c1 and once with x = c2 and see which run gives the better value for the objective function. This is most probably the way to go because you seem to have nonlinear constraints in a function "con" that intlinprog cannot handle.
As a last option, use "ga".
More Answers (0)
See Also
Categories
Find more on Systems of Nonlinear Equations 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!