Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
初级会员
注册日期: 2009-07-19
年龄: 59
帖子: 1
声望力: 0 ![]() |
![]()
[求助]MATLAB中GA工具箱VECTORIZE 是干什么的? 我是一个新手,对matlab不怎么熟练,正在学习ga,请教一些问题,谢谢~
MATLAB中GA工具箱第一个DEMO的最后一个方面的讲到的vectorize是干什么? 该程序源代码如下 %% Vectorizing your fitness function % Consider the previous fitness function again: % % f(x) = a * (x(1)^2 - x(2)) ^2 + (b - x(1))^2; % % By default, the GA solver only passes in one point at a time to the % fitness function. However, sometimes speed up can be achieved if the % fitness function is vectorized to take a set of points and return a set % of function values. % % For example if the solver wants to evaluate a set of five points in one % call to this fitness function, then it will call the function with a % matrix of size 5-by-2, i.e. , 5 rows and 2 columns (recall 2 is the number % of variables). % % Create an M-file called vectorized_fitness.m with the following code: % % function y = vectorized_fitness(x,a,b) % y = zeros(size(x,1),1); %Pre-allocate y % for i = 1:size(x,1) % x1 = x(i,1); % x2 = x(i,2); % y(i) = a * (x1^2 - x2) ^2 + (b - x1)^2; % end % % This vectorized version of the fitness function takes a matrix x with % an arbitrary number of points, the rows of x, and returns a column % vector y of length the same as the number of rows of x. % % We need to specify that the fitness function is vectorized using the % options structure created using GAOPTIMSET. The options structure is % passed in as the third argument. FitnessFunction = @(x) vectorized_fitness(x,100,1); numberOfVariables = 2; options = gaoptimset('Vectorized','on'); [x,fval] = ga(FitnessFunction,numberOfVariables,options) 是否是进行初始化?(其中x是一个N维向量,一开始的值是初始值)但是GA需要初始化吗? 另外我在《测试智能信息处理》中看到一个对Griewangk函数的极小化 程序1:Griewangk函数 function[eval]=gw(sol) numv=size(sol,2); x=sol*(1:numv); for i=1:numv multi=multi*cos(x(i)/sqrt(i)); end eval=sum(x.^2/4000)-multi+1; 程序2:Griewangk函数适应度代码 function[sol,eval]=gwmin(sol,options) numv=size(sol,2)-1; x=sol(1:numv); eval=gw(x); eval=-eval; 遗传算法求解: bounds=ones(6,1)*[-512 512]; [p,endPop,bestSols,trace]=ga(bounds,'gwmin'); plot(trace(:,1),trace(:,3),'b-') hold on plot(trace(:,1),trace(:,2),'b-') xlabel('Generation'); ylabel=('Fitness'); 输出解的变化和种群平均值变化并输出最优解 p=0.002 -0.004 -0.003 0.006 -0.0022 0.0002 我的疑问:在程序2:Griewangk函数适应度代码中的options是什么?我没有输入啊? sol是解的值吗?eval是适应度函数吗? 我在matlab中进行到[p,endPop,bestSols,trace]=ga(bounds,'gwmin'); 就没法进行了:提示 ??? Error using ==> gads\private\validate>validNumberofVariables Number of variables (NVARS) must be a positive number. Error in ==> gads\private\validate at 43 validNumberofVariables(o.GenomeLength); Error in ==> ga at 153 [GenomeLength,FitnessFcn,options] = validate(GenomeLength,FitnessFcn,options); 请问是什么意思啊? 另外最优解p有那么多,他们都代表解吗?因为他么都趋近于零所以最优解p=0对吗? 但是为什么vectorize就只输出一个解呢? 谢谢! |
![]() |
![]() |