How to pass input parameter in a function handle

I don't know what it is really called so maybe the title of my question can be misleading. Anyway, I am using simulated annealing and I initially set the options as follows
options = saoptimset('DataType', 'custom', 'AnnealingFcn', @PermuteElements, ...
'StallIterLimit',50, 'ReannealInterval',50, 'PlotInterval', 50, 'AcceptanceFcn', @acceptancesa);
then I called simulated annealing with these options and it works fine.
The problem is that I want to have some input parameters for the function PermuteElements(param1,param2,etc) which I will need inside the function. but when I add them in the options
options = saoptimset(..., @PermuteElements(param1,param2,etc), ...);
I get an error. I don't exactly understand the role of the @ in front of the function. So how should I input the parameters to the function? Also, how can I figure out when (and by which function) will the function I made exactly be called during simulated annealing execution?

 Accepted Answer

Yes, you simply need to wrap your function handle into an anonymous function where the parameters are set. In your case:
%%in your function where param1 is defined and saoptimset is called
param1 = ... %must be set before the anonymous function is created
PermEl = @(optimValues) PermuteElements(param1, optimValues);
%note that changes to param1 from then on will not affect PermEl. param1 value has in effect become a constant in PermEl
options = saoptimset('DataType', 'custom', 'AnnealingFcn', PermEl, ...
'StallIterLimit',50, 'ReannealInterval',50, 'PlotInterval', 50, 'AcceptanceFcn', @acceptancesa);
In effect, the anonymous function is equivalent to creating a function that does
function NewPoint = PermEl(optimValues)
NewPoint = PermuteElements(param1, optimValues) %with param1 defined by the main code
end
except that the above does not work because standard functions have no way of accessing the variables from another function, so param1 would be undefined. Anonymous functions and nested functions can do the capture.

7 Comments

So far, I putted PermuteElements function inside the outer function and now I can access the value inside param1. But it should not really be there, so I want to keep it in a separate file.
Now, what I already said is that at the outer function where param1 is defined and saoptimset is called, optimValues variable still does not exist so I can't pass it along with param1. Or can I still it call like you answered me despite that?
When I tried it, I got
Error using ProblemResolution>@(optimValues)PermuteElements(param1,optimValues)
Too many input arguments.
Error in sanewpoint (line 17)
newx(:) = options.AnnealingFcn(optimvalues,problem);
Yes, I'm aware optimValues is not defined in your outer function. Did you try the anonymous function I gave you? optimValues is just the name of the input argument of that anonymous function. it'll receive the value when it is called by your optimiser.
Maybe if I use another name for the input arg of the function, it'll be clearer
PermEl = @(ov) PermuteElements(param1, ov); %whatever name you use for ov doesn't matter. I thought optimValues made more sense
options = saoptimset('DataType', 'custom', 'AnnealingFcn', PermEl, ...
'StallIterLimit',50, 'ReannealInterval',50, 'PlotInterval', 50, 'AcceptanceFcn', @acceptancesa);
Yes, I tried it and got the error I wrote in previous comment. In simulated annealing, this is going to be taken as the custom annealing function annealingFcn but it is telling me, it has too many input arguments.
But still thanks for explaining again with ov instead of optimValues (it is clearer now).
I'm not clear on which function is causing the 'Too many input arguments' error. Was this the full error message?
Either, PermuteElements expects less than two inputs, or the optimisation function is calling the anonymous function with more than one argument.
Can you clarify:
  • the definition of PermuteElements
  • what sort of function the optimisation is expecting (how many inputs/outputs)
This is the error I get, the rest of it is just showing the order from the functions that called the last function with the error
I have followed this example in the documentation http://www.mathworks.com/help/gads/examples/custom-data-type-optimization-using-simulated-annealing.html?refresh=true I changed the function mulprocpermute with my own permuteElements
function schedule = mulprocpermute(optimValues,problemData)
so my function was
function NewPoint = PermuteElements(optimValues,~)
I replaced problemData with ~ because I am not using it in my function
What I want it next is for the function to become
function NewPoint = PermuteElements(param1,optimValues,~)
and this is when I got the error.
Well, there you go, the optimiser passes two arguments to the function handle so the anonymous function must accept two arguments.
You can either ignore that extra argument in the anonymous function:
PermEl = @(optimValues, ~) PermuteElements(param1, optimValues);
with PermuteElements defined as:
function NewPoint = PermuteElements(param1, optimValues)
or ignore it in PermuteElements:
PermEl = @(optimValues, problemData) PermuteElements(param1, optimValues, problemData)
with PermuteElements defined as:
function NewPoint = PermuteElements(param1, optimValues, ~)
Thanks, now it works fine :).

Sign in to comment.

More Answers (1)

I think what you are trying to do is described in this documentation page.

1 Comment

I tried to do it with anonymous function but it is still not right. In fact, PermuteElements takes as an input parameter optimValues which is produced during the simulated annealing process and I want to have another input with it from when I first call the simulated annealing. (the two parameters are not available at the same function/workspace) Basically the header of my function is like this
function NewPoint = PermuteElements(param1, optimValues)
I can not do
PermEl=@(x) PermuteElements(param1,optimValues)
then
options = saoptimset(..., PermEl, ...);
because optimValues does not exist at that step yet.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!