Defining objective function using nested functions (Genetic Algorithm Solver)
2 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone,
I am trying to do a maximization problem using optimization toolbox genetic algorithm solver. My objective function has parameters so I need to write a nested function(Since i have extra parameters besides from my variables) But i am having a trouble writing it. Here is what my objective function looks like and i wrote so far;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/938719/image.png)
function z = objectivefunc(x,r,aii)
z= sum(x.*r.*aii, 'all');
end
(I reshaped my matrixes to be a row vector.)
X is my decsision variable and its dimensions are 1*1296 and r and aii are my parameters with dimensions 1*1296. I have looked through the documentation matlab provides and found this one;
I am very new to MATLAB so i really do not know how to write it correctly. So, any ideas or suggestions on how to implement nested functions to my problem?
3 件のコメント
Torsten
2022 年 3 月 23 日
編集済み: Torsten
2022 年 3 月 23 日
This problem has nothing to do with nested functions. You pass r and a to objectivefunc - so the two matrices can be accessed there. Maybe in a former version of your code, you did not include r and a in the list of parameters passed to objectivefunc and so someone suggested that you should use nested functions (because r and a are then also visible in objectivefunc although you don't pass them).
If you define the array of initial values for the unknowns x0 for "ga" as a 36x36 matrix, you will find them in objectivefunc also as a 36x36 matrix and you can use Matt's answer to calculate z if r is 36x36 and a is 36x1.
採用された回答
その他の回答 (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!