when using genetic algorithm, the number of variables(nvar) is dependant on the row vector(x) that my fitness function accepts. How can I deal with that?

2 vues (au cours des 30 derniers jours)
Hello all,
I need to write something like this: ga(h, sum(x(1:5)),[],[],[],[],LB,UB,[],[],ga_opts);
as you see, the number of variables (nvar) is dependant on the vector I want to optimize(x). Is there any way I can deal with this problem?
Thank you

Réponses (2)

Evan
Evan le 29 Juil 2013
Modifié(e) : Evan le 29 Juil 2013
Have you looked into the varargin function?
help varargin
You could then parse the arguments contained within varargin based upon whatever conditions of your row vector x that determine their order/number.
  1 commentaire
Shadan
Shadan le 29 Juil 2013
Thank you for your response. The thing is that varargin will not be recognized outside of the function definition. for example if I write: ga(h, sum(varargin{1}(1:5)),[],[],[],[],LB,UB,[],[1:5],ga_opts);

Connectez-vous pour commenter.


amanita
amanita le 29 Nov 2013
I dont know if this is relevant, but i usually set nvars as the maximum number of variables and keep in the fitness function only the ones needed. For example, i have a vector of coefficients W that i want to optimize, but its length is dependent on an integer variable I, ie; If i have I=2 i need a vector W with 2 elements, if I=4 i need W with 4 elements. If the maximum number for I is 10. Then:
h = @(X) NETWORK_mex(X);
nvars=11;
LB=[1 -1*ones(1,10)]
UB=[10 ones(1,10)]
[x, err] = ga(h, nvars,[],[],[],[],LB,UB,[],1,ga_opts);
And inside the fitness function:
function J=NETWORK(X)
I=X(1);
W=X(2:I+1);
...

Community Treasure Hunt

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

Start Hunting!

Translated by