Huckleberry Febbo
Huckleberry Febbo

Reputation: 313

Passing Extra Parameters Through Genetic Algorithms in Matlab

I am having trouble passing my data through Matlabs GA function.

I have some data that I store in the following structure:

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);

Then I get this error:

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

I have many more structures of data and I do not want to pass each parameter like they say in:

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

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

Upvotes: 0

Views: 564

Answers (1)

Huckleberry Febbo
Huckleberry Febbo

Reputation: 313

Use function handles!!!

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)

So, now my constraints and objective function are being passed all of the parameters that they need!

Upvotes: 1

Related Questions