# How to set a boundary condition to my solution of the fitness function? (MATLAB genetic algorithm)

2 views (last 30 days)
findpeaks123 on 1 Jun 2021
Commented: findpeaks123 on 17 Jun 2021
I have the following problem:
I am optimizing three design parameters. For a more efficient and automated optimization I use the genetic algorithm in MATLAB. For each iteration the three design paramers are varied to find the optimal solution. The problem is that i want to restrict the solution of the fitness function to be between certain values. For that i need to get the solution and if it's not in the range, i need to discard it from the current population and to redo the simulation with another parameters till my solutions falls in the required range. Therefore i can create an output function and evaluate it every iteration but there must be some internal functions of the genetic algorithm which are doing this automatically. I cound't find anything yet how to create a loop or statement to discard the value from the current population and redo the simulation. Does anyone have an idea or any suggestions? Also some links would help.
%Output function
function [state,options,optchanged] = ga_output_function(options,state,flag)
if strcmp(flag,'iter') %if ga is in iteration
p = state.Population;
s = state.Score;
%test and change things
else
%something else
end
end

Alan Weiss on 2 Jun 2021
I believe that your optimization will work much better using patternsearch as the solver instead of ga, or even using surrogateopt if you have a relatively recent MATLAB version.
You can include a nonlinear constraint function to test whether the result is in the range you want. You don't need to use an output function for that.
You can also try setting your objective function to return NaN as the objective function value for infeasible points.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
findpeaks123 on 17 Jun 2021
Thank you!