声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1467|回复: 3

[综合讨论] 新手请教 遗传算法的求解!!请高手帮忙!!

[复制链接]
发表于 2009-3-23 17:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
function[sol,eval]=gademo1eval(sol,options)
x=solve(1)
eval=x+10*sin(5*x)+7*cos(4*x);
clc
fplot('x+10*sin(5*x)+7*cos(4*x)',[0 9])
initPop=initializega(10,[1 9],'gademoleval');
plot(initPop(:,1),initPop(:,2),'b*')
[x endPop]=ga([0 9],'gademoleval',[],initPop,[le-5 1 1],'maxGenTerm',1,'normGeomSelect',[0.08],[arithXover'],[20],'nonUnifMutation',[2 1 3]);
plot(endPop(:,1),endPop(:,2),'bo')
[x endPop bpop trace]=ga([0 9],'gademoleval',[],initPop,[le-6 1 1],'maxGenTerm',25,'normGeomSelect',[0.08],[arithXover'],[2],'nonUnifMutation',[2 25 3]);
plot (endPop(:,1),endPop(:,2),'y*')
figure(2)
plot(trace(:,1),trace(:,3),'y-')
hold on
plot(trace(:,1),trace(:,2),'r-')
xlabel('Generation');ylabel('Fittness');
legend

问题:
??? Error using ==> eval
Undefined command/function 'gademoleval'.

Error in ==> initializega at 80
  eval(estr);

Error in ==> gademo1 at 6
initPop=initializega(10,[1 9],'gademoleval');
回复
分享到:

使用道具 举报

 楼主| 发表于 2009-3-23 19:02 | 显示全部楼层

帮帮忙啊!

Error in ==> initializega at 49
if any(evalFN<48) %Not a .m file





function [pop] = initializega(num, bounds, evalFN,evalOps,options)
if nargin<5
  options=[1e-6 1];
end
if nargin<4
  evalOps=[];
end
if any(evalFN<48) %Not a .m file
  if options(2)==1 %Float GA
    estr=['x=pop(i,1); pop(i,xZomeLength)=', evalFN ';'];  
  else %Binary GA
    estr=['x=b2f(pop(i,:),bounds,bits); pop(i,xZomeLength)=', evalFN ';'];
  end
else %A .m file
  if options(2)==1 %Float GA
    estr=['[ pop(i,:) pop(i,xZomeLength)]=' evalFN '(pop(i,:),[0 evalOps]);'];
  else %Binary GA
    estr=['x=b2f(pop(i,:),bounds,bits);[x v]=' evalFN ...
'(x,[0 evalOps]); pop(i,:)=[f2b(x,bounds,bits) v];'];  
    end
end

numVars     = size(bounds,1);   %Number of variables
rng         = (bounds(:,2)-bounds(:,1))'; %The variable ranges'
if options(2)==1 %Float GA
  xZomeLength = numVars+1;   %Length of string is numVar + fit
  pop         = zeros(num,xZomeLength);  %Allocate the new population
  pop(:,1:numVars)=(ones(num,1)*rng).*(rand(num,numVars))+...
    (ones(num,1)*bounds(:,1)');
else %Binary GA
  bits=calcbits(bounds,options(1));
  xZomeLength = sum(bits)+1;   %Length of string is numVar + fit
  pop = round(rand(num,sum(bits)+1));
end
for i=1:num
  eval(estr);
end
发表于 2009-3-23 22:47 | 显示全部楼层
ga没学过, 刚刚help下 (r2006a)
x = ga(fitnessfcn,nvars,A,b,Aeq,beq,LB,UB,nonlcon,options)
好像与楼主调用方式不同!?
发表于 2009-3-24 10:23 | 显示全部楼层
调用格式出错
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-13 22:58 , Processed in 0.079368 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表