MATLAB: How to use genetic algorithm to minimize an objective function having 7 variables

genetic algorithmGlobal Optimization Toolboxglobalsearch algorithm

%% Objective function
s30min=((P1(x)-P1)/P1)^2 + ((P2(x)-P2)/P2)^2 + ((P3(x)-P3)/P3)^2 + ....+((Pn(x)-Pn)/Pn)^2 %(Lets say there is n number of terms in the objective function)
In the objective function, P1, P2, P3,…..Pn are known observed values.
n=24
P1(x), P2(x), P3(x),…..Pn(x) are the functions of x, where x=[x1,x2,x3,x4,x5,x6,x7];
I want to find the values in x when the value of s30min is minimum.
%%Minimization using GlobalSearch algorithm
lb=[1.0 0.0 0.0 0.0 0.0 0.0 1.0];
ub=[100 500 500 500 500 500 1.0];
problem = createOptimProblem('fmincon','objective',@s30min,...
'x0',[1.0001 0.0001 0.0001 0.0001 0.0001 0.0001 1.0],'lb',lb,'ub',ub,...
'options',optimoptions(@fmincon,'Algorithm','interior-point','Display','off'));
gs = GlobalSearch('Display','off');
[x,fval] = run(gs,problem);
%% After minimization minimum function value=0.1781
%%Minimization using genetic algorithm
nvars=7;
lb=[1.0 0.0 0.0 0.0 0.0 0.0 1.0];
ub=[100 500 500 500 500 500 1.0];
ObjectiveFunction = @s30min;
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],lb,ub);
%% After minimization minimum function value=0.64
At first, I used GlobalSearch algorithm and got the minimum function value 0.1781. Later, I used genetic algorithm for the same problem. Actually, I am trying to get minimum function value closer to 0.1781 using genetic algorithm. Is there any way to get minimum function value closer to 0.1781 using genetic algorithm? I found GlobalSearch function faster than ga function. Is it possible to make genetic algorithm faster? I ran GlobalSearch algorithm almost 100 times and I found minimum function value 0.1781 nearly 10 times. ga is a stochastic algorithm and giving different function value at each time. There are other solvers as well. I am confused which solver I should use for my objective function. I really don't know whether my function is smooth or nonsmooth. Any advice would be highly appreciated.

Best Answer

Your question seems to be "Is the genetic algorithm a good solver for global optimization?" The answer is in the table for choosing a solver.
  • If your objective function is smooth, use GlobalSearch or MultiStart.
  • If not, use patternsearch with many initial points.
To start patternsearch at multiple points when you have finite bounds lb and ub on every component, try:
x0 = lb + rand(size(lb)).*(ub - lb);
So no, the genetic algorithm is not recommended as a global solver. As you have found out by now, it is slow and unreliable.
You may well want to start MultiStart from a regular or perturbed grid of points, but that can use a lot of memory and time in six or seven dimensions (you seem to have fixed x(7) = 1). See Isolated Global Minimum.
Alan Weiss
MATLAB mathematical toolbox documentation