# Binary/Integer variables in genetic algorithm

32 views (last 30 days)
Stefanos Mavropoulos on 13 Jul 2015
Edited: Ghada Saleh on 15 Jul 2015
I want to define some variables as integer and constraint them to be binary. Even though I set the IntCon matrix the variables that I want to be integer, the ga doesn't recognize them as integers.
function [x,fval,exitflag,output,population,score] = model()
nvars=64;
IntCon=[33:64];
TolFun_Data=10^-2;
TolCon_Data=10^-2;
options = gaoptimset;
options = gaoptimset(options,'TolFun', TolFun_Data);
options = gaoptimset(options,'TolCon', TolCon_Data);
[x,fval,exitflag,output,population,score] = ga(@objective,nvars,[],[],[],[],[],[],@constraint,IntCon,options);
The solution for the integer variables is always a non-integer value. (such as 0.0221, -0.0153).
Also, should I constraint the integer variables to binary [0,1] using linear constraint lb,ub?

Ghada Saleh on 15 Jul 2015
Edited: Ghada Saleh on 15 Jul 2015
Hi Stefanos,
I understand that the output of the generic optimization is not integer for the variables specified in 'IntCon' as integers. I would check the following:
1. First make sure that the constraints in the 'constraint' function are all inequalities because equality constraints are not allowed while using the integer 'ga' solver.
2. You need to check the 'exitflag' and make sure that the solver is able to find a feasible solution. One possibility as to why you are getting non-integer values is that the solver is not able to find a feasible solution. You can check the list of exit flags in the following link: http://www.mathworks.com/help/gads/ga.html#zmw57dd0e32305
3. Regarding imposing binary constraints, you can simply add them as lower and upper bounds. 'ga' solves integer problems best when you provide lower and upper bounds for every 'x' component. You can also refer to the following link for more information on Integer optimization. http://www.mathworks.com/help/gads/mixed-integer-optimization.html
I hope the above information helps.

### Community Treasure Hunt

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

Start Hunting!