Matlab中遗传算法的优化 [英] Optimization with genetic algorithm in matlab
问题描述
我已经使用遗传算法编写了一个简单的优化代码.我不知道为什么在运行代码时会出错.这是我的代码:
I have written a simple optimization code using genetic algorithm.I don't know why I get error during running the code.Here is my code:
f = @(x1,x2) 1-x1.^2+(x1-x2).^2;
A = [1 1;-1 2;2 1];
b =[2 2 3]' ;
Aeq = [];
beq = [];
Lb = [0 0]';
Ub = [];
[Xopt,Fval] = ga(f,2,A,b,Aeq,beq,Lb,Ub)
我不知道为什么matlab会给我错误.我根据遗传算法文档"(Genetic algorithm Documentation)编写了该程序.一点仍然给我错误:
I don not know why matlab gives me error.I wrote this programm based on the "Genetic algorithm Documentation" bit still gives me error:
Error using @(x1,x2)1-x1.^2+(x1-x2).^2
Not enough input arguments.
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in galincon (line 18)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 351)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue
推荐答案
MATLAB中所有优化方法的目标函数仅接受1个参数.根据 ga文档:
Objective functions of all optimization methods in MATLAB only accept 1 argument. According to ga documents:
乐趣-目标功能
客观函数,指定为函数句柄或函数名称.写目标函数接受长度为 nvars
的行向量并返回标量值.
Objective
function, specified as a function handle or function name. Write the
objective function to accept a row vector of length nvars
and return a
scalar value.
当"UseVectorized"选项为true时,请写有趣的文字来接受pop-by-nvars矩阵,其中pop是当前人口规模.在这个在这种情况下,fun返回一个与pop长度相同的向量,其中包含适应度函数值.确保乐趣不会带来任何后果流行音乐的特定大小,因为ga可以传递a的单个成员人口,甚至在向量化的计算中.
When the 'UseVectorized' option is true, write fun to accept a pop-by-nvars matrix, where pop is the current population size. In this case, fun returns a vector the same length as pop containing the fitness function values. Ensure that fun does not assume any particular size for pop, since ga can pass a single member of a population even in a vectorized calculation.
更改目标函数,它应该起作用:
Change you objective function and it should work:
f = @(x) 1-x(1).^2+(x(1)-x(2)).^2;
这篇关于Matlab中遗传算法的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!