声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2713|回复: 3

[综合讨论] m函数simulink以及遗传算法混用的问题

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

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

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

x
m函数simulink以及遗传算法混用的问题下面一例是matlab帮助文档中关于遗传算法的一个例子那个运算
%函数
function y = simple_fitness(x)
y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
%函数
%脚本
FitnessFunction = @simple_fitness;
numberOfVariables = 2;
[x,fval] = ga(FitnessFunction,numberOfVariables)
%脚本
我用函数在simulink中建立模型
截图00.jpg
存储为my_sim_2名称
编写函数如下
function y=my_sim_m(x)
global x1 ;
global x2;
assignin('base','x1',x(1));
assignin('base','x2',x(2));
sim('my_sim_2');
assignin('base','zz',zz);
y=zz;
再编写脚本调用该函数用遗传算法进行相关计算如下:
FitnessFunction = @my_sim_m;
numberOfVariables = 2;
[x,fval] = ga(FitnessFunction,numberOfVariables);
结果运算出错!
错如下:
??? Error using ==> gads\private\makeState
GA cannot continue because user supplied fitness function failed with the following error:
Error using ==> sim
--> Invalid matrix-format variable specified as workspace input in 'my_sim_2/From Workspace'. The matrix must have two dimensions and at least two columns. Complex signals of any data type and non-double real signals must be in structure format. The first column must contain time values and the remaining columns the data values.
--> Invalid matrix-format variable specified as workspace input in 'my_sim_2/From Workspace1'. The matrix must have two dimensions and at least two columns. Complex signals of any data type and non-double real signals must be in structure format. The first column must contain time values and the remaining columns the data values.

Error in ==> gads\private\gaunc at 44
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);

Error in ==> ga at 384
        [x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,GenomeLength, ...




请高手给与指点,不知道是错在哪个地方,我认真检查了,原本以为x1,x2两个变量在m文件与simulink间不能顺利传递值,这一疑问我通过一个更简单的例子已经得到解决,由于接触遗传算法不久,并且我是想通过遗传算法对我simulink中的的相关参数进行优化求解,但遗传算法又必须有适应度函数,所以我就想到这样做!请高手给与指点哈!期待!!!!!
回复
分享到:

使用道具 举报

发表于 2009-3-25 07:54 | 显示全部楼层
workspace的数据格式是什么
 楼主| 发表于 2009-3-25 08:20 | 显示全部楼层
谢谢yufeng的回答,workspace 的数据格式都是数值形式的变量!yufeng有什么建议吗?
发表于 2012-7-16 10:43 | 显示全部楼层
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-5-15 11:22 , Processed in 0.067562 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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