How do I implement simulannealbnd function in simulink?

조회 수: 2 (최근 30일)
Carlo Loregian
Carlo Loregian 2021년 3월 11일
댓글: Carlo Loregian 2021년 3월 11일
Hello,
I am trying to implement simulated annealing optimisation into my simulink model through the Matlab function block. Unfortunatey when I run the simulation I get the error 'Function 'simulannealbnd' not supported for code generation.'. I understand this is a limitation of the function and it cannot be directly implemented in simulink, however I was wondering if there were any workarounds or equivalent simulink functions that I can use. Here is a snippet of the code I am using:
function [c_batt, c_sc] = sa_optimisation(P_dem, Pmax_batt, Pmax_sc)
%Prerequisites
lb = [-1,-1];
ub = [1,1];
x0 = lb;
rng default
%Execute cost function
cost_function = @(x,y)powerCostFunction(P_dem, x, y, Pmax_batt, Pmax_sc);
loss = @(p)cost_function(p(1),p(2));
x = simulannealbnd(loss,x0,lb,ub);
c_batt = x(1);
c_sc = x(2);
end

채택된 답변

Abdolkarim Mohammadi
Abdolkarim Mohammadi 2021년 3월 11일
You can declare the functions that are not compatible with code generation using coder.extrinsic() as extrinsic functions. Simply try this code as the very first command in your function:
coder.extrinsic ('simulannealbnd');
However, you should notice that extrinsic functions are executed in the MATLAB environment. Therefore, this adds an overhead to the overall execution time, but it is often negligible.
  댓글 수: 2
Carlo Loregian
Carlo Loregian 2021년 3월 11일
Thanks for the quick reply.
I have tried that, however that creates another error stating: 'Function handles cannot be passed to extrinsic functions.'. Is there a way to call simulannealbnd without a function handle as an argument?
Carlo Loregian
Carlo Loregian 2021년 3월 11일
Following this answer and this workaround for function handles, the simulation now runs.
@Abdolkarim Mohammadi-Balani Thanks for the help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by