problem with writing uncommon constraint function in genetic algorithm???

Hi everyone, How to write a constraint function in genetic algorithm that is not directly related to fitness function?
for example, suppose that fitness=x1+x2 ; y1<y2 while y1 and y2 is dependent on x1,x2 and other some parameters.
Any suggestion will be great help and I appreciate that.
thank you guys.

Antworten (1)

Take a look at the documentation of nonlinear constraints. Your nonlinear constraint function would be something like this:
function [c,ceq] = confun(x)
% calculate y1 here
% calculate y2 here
c = y1 - y2;
ceq = [];
If confun depends on other parameters besides x, see Passing Extra Parameters.
Alan Weiss
MATLAB mathematical toolbox documentation

4 Kommentare

thank you Alan, I improved my code to this but I get this error:
??? Input argument "GPR" is undefined.
Error in ==> myconstr at 3
c(1) = Etouch-GPR ;
----------------------------------------------------------------
main code:
...
a = 4; b = 2.1; c = 4;
% Assign parameter values
[x,fval] = runnested(a,b,c);
...
----------------------------------------------------------------
function [x,fval] = runnested(a,b,c)
nvars=2;
[x,fval] = ga(@nestedfun,nvars,[],[],[],[],[],[],@myconstr);
% Nested function that computes the objective function
function y = nestedfun(x)
y = (x(1)*a)+(b*x(2)*c);
end
end
----------------------------------------------------------------
function [c, ceq] = myconstr(Etouch,GPR,Estep,Es,Em)
c(1) = Etouch-GPR ;
c(2) = Estep-Es ;
c(3) = Etouch-Em ;
ceq = [];
end
beside Etouch,GPR,Estep,Es,Em calculate in main code and I don't know why got this error?!! if GPR is undefined why Etouch is defined; because I do the same thing for both of them.
thank you Alan, you are the best.
Your nonlinear constraint function needs to be a function of one variable, which I assume is Etouch.
function [c, ceq] = myconstr(Etouch)
c(1) = Etouch-GPR ;
c(2) = Estep-Es ;
c(3) = Etouch-Em ;
ceq = [];
end
Alan Weiss
MATLAB mathematical toolbox documentation
Alan, I changed the code but I got this error:
??? Undefined function or variable 'GPR'.
Error in ==> myconstr at 3
c(1) = Etouch-GPR ;
thanks man.
I think that the error message is clear: GPR is undefined when the nonlinear constraint function runs. I suggest that you use the debugger to find out why.
Alan Weiss
MATLAB mathematical toolbox documentation

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 13 Dez. 2015

Kommentiert:

am 28 Dez. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by