在Matlab中通过遗传算法传递额外参数

时间:2015-04-12 18:00:59

标签: algorithm matlab genetic-algorithm

我无法通过Matlabs GA功能传递数据。

我有一些数据存储在以下结构中:

param = 
               g: 9.8100
             rho: 1.2000
         mph_mps: 0.4470
        rpm2rads: 0.1047
gasoline_density: 0.7197
    liter2gallon: 0.2642
         MIN_SOC: 0.4000
         MAX_SOC: 0.8000
           grade: 0

ds1 = struct2dataset(param);
options1 = gaoptimset('InitialPopulation',1,'PopulationSize',100,'Generations',50,'PlotFcns',@gaplotbestfun,ds1);

然后我收到此错误:

Error using gaoptimset (line 267)
Arguments must occur in name-value pairs.

我有更多的数据结构,我不想传递每个参数,就像他们在:

中所说的那样

http://www.mathworks.com/help/gads/gaoptimset.html(即)

options = gaoptimset('param1',value1,'param2',value2,...)

1 个答案:

答案 0 :(得分:1)

使用功能句柄!!!

http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html

% Set objective function
vfun=@(x)Dynamic_Programming_func(x,param);
% Set constraint function
nonlcon=@(x)constraint(x,param);

options1 = gaoptimset('InitialPopulation',ini,'PopulationSize',populations,'Generations',generations,'PlotFcns',@gaplotbestfun);

%% Solve problem 
[x,fval,exitflag,output] = ga(vfun,nvars,[],[],[],[],x_L,x_U,nonlcon,IntCon,options1)

所以,现在我的约束和目标函数正在传递它们需要的所有参数!