Passing additional argument into objconstr
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Steven Lord
2021-9-8
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".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!