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 次查看(过去 30 天)
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 个)

Evan
Evan 2013-7-29
编辑:Evan 2013-7-29
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 个评论
Shadan
Shadan 2013-7-29
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);

请先登录,再进行评论。


amanita
amanita 2013-11-29
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);
...

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by