Passing additional argument into objconstr
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Steven Lord
am 8 Sep. 2021
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 Kommentar
Siehe auch
Kategorien
Mehr zu Surrogate Optimization finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!