GA - objective and constraints have to run the same expensive function
조회 수: 11 (최근 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!