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?

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

回答(2 个)

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 个评论

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);

请先登录,再进行评论。

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);
...

类别

提问:

2013-7-29

回答:

2013-11-29

Community Treasure Hunt

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

Start Hunting!

Translated by