How can I convert symbolic equation to fitness function which can be evaluated by genetic algorithm

3 views (last 30 days)
Hi,
I have a symbolic equation and wish to convert to a function which can be evaluated by genetic algorithm (ga). I have tried using the matlabFunction and convert the symbolic equation into a matlab file. However, this generated file can only be evaluated by fmincon or patternsearch algorithms and not genetic algorithm. I get this error using ga.
Caused by: Failure in initial user-supplied fitness function evaluation. GA cannot continue.
It seems like the matlabFunction does not generate the format required by ga, can anyone please advise what's the solution/workaround to this problem?
Thanks!

Answers (3)

Alan Weiss
Alan Weiss on 22 Jan 2014
fmincon and patternsearch expect that the variable x is a column, though they are pretty forgiving if it is something else. ga expects that x is a row. So perhaps you should have
X = sym('x',[1 2*N]);
I could not check this because I do not know what P and price are.
Alan Weiss
MATLAB mathematical toolbox documentation

Walter Roberson
Walter Roberson on 21 Jan 2014
Please show symvars() of the symbolic expression, and show the call to matlabFunction that you used. Please also show the first bit of the generated function -- in particular, the part inside the @() header. Please show your call to ga()

Leong Kit
Leong Kit on 21 Jan 2014
The code are as follow:
N = 24;
X = sym('x',[2*N 1]);
Y = X(1:N);
W = 3.2516e-6.*Y.^3 - 0.0010074.*Y.^2 + 0.390950.*Y+2.2353;
Z = P.*W;
totR = sum(Z);
totR = subs(totR,[P],[price]);
matlabFunction(totR,'vars',{X},'file','objFcn');
% Call to ga
x1 = ga(@objFcn, N*2, A, b, Aeq, beq)

Community Treasure Hunt

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

Start Hunting!