Programmatically testing whether a variable is an optimoptions object

2 ビュー (過去 30 日間)
Matt J
Matt J 2020 年 2 月 27 日
編集済み: Matt J 2020 年 2 月 28 日
I was wondering if there was something like an isoptimoptions function that could be used to test in general whether a variable was generated by optimoptions(). The isa command alone will not seem to do it, because optimoptions objects occupy separate classes, depending on which solver the object was generated for.
>> opts1=optimoptions('lsqnonlin');
>> opts2=optimoptions('linprog');
>> whos opts1 opts2
Name Size Bytes Class Attributes
opts1 1x1 29913 optim.options.Lsqnonlin
opts2 1x1 14775 optim.options.Linprog
The following user-defined command seems to work, but I'm wondering if there are more direct alternatives,
function bool=isaoptimoptions(opts)
bool = startsWith(class(opts),'optim.options')
end
  4 件のコメント
Guillaume
Guillaume 2020 年 2 月 28 日
superclasses(obj)
will tell you the base classes (if any). isa on the base class would work.
Matt J
Matt J 2020 年 2 月 28 日
Theyhave several parent classes, but those I've checked all seem to have 'optim.options.SolverOptions' as a parent in common.
>> superclasses(optimoptions('linprog'))
Superclasses for class optim.options.Linprog:
optim.options.MultiAlgorithm
optim.options.SolverOptions
matlab.mixin.CustomDisplay
>> superclasses(optimoptions('intlinprog'))
Superclasses for class optim.options.Intlinprog:
optim.options.SingleAlgorithm
optim.options.SolverOptions
matlab.mixin.CustomDisplay
>> superclasses(optimoptions('lsqlin'))
Superclasses for class optim.options.Lsqlin:
optim.options.MultiAlgorithm
optim.options.SolverOptions
matlab.mixin.CustomDisplay

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

採用された回答

Guillaume
Guillaume 2020 年 2 月 28 日
編集済み: Guillaume 2020 年 2 月 28 日
"those I've checked all seem to have 'optim.options.SolverOptions' as a parent in common."
In that case:
isa(opts, 'optim.options.SolverOptions')
edit:
Strangely enough the base class optim.options.SolverOptions is part of base matlab (or a toolbox I've got even though I've haven't got the optimisation toolbox) and available as an m file.
The comment in the m file does say that this is the "base class for Optimization Toolbox options", so yes, the above is what you want.
  1 件のコメント
Matt J
Matt J 2020 年 2 月 28 日
編集済み: Matt J 2020 年 2 月 28 日
Yeah, I guess that makes sense. It would be nice if it were documented in the toolbox that this will always be the root class ...

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

その他の回答 (1 件)

Luna
Luna 2020 年 2 月 28 日
Hi,
Maybe something like this helps?
metaClass = metaclass(opts1);
if contains(metaClass.Name,'optim.options')
disp('This is a optim.options object.')
else
end
  1 件のコメント
Matt J
Matt J 2020 年 2 月 28 日
Hi Luna,
That would work, but is pretty much the same as the provisional solution mentioned in my original post.

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

カテゴリ

Help Center および File ExchangeLinear Programming and Mixed-Integer Linear Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by