matlab vectorize,[求助]MATLAB中GA工具箱VECTORIZE 是干什么

icon1.gif [求助]MATLAB中GA工具箱VECTORIZE 是干什么

[求助]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就只输出一个解呢?

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值