Fitness 函数必须 return 一个标量
The Fitness function must return one scalar
我在 Matlab 上遇到一个错误,说我的适应度函数(遗传算法)应该 return 一个标量。目标是计算最佳系数 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
我在方法中有什么地方错了吗?
错误消息准确说明了问题所在:您的适应度函数应该 return 是一个标量。根据您的代码,我认为您想使用某些数量的 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
我在 Matlab 上遇到一个错误,说我的适应度函数(遗传算法)应该 return 一个标量。目标是计算最佳系数 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
我在方法中有什么地方错了吗?
错误消息准确说明了问题所在:您的适应度函数应该 return 是一个标量。根据您的代码,我认为您想使用某些数量的 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