GA function changing an input array

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]

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de Mayo de 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 comentarios

Stuart Belcke
Stuart Belcke el 28 de Mayo de 2018
Editada: Stuart Belcke el 28 de Mayo de 2018
I am trying to use the C as an initial guess for the ga to run C_Opt_Normalized with. So you are saying the ga populates my C matrix regardless of an initial guess?
The second value in my original post is the value of C without running the cost function. It is simply what is on the other side of what I passed into the ga().
Walter Roberson
Walter Roberson el 28 de Mayo de 2018
The only way to give an initial guess to ga() is to pass in an options structure created by optimoptions() passing in InitialPopulationMatrix
Question for you: why are you passing in what looks like lower bound (lb) and upper bound (ub) to C_Opt_Normalized, but not passing those to ga itself? If you have a restricted range it would seem to make sense to tell ga what the ranges are ?
Stuart Belcke
Stuart Belcke el 29 de Mayo de 2018
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 el 30 de Mayo de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Linear Programming and Mixed-Integer Linear Programming en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Mayo de 2018

Abierta de nuevo:

el 22 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by