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.
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);
...
카테고리
도움말 센터 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!