-1

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?

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)

Thanks!

kit
  • 73
  • 1
  • 2
  • 5
  • 1
    It's hard to test out things when the code example isn't runnable. You have unknown parameters/variables (`P`, `W`, `price`) with no values given. Same goes for your call to `ga`. – horchler Jan 21 '14 at 22:53
  • Are you using parallel computing? Creating new functions while workers are running often causes problems. – Daniel Jan 21 '14 at 22:57
  • @horchler P & W are symbolic variables which are derived accordingly. The price is a 24x1 constant values. The key thing to note here is the matlabFunction creating a function which is not being able to run with ga. It would be great if you can advise on this. – kit Jan 21 '14 at 23:11
  • @DanielR No, I am not using parallel computing. – kit Jan 21 '14 at 23:12

1 Answers1

1

From the documentation for ga:

The fitness function should accept a row vector of length nvars and return a scalar value.

Your objFcn is accepts a column vector and throws an error if a row vector is passed in. You can fix this (I think) by changing this line to:

X = sym('x',[1 2*N]);

If P is non-scalar then you may need to transpose it. Of course, without runnable code there could be all sorts of other things going on too. There are probably other places and ways that you could fix the issue.

horchler
  • 18,384
  • 4
  • 37
  • 73