GA - objective and constraints have to run the same expensive function
6 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I'm trying to use GA for a constrained optimisation problem. The issue is that constraint function requires a value that is calculated during the execution of the objective function. In this code snippet, I've kept a dummy fem function.
But in the actual code (Which I can't share), the fem.m function returns the fitness value (needed for objective function) and a displacement value (needed for constraint function).
Is there a way that I can call the function only once? Or somehow store the displacement value and use it while calling the constraint?
clear
clc
fun = @(x) objective(x);
con = @(x) constraint(x);
% dummy number of variables. here taken as 3
lb = [ 0 0 0 ];
ub = [ 100 100 100];
options = optimoptions('ga','Display','iter');
[x,fval,exitflag,output,population,scores]=ga(fun,3,[],[],[],[],lb,ub,con,options);
function energy = objective(x)
% expensive finite element function call
[energy, displacement] = fem(x);
end
function [c,ceq] = constraint(x)
ceq = [];
% need to call expensive fem again
[energy, displacement] = fem(x);
c = [4 - displacement];
end
%Dummy fem function, in reality not explicit and computationally expensive
function [energy,displacement] = fem(x);
energy = sum(x);
displacement = x(2);
end
0 件のコメント
回答 (3 件)
Alan Weiss
2022 年 8 月 31 日
This is a difficult problem because of the way that ga calls functions, both fitness and nonlinear constraint. For most solvers, the objective and constraint functions at a point are called right after each other. However, for ga I believe that all of the fitness functions for a population are called together, and all the constraint functions are called together, meaning there is little opportunity to cache these values as done for other solvers in Objective and Nonlinear Constraints in the Same Function or Objective and Constraints Having a Common Function in Serial or Parallel, Problem-Based.
Sorry.
But do you really need to use ga? Have you tried patternsearch, possibly starting from a variety of initial points? Or possibly surrogateopt, which is designed for computationally intensive functions?
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
0 件のコメント
Steven Lord
2022 年 8 月 31 日
One potential solution would be to memoize your expensive function, pass the memoized function into your objective and constraint function as an additional parameter, and evaluate it using that memoized function.
f = memoize(@expensiveFunction);
tic
y = f(1);
toc
tic
z = f(1);
toc
check = isequal(y, z) % true
function out = expensiveFunction(in)
pause(2)
out = in + 1;
end
0 件のコメント
John D'Errico
2022 年 8 月 31 日
編集済み: John D'Errico
2022 年 8 月 31 日
I would try memoization.
help memoize
For example, we can use it as in the example below. I've created a function that does little computationally, but all that matters is it will be called only once for a given set of parameters.
myfunmemo = memoize(@myfun);
Now test it out. First, see that myfun does execute when we call it, and it dumps out a comment that it was called, and executed.
myfun(1:5)
myfun(1:5)
However, do the same for myfunmemo.
myfunmemo(1:5)
myfunmemo(1:5)
As you can see, there was only one call made to the origianl function. The second call used the cached value. So if it is terribly expensive to make that call, a memoization will allow you to avoid that repeated call. Note that memoization does work for non-integer arguments.
myfunmemo(pi + (1:10))
myfunmemo(pi + (1:10))
Will memoization cause a problem if the cache grows large? Well, probably so. I assume there will be a quadratic cost penalty associated with memoization, as the cache grows internally. But if the call itself was too costly to be done twice, you may still see a gain.
function Y = myfun(X)
Y = sum(X); % what it does internally is irrelevant.
disp("Initial call for this set of parameters")
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Genetic Algorithm についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!