Using Genetic Algorithm with bounds and non linear constraints

4 vues (au cours des 30 derniers jours)
P d
P d le 25 Nov 2015
Modifié(e) : Alan Weiss le 22 Déc 2015
I have a problem that I am trying to solve with a genetic algorithm. I am able to run the code with just the bounds, but when I add the non linear constraints, MATLAB gives the following errors:
"The following error occurred converting from function_handle to double: Error using double Conversion to double from function_handle is not possible.
Error in constrValidate (line 55) Iterate.cineq(:) = cineq;
Error in gacommon (line 133) [LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...
Error in ga (line 327) [x,fval,exitFlag,output,population,scores,FitnessFcn,nvars,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in GenericzTurbo_only_ga (line 120) [xopt,fval,eflag,output,solutions] = ga(problem);"
I have my code defined as follows
x0 = [5000 0.5 .2 70 .2 70 .1 .177 32.19 .002 .95 .002 .95 1.38]; % defaults of variables
lb = [1000 0 .1 50 .1 50 .001 .138 27.24 .002 .40 .002 .40 0.6]; % lower boundaries of variables
ub = [10000 1.0 .3 80 .3 80 .29 .209 39.81 .002 1.50 .002 1.50 2.5]; % upper boundaries of variables
ObjectiveFunction = @zTurbo;
nvars = length(x0); % Number of variables
ConstraintFunction = @simple_constraint; % the non linear constraints
tic
[xopt,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],lb,ub, ConstraintFunction);
toc
The constraint function itself is defined as follows
function [ c, ceq ] = simple_constraint( x )
c = @(x) [x(:,8) - (0.0001*x(:,9)^2 -0.0123*x(:,9) + 0.4690); ...
x(:,7) - (0.0003*x(:,9)^2 -0.0258*x(:,9) + 0.7458)];
ceq = @(x) [];
end
I have no idea what the errors actually mean, and why the genetic algorithm works fine without the non linear inequality constraints. Could someone please help me with this?

Réponses (1)

Alan Weiss
Alan Weiss le 22 Déc 2015
Modifié(e) : Alan Weiss le 22 Déc 2015
Your simple_constraint function should not return function handles for c and ceq, but instead should return double values:
function [ c, ceq ] = simple_constraint( x )
c = [x(:,8) - (0.0001*x(:,9)^2 -0.0123*x(:,9) + 0.4690); ...
x(:,7) - (0.0003*x(:,9)^2 -0.0258*x(:,9) + 0.7458)];
ceq = [];
end
I am also not sure that you have written it correctly in a vectorized form. Perhaps you should not have things like x(:,9). You certainly did not include dot-exponentiation, such as x(:,9).^2. I suggest that you do not, at first, use vectorized functions, and just assume that x is a row vector, so you would have things like x(9) instead of x(:,9).
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

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

Start Hunting!

Translated by