MATLAB Answers

Passing additional argument into objconstr

1 view (last 30 days)
Ethan Goldstein
Ethan Goldstein on 8 Sep 2021
Commented: Ethan Goldstein on 8 Sep 2021
Hello,
I am using the surrogateopt() function, as shown below:
functionPointer = @cost;
objconstr = packfcn(functionPointer, []);
options = optimoptions('surrogateopt',...
'PlotFcn','surrogateoptplot',...
'MaxFunctionEvaluations',500,...
'CheckpointFile', "\checkpoint.mat");
ub = [100 100 100];
lb = [0 0 0];
intcon = [1 2 3];
[k, fval, exitflag, output, trials] = surrogateopt(objconstr, lb, ub, intcon, options);
The surrogateopt() function requires class objconstr. When creating the objconstr object a function pointer is passed in:
functionPointer = @cost;
objconstr = packfcn(functionPointer, []);
According to the documentation, the only argument passed into the cost function is the variable which is used to directly compute the cost. This variable is constrained by the ub an lb variables. If possible, I would like to pass in another inpur variable to the cost function (two inputs instead of just one). To illustrate my request, see the desired code below:
functionPointer = @cost(k, desiredObject);
objconstr = packfcn(functionPointer, []);
Based on my understanding of how MATLAB handles function pointers, this behaviour should be possible. However, I would have to modify the code inside surrogateopt() which calls this function pointer. Any solution to this issue without modification of surrogateopt() would be very helpful.
Thank You,
Ethan

Answers (1)

Steven Lord
Steven Lord on 8 Sep 2021
Adapting the example from the documentation page for packfcn:
ros = @(x, k) k*(x(2) - x(1)^2)^2 + (1 - x(1))^2; % Added a parameter k
objconstr = packfcn(@(x) ros(x, 100), @unitdisk); % Fix the value of k to k = 100
lb = [-2 -2];
ub = -lb;
[x,fval] = surrogateopt(objconstr,lb,ub); % Solve as per the example
function [c,ceq] = unitdisk(x)
c = x(1)^2 + x(2)^2 - 1;
ceq = [ ];
end
This is one of the techniques shown in the documentation for passing additional parameters into a function that will be passed into an optimization, root finding, ODE solver, or other type of "function function".
  1 Comment
Ethan Goldstein
Ethan Goldstein on 8 Sep 2021
The second argument is a constraint used in the optimization algorithm. I was hoping that the argument passed in would be unused by the algorithm.
packfcn(@(x) ros(x, 100), @unitdisk)
Furthermore, the object I am passing in is the self referential pointer:
function output = cost(app, k)
app.writeToStatusText("Cost Evaluated!");
output = k(1)*k(2)*k(3);
end
This cost function is a member of the same class. When run surrogateopt() I get the following warning:
Warning: Unable to save App Designer app object. Save not supported for
matlab.apps.AppBase objects.
Warning: While loading an object of class 'TemperatureAutoTune':
Unable to load App Designer app object. Load not supported for
matlab.apps.AppBase objects.
The surrogateopt()is attempting to load/save the object it is a member of. Is there a way to prevent this behaviour while still passing in the object pointer?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!