Fitness函数必须返回一个标量

时间:2015-11-19 11:33:16

标签: matlab genetic-algorithm

我在Matlab上有一个错误,说我的健身功能(遗传算法)应该返回一个标量。目标是计算最佳系数x(1)x(2)和x(3):

function score= Fitness_fct(x,L,C,COV,expected_score) 
   %L,C,COV and expected_score are vector of size 22 by 1
   %x is the population (coefficients) that the GA uses

   score=sqrt(power(((power(L,x(1)) + power(C,x(2))+power(COV,x(3)))- 
      expected_value),2));
end

 FitnessFunction =@(x) Fitness_fct(x,L ,C,COV, expected_score );
 [x,fval] = ga(FitnessFunction,4);
 % here x will be the optimized coefficients and fval will be the   
   value obtained by the optimized coefficients

在获得优化系数(x(1),x(2),x(3))之后,我将计算我希望它接近向量的最终得分" Expected_value" :

for i=1:22
final_score(i)= ((power(L,x(1)) + power(C,x(2))+power(COV,x(3));
end

我对方法中的某些内容有误吗?

1 个答案:

答案 0 :(得分:0)

错误信息正是说错了:你的健身功能应该返回一个标量。根据您的代码,我认为您想要使用某些数量的root-mean-square作为适应度。要实现这一点,只需sum()取平方根之前的平方数量:

function score= Fitness_fct(x,L,C,COV,expected_score) 
   %L,C,COV and expected_score are vector of size 22 by 1
   %x is the population (coefficients) that the GA uses

   score=sqrt(sum(power(((power(L,x(1)) + power(C,x(2))+power(COV,x(3)))- 
      expected_value),2))); % note the added sum()
end

 FitnessFunction =@(x) Fitness_fct(x,L ,C,COV, expected_score );
 [x,fval] = ga(FitnessFunction,4);
 % here x will be the optimized coefficients and fval will be the   
   value obtained by the optimized coefficients