GA function changing an input array

1 回表示 (過去 30 日間)
Stuart Belcke
Stuart Belcke 2018 年 5 月 28 日
再開済み: Walter Roberson 2018 年 12 月 22 日
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]

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 5 月 28 日
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 件のコメント
Stuart Belcke
Stuart Belcke 2018 年 5 月 29 日
I got the initial guess into the ga. However, the initial guess I am feeding into the GA is the correct answer (i.e. the optimum C to minimize the cost function) and the program continues to change the C matrix. So I must have something wrong in my code otherwise the program would stop immediately.
I am attempting to complete parameter uncertainty analysis and the ub and lb are for the bounds on the surface plane and not for the bounds of the C matrix (describing the surface) that the ga is searching for.
Walter Roberson
Walter Roberson 2018 年 5 月 30 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by