GA function changing an input array
1 view (last 30 days)
Show older comments
I am trying to optimize 12 coefficients that describe a surface equation. The coefficients are normally in a 3X4 matrix. I use C = reshape(C',1,[]) to reshape the coefficients into an array but when that 1x12 array passes into the GA function the values randomly change. I can tell the values change by having Matlab spit out the C matrix before and after the GA function is called.
(code calling the ga and the function holding the cost function)
C = reshape(C',1,[])
[C,J_Cmax] = ga(@(C) C_Opt_Normalized(C,ThetaH_norm,Dim,L,D,lam0,lb,ub),[],[],[],[],[],[],[],ga_options);
(function being optimized)
function [J_Cmax] = C_Opt_Normalized(C,ThetaH_norm,Dim,L,D,lam0,lb,ub)
C
Between the first C spit out and the second the values change. I need my cost function to use:
C = [.02 .01 0 0 5 2 1.5 0 6 4 4.5 3]
but the ga changes it to:
C = [-6.6700 -7.4019 -7.9294 1.3010 -9.8413 -7.0841 -3.7203 -2.3271 -1.9639 5.1746 6.8477 -5.1248]
0 Comments
Answers (1)
Walter Roberson
on 28 May 2018
The C that you create with C = reshape(C',1,[]) is not being passed into C_Opt_Normalized .
Your call
[C,J_Cmax] = ga(@(C) C_Opt_Normalized(C,ThetaH_norm,Dim,L,D,lam0,lb,ub),[],[],[],[],[],[],[],ga_options);
creates an anonymous function with dummy parameter name C; until the end of the expression that defines the anonymous function, C refers to a vector of values that ga randomly creates and passes in to the anonymous function. The expression
@(C) C_Opt_Normalized(C,ThetaH_norm,Dim,L,D,lam0,lb,ub)
has exactly the same effect as
@(VectorToBeEvaluated) C_Opt_Normalized(VectorToBeEvaluated,ThetaH_norm,Dim,L,D,lam0,lb,ub)
Furthermore, you have
[C,J_Cmax] = ga(@(C) C_Opt_Normalized(C,ThetaH_norm,Dim,L,D,lam0,lb,ub),[],[],[],[],[],[],[],ga_options);
which assigns the result of the ga() call to C, so after ga() finishes, you overwrite C with the best solution that ga() found. That is very unlikely to be the same as the C = reshape(C',1,[]) that you had before that line.
4 Comments
Walter Roberson
on 30 May 2018
ga has no way of knowing that a particular proposed value is the location of the minima: it has to try a number of locations and see what happens.
As far as ga is concerned, if there is a different vector of values that returns a value that is 1e-17 smaller just because of mathematical round-off error, then ga thinks that is a "better" solution. ga is not concerned with any kind of mathematical proof of minima: ga cares only about what numeric value is actually returned from the cost function.
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!