fminsearch + objective function with variable input arguments

5 views (last 30 days)
Hello,
I am relatively new to MATLAB and this is actually my first question, so I thank everyone in advance who takes the time to answer.
I am developing an optimization algorithm which can solve a specific problem using a variable amount of optimization parameters. The idea is to run an objective function multiple times with an increasing number of parameters so that in the end I can evaluate the optimization results vs the complexity (number of parameters).
I would like to know if is there a way of generating a handle to the objective function with a variable amount of parameters. Right now I am using a very inelegant approach like:
switch numberParameters
case 1
hObjectiveFunction = @(paramOpt) objectiveFunction(paramOpt(1));
case 2
hObjectiveFunction = @(paramOpt) objectiveFunction(paramOpt(1),...
paramOpt(2));
case i
hObjectiveFunction = @(paramOpt) objectiveFunction(paramOpt(1),...
paramOpt(2),...
...
paramOpt(i));
end
fminsearchbnd(hObjectiveFunction, x0)
As you can see this is very bothersome as I would need to explicitly write the number of optimization parameters that I would like to have.
Does anyone have any idea how to do it in a more elegant way, without having to code all the options?

Accepted Answer

Matt J
Matt J on 22 Jul 2015
Edited: Matt J on 22 Jul 2015
This is what vectorization is meant for. You shouldn't be passing parameters as separate arguments to objectiveFunction. You should be passing a single vector argument and coding the processing inside objectiveFunction in a way that is independent of its length, something MATLAB gives lots of facilities for.
For example, instead of code that looks like this,
function d=sumtimes2(a,b,c)
d=2*(a+b+c);
end
you should have code that looks like the following where x can be a vector of any length
function d=sumtimes2(x)
d=2*sum(x);
end
  2 Comments
Nuno Alves de Sousa
Nuno Alves de Sousa on 22 Jul 2015
Edited: Nuno Alves de Sousa on 22 Jul 2015
Thanks a lot for your answer. I have just one last problem to solve.
Since my objective function takes varargin, I am not sure how to use vectorization when generating a handle to the function because I don't invoque the function explicitly in the code with an exact number of input arguments, so I don't see how I can use nargin in to adapt the processing inside the objective function. Should I do something like this:
% ask user for the number of parameters to use (nParam)
hObjectiveFunction = @(paramOpt) objectiveFunction(nParam,...
paramOpt);
Now paramOpt is a vector that corresponds to the values fminsearch is going to test and, since I pass nParam (basically nParam = nargin) to the objective function, I know the length of the vector and thus the number of optimization variables the user wants to use so that I can adapt the processing inside objectiveFunction?
Sorry if this sounds like a very dumb question, but I am indeed a a beginner.
Matt J
Matt J on 22 Jul 2015
Edited: Matt J on 22 Jul 2015
Since my objective function takes varargin, I am not sure how to use vectorization when generating a handle to the function because I don't invoque the function explicitly in the code with an exact number of input arguments
Well, my point was that you should probably rewrite the objective function so that it does not take varargin, or at least not for the purpose of receiving multiple unknows. You are using varargin as a way to split your unknown variables into multiple input arguments, which I contended you should not be doing.
However, an alternative to rewriting objectiveFunction() would be to write a separate wrapper mfile for it that looks like the following
function out=mywrapper(paramOpt)
argCell=num2cell(paramOpt);
out=objectiveFunction(argCell{:});
end
and now simply pass fminsearch a handle to mywrapper(),
fminsearchbnd(@mywrapper, x0)
This is less elegant and less efficient practice, but it does work, and independently of the length of paramOpt.
One other thing I hope you are aware of is that fminsearch is designed for a small number of unknowns only. If you plan to use nParam greater than about 6, you will get poor results.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!