필터 지우기
필터 지우기

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