How to write a very long sum in a fitness function for global optimisation in Matlab -
i struggling writing parametrised fitness function global optimisation toolbox in matlab.
approach:
[x fvall,exitflag,output]=ga(fitnessfcn,nvars,a,b,aeq,beq,lb,ub)
i have fitness function call with
fitnessfcn=@fitnesstest;
hence, function stated in separate file.
problem: issue optimisation simple super long sum like
cost=f1*x1+f2x2+...fnxn
n should parameterised (384 @ moment). in matlab files, objective function short , neat like
y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
i have tried several approaches "writing" objective function intelligently, then, cannot call function correctly:
if write fitness function manually (for fi=1)
function y = simple_fitness(x) y = x(1)+ x(2)+ x(3)+ x(4)+ x(5)+ x(6)+ x(7)+ x(8);
the global optimisation works
but if use automated approach:
n = 8; %# number of function handles parameters = 1:1:n; store = cell(2,3); i=1:n store{1,i} = sprintf('x(%i)',parameters(i)); store{2,i} = '+'; %# operator end %# combine such %# sin(t)+sin(t/2)+sin(t/4) funstr = [store{1:end-1}];%# ignore last operator endfunction=';'; %functionhandle = str2func(funstr) y=strcat(funstr,endfunction)
matlab not recognise function properly:
error:
subscripted assignment dimension mismatch.
error in fcnvectorizer (line 14) y(i,:) = feval(fun,(pop(i,:)));
thanks! cannot write objective function hand have several hundred variables.
you can use sum(x) directly using function handle, instead of writing indexes, do: fitnessfcn = @(x) sum(x)
.
Comments
Post a Comment