ga-fitness

Dear All,
I need to optimize parameters of a function using the ga module. I have a coefficient matrix say A(9:4):
A =
-0.0996 -0.0323 -0.0640 -0.0365
0.0859 -0.0636 0.0552 0.0315
0.1805 0.0125 0.1159 0.0662
0.0521 0.0513 0.0335 0.0191
-0.3028 0.0647 -0.1946 -0.1111
-0.2242 -0.0382 -0.1440 -0.0822
0.0475 -0.0191 0.0305 0.0174
0.2170 -0.0010 0.1394 0.0796
0.0437 0.0257 0.0281 0.0160
and a vector B where i store the experimental data say B(9:1):
B =
-4.8700
-3.2100
5.1000
5.3400
-1.6200
-8.1700
-0.4700
4.8300
3.0800
my chromosome will be a vector x(4:1)
I would like to write a fitness function like:
fitnessfcn = abs((A*x)-B) [x,fval]=ga(fitnessfcn,4)
I have used the below function
FitnessFcn = @(x) abs( ((A(1,1)*x(:,1))+(A(1,2)*x(:,2))+(A(1,3)*x(:,3))+(A(1,4)*x(:,4)))-B(1,1))
which only uses (the first row of A) * (column x)-(first element of B)
How can I write in general form to use all the data. I have tried fitnessfcn = abs((A*x)-B) but it says Error using ==> mtimes Inner matrix dimensions must agree.
Do I have to use for loops and how to write a function with for loops?
I would really welcome any sugestion.
Kind regards, Christos

 Risposta accettata

Andrei Bobrov
Andrei Bobrov il 11 Mag 2011

0 voti

variant
FitnessFcn = @(x)bsxfun(@minus,abs(bsxfun(@times,A,x)),B)

1 Commento

chris
chris il 22 Giu 2012
Many thanks for your answer. It is very helpful. I am using this in the example above with:
[x]=ga(FitnessFcn,4)
and i am getting an error message:
Subscripted assignment dimension mismatch.
Is it easy to find the problem?
Once again thanks
Kind regards, christos

Accedi per commentare.

Più risposte (3)

Star Strider
Star Strider il 23 Giu 2012

1 voto

If the fitness function is supposed to produce a scalar to be minimized, then I suggest something like:
FitnessFcn = @(A,B,x) ( sum( (A*x-B).^2 ) );
Since A is (9x4), x is (4x1) and B is (9x1), A*x will produce a (9x1) vector. A*x-B produces the error vector, and the FitnessFcn here will produce the sum of the squared errors, a scalar >= 0.
chris
chris il 11 Mag 2011

0 voti

Many thanks for your answer. It is very helpful. I am using this in the example above with:
[x]=ga(FitnessFcn,4)
and i am getting an error message:
Subscripted assignment dimension mismatch.
Is it easy to find the problem?
Once again thanks
Kind regards, christos
chris
chris il 22 Giu 2012

0 voti

Sorry for your time. I am coming back to this old question since after postponing for one year I start again to try make it work. However I haven't solved the problem above. I would be more than happy for any help since my experience with matlab is rather limited.
All the best, chris

Categorie

Scopri di più su Parallel Computing in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by