How to pass input parameter in a function handle

6 visualizaciones (últimos 30 días)
etudiant_is
etudiant_is el 13 de Jun. de 2016
Comentada: etudiant_is el 14 de Jun. de 2016
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?

Respuesta aceptada

Guillaume
Guillaume el 13 de Jun. de 2016
Editada: Guillaume el 13 de Jun. de 2016
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 comentarios
Guillaume
Guillaume el 13 de Jun. de 2016
Editada: Guillaume el 13 de Jun. de 2016
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, ~)
etudiant_is
etudiant_is el 14 de Jun. de 2016
Thanks, now it works fine :).

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 13 de Jun. de 2016
I think what you are trying to do is described in this documentation page.
  1 comentario
etudiant_is
etudiant_is el 13 de Jun. de 2016
Editada: etudiant_is el 13 de Jun. de 2016
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Simulated Annealing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by