The "run" command does not work in GARDER.

5 ビュー (過去 30 日間)
Zbigniew Garncarek
Zbigniew Garncarek 2024 年 2 月 17 日
コメント済み: Piotr Kot 2024 年 12 月 20 日
A script using Multi Start to search for a global minimum works in MATLAB, but an error appears in GARDER:
Error: You may not use the command(s) run in your code
Why does this happen, and how can you prevent it?
kol101=@(x)x.^2+2*sin(x.^2)+exp(-x);
opts = optimoptions(@fmincon,'Algorithm','sqp');
problem = createOptimProblem('fmincon','objective',...
kol101,'x0',3,'lb',1.2,'ub',4.8,'options',opts);
ms = MultiStart;
[xmin,fmin,flaga] = run(ms,problem,20)
fplot(kol101,[1.2,4.8])
hold on
plot(xmin,fmin,'rp')

採用された回答

Cris LaPierre
Cris LaPierre 2024 年 2 月 17 日
編集済み: Cris LaPierre 2024 年 2 月 17 日
I assume you are refering to MATLAB Grader. Certain commands and functions are not allowed when running code in MATLAB Grader, including run. However, the aim was likley to prevent use of the run function, which would allow users to circumvent restrictions an instructor places on solving a problem.
Ok, that's the why. In your case, you are not using the (very overlaoded) run function. Instead, you are calling an object function of MultiStart. That provides the workaround you need to use MultiStart in Grader. There are two ways to invoke a method (a.k.a. object function), which you can read about here.
  • obj.methodName(arg)
  • methodName(obj,arg)
Use the first syntax with dot notation, and the code will run in Grader
kol101=@(x)x.^2+2*sin(x.^2)+exp(-x);
opts = optimoptions(@fmincon,'Algorithm','sqp');
problem = createOptimProblem('fmincon','objective',...
kol101,'x0',3,'lb',1.2,'ub',4.8,'options',opts);
ms = MultiStart;
[xmin,fmin,flaga] = ms.run(problem,20)
MultiStart completed the runs from all start points. All 20 local solver runs converged with a positive local solver exitflag.
xmin = 2.0510
fmin = 2.5856
flaga = 1
fplot(kol101,[1.2,4.8])
hold on
plot(xmin,fmin,'rp')
  2 件のコメント
Zbigniew Garncarek
Zbigniew Garncarek 2024 年 2 月 20 日
Thank you very much for the explanation.
Piotr Kot
Piotr Kot 2024 年 12 月 20 日
"Certain commands and functions are not allowed when running code in MATLAB Grader, including run"
But unfortunately, that's not true
Just take a look at the thread
or
Currently, any Matlab command, including system or run, can be run, and the method described in these threads is acceptable by administrators!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Matrix Inequalities についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by