nonlinear constraints in fmincon
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have a function "myfunc" to minimize subject to a set of nonlinear constraints written in a function "myconstr". The problem is that myfunc is expensive to evaluate and "myconstr" uses as one of its inputs, the output of "myfunc". How can I deal with this problem efficiently if I want to use fmincon as the optimizer?
Here are some avenues that I am looking into but I am not satisfied with them:
- It is possible to write myconstr so that it re-evaluates myfunc but this would slowdown things even more.
- do the opposite: evaluate myconstr within myfunc and use a global variable, which I am reluctant in doing.
any ideas?
Thanks,
Pat.
0 个评论
采纳的回答
更多回答(1 个)
Ryan Livingston
2013-1-14
You could make a little wrapper around myfunc which maintains a previous value...or values
function out = callMyfunc(arg1, arg2)
persistent oldVal;
if isempty(oldVal)
oldVal = cell(1,2);
oldVal{1} = {arg1, arg2};
oldVal{2} = myfunc(arg1, arg2);
out = oldVal{2};
return;
end
% Check to see if we've evaluated this one before
if (isequal({arg1, arg2}, oldVal{1}))
out = oldVal{2}; %Use stored value
else
out = myfunc(arg1, arg2);
end
Alternatively, I could imagine just making a handle class which maintains the old values and passing that in and out of the calls to callMyfunc if you don't like the persistent idea. Then, you attempt to record each evaluation if the lookup fails.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!