Genetic algorithm only works with integer constraints..
Mostrar comentarios más antiguos
Hello.
I am solving a problem using GA, and while my problem formulation does not require to use integer constraints, I found out that using integer constraints is the only way to solve it - and I do not understand why.. I will explain below.
I have 20 optimization variables.
- 9 of them represent switching times, formulated in a non-dimensional form, and bounded by [0 1],
- one is the final time, bounded by [tf1 tf2]
- other 10 variables represent angles, bounded by [0, 2pi]
If I do not introduce any integer constraints, and only use bounds for the optimization variables, then the GA window looks as below. Nothing changes on the plot, and there is no solution if I stop GA.
However, if I introduce integer constraints for the angles, then GA works very well for my problem:

To do this, I bound angles from 0 to 360 (achieving a 1 degree step), or from 0 to 720 (0.5deg step), and so on..
I learnt that no matter how I formulate this problem (different number of variables, times instead of switching times, etc.), this is always the pattern - a portion of the optimization variables have to be integer constrained. If not for angles, then using integer constraints for the times is also working (such that the time variable can take any value with a step of 1 second).
I would like to understand why this is the case. Ideally, I would like not to use integer constraints. Could it be that I need a better computer?
Thank you.
17 comentarios
Alan Weiss
el 3 de Oct. de 2022
Yakov, I do not understand why this is happening. Perhaps you should contact technical support so they can investigate your case in detail.
But, if you'd rather talk about it in the community, then please give us some more information.
- Does your problem have linear constraints or nonlinear constraints?
- What MATLAB version do you use?
- Does anything change if you give an initial population?
- Please show your options and entire ga call.
- Is it possible for your fitness function to return complex values?
Alan Weiss
MATLAB mathematical toolbox documentation
Maybe you use sin and cos for the angles when sind and cosd are required ?
Just a wild guess ...
Yakov Bobrov
el 4 de Oct. de 2022
Editada: Yakov Bobrov
el 4 de Oct. de 2022
Torsten
el 4 de Oct. de 2022
We neither know fitness4 nor nonlcon4. So no useful comment can be made.
Yakov Bobrov
el 4 de Oct. de 2022
Editada: Yakov Bobrov
el 4 de Oct. de 2022
Yakov Bobrov
el 4 de Oct. de 2022
Yakov Bobrov
el 4 de Oct. de 2022
Editada: Yakov Bobrov
el 4 de Oct. de 2022
No, sorry.
You might want to wait a while if someone else in the forum is able to guess some reason. But I doubt it without the necessary background information.
And if it's a bug in ga (what you allude to, I guess), we won't be able to find it.
Why don't you contact support ? They will treat your data confidentially as far as I can tell.
Yakov Bobrov
el 4 de Oct. de 2022
Torsten
el 4 de Oct. de 2022
Do you use rounding to integers in fitness4 for the variables 10:19 such that they are quasi integers anyway ?
Yakov Bobrov
el 4 de Oct. de 2022
Torsten
el 4 de Oct. de 2022
And how does ga work if you start from the integer solution, but without the intcon setting ?
Yakov Bobrov
el 4 de Oct. de 2022
Yakov Bobrov
el 5 de Oct. de 2022
Shouldn't the "options" settings follow directly the nonlinear constraints function "nonlcon" if no integer constraints are involved ?
[xSol,fval,exitflag,OUTPUT_GA,POPULATION_FINAL,SCORES] = ga(@(x) fitness4(x), 20,[],[],[],[],lb,ub,@(x) nonlcon4(x),GA_opts);
instead of
[xSol,fval,exitflag,OUTPUT_GA,POPULATION_FINAL,SCORES] = ga(@(x) fitness4(x), 20,[],[],[],[],lb,ub,@(x) nonlcon4(x),[],GA_opts);
in your first two test cases ?
Yakov Bobrov
el 5 de Oct. de 2022
Respuestas (1)
Yakov, your report indicates that the nonlinearly-constrained problem is being solved in the usual way: having very few iterations, because most of the time is taken up with solving subproblem iterations.

When you include integer constraints, the solver uses a different algorithm that shows many more iterations. For a simple demonstration of this effect, see the following simple example:
fun = @(x)log(1 + 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2);
opts = optimoptions('ga',PlotFcn='gaplotbestf');
nvar = 2;
lb = [-2 -2];
ub = -lb;
sol1 = ga(fun,nvar,[],[],[],[],lb,ub,@nlcon,[],opts);
sol2 = ga(fun,nvar,[],[],[],[],lb,ub,@nlcon,1,opts);
function [c,ceq] = nlcon(x)
c = x(1)^2 + x(2)^2 - 1;
ceq = [];
end
Alan Weiss
MATLAB mathematical toolbox documentation
2 comentarios
Yakov Bobrov
el 7 de Oct. de 2022
Alan Weiss
el 13 de Oct. de 2022
This could be due to the nonlinear constraint handling routine. When your population is entirely infeasible with respect to nonlinear constraints, the returned fitness value can be negative. Once you have a feasible individual, who will presumably have a positive fitness function. then for the penalty algorithm all the penalty values will be positive, meaning the fitness function will return just positive values. See Nonlinear Constraint Solver Algorithms.
Alan Weiss
MATLAB mathematical toolbox documentation
Categorías
Más información sobre Solver Outputs and Iterative Display en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








