How to set specific variables in fmincon to be integers only?

Dear all,
I am interested in finding the maximum of a function with three variables and two inequality constraints using fmincon.
One of the variable is an integer and another variables is an angle, which can only vary by half a degree. I would really appreciate if anyone can help me to set specific variables to vary as integers and/ by 0.5.
Thanks

 Risposta accettata

Matt J
Matt J il 25 Feb 2020
Modificato: Matt J il 25 Feb 2020
fmincon doesn't allow integer constraints, but intlinprog and ga do.
Failing, that, you could just run a loop that tests all combinations of the discrete variables. Since you only have two discrete variables, there shouldn't be an unmanageable number of combinations.

Più risposte (2)

Very late answer but someone else might benefit from it. You might want to give surrogate modelling a go, see the solver surrogateopt. This allows to find a global minimum of a nonlinear objective function and you can specify which variables you would like to be integers.
I liked Giuseppe's suggestion, but unfortunately I could not get it to work for a very simple example.
Giuseppe Inghilterra
Giuseppe Inghilterra il 25 Feb 2020
Modificato: Giuseppe Inghilterra il 25 Feb 2020
Hi,
another solution could be adding non linear equality constraints: for example remainder of division between first variable and 1 is zero and remainder between second variable (angle) and 0.5 is zero by using mod function (e.g mod(variable1,1) = 0 and mod(variable2,0.5) = 0).
In this way variable1 will be an interger and variable2 will be a multiple of 0.5.
Another non linear equality constraint could be by using sine or cosine function, e.g sin(k*pi) = 0. This constraint is satisfied only if k is an integer number.
You should be able to add these non-linear equality constraints to fmincon function. (see: https://www.mathworks.com/help/optim/ug/fmincon.html#busog7r-nonlcon).

4 Commenti

No, these methods will not work. fmincon requires the constraints to be differentiable and mod() is not a differentiable operation.
Applying the nonlinear equality constraint sin(k*pi) = 0 also has some subtle technical problems, since the local set of feasible search directions is empty at every integer, k. Because of this, fmincon will think any integer it lands on is an optimal solution. Also, it can jump to some crazy results from very harmless looking initial guesses, as the following simple examples show.
nlcon=@nonlcon;
function [c,ceq,gc,gceq]=nonlcon(k)
c=[];
ceq=sin(pi*k);
gc=[];
gceq=pi*cos(pi*k);
end
>> fmincon(@(k)k.^2,0.51,[],[],[],[],[],[],nlcon )
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
ans =
3.0000
>> fmincon(@(k)k.^2,6.5,[],[],[],[],[],[],nlcon )
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
ans =
1.0461e+06
I see now limitations on fmincon:
"fmincon is a gradient-based method that is designed to work on problems where the objective and constraint functions are both continuous and have continuous first derivatives".
Thus my proposed solutions will not work or better the result will be bad because fmincon is a function that works well only for some problems, as demonstrated by Matt.
Thank you Matt, learned something new on fmincon.
Thank you very much for clear explanation. I will implement a different approach.

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by