Ordering of OptimizationVariables in an OptimizationProblem

When I use the problem-based optimization framework to create an OptimizationProblem, e.g.,
r=optimvar('r',5,'LowerBound',0,'UpperBound',1);
B= optimvar('B', 3);
prob=optimproblem('Constraints', sum(r)<=1,'Objective', sum(r)+sum(B));
the ordering of the variables in prob appears to be alphabetical,
prob.Variables
ans = struct with fields:
B: [3×1 optim.problemdef.OptimizationVariable] r: [5×1 optim.problemdef.OptimizationVariable]
However, I would prefer the ordering to be r first, then B. I thought I might be able to use orderfields() to change it, but no luck:
prob.Variables=orderfields(prob.Variables,{'r','B'})
OptimizationProblem property 'Variables' is read-only.
Is there a supported way of doing this?
The reason I want to do this is so that when I use,
s=prob2struct(prob);
the sub-matrix partitions of s.lb, s.ub, s.Aeq, etc... corresponding to r and B will have an ordering that I can know and control in advance. I'm assuming this ordering is dictated by the ordering of the fields in prob.Variables.

3 个评论

Is it worth the hassle to use this modern/automatic "problem-based framework"? I've always shied away from this type of automatic parsing just because of not knowing/not being sure about how these things are handled. What are the benefits you see/expect?
IMO, it is at minimum often a convenient tool for setting up linear constraints. It spares you the mental gymnastics of having to figure out the matrix form of the constraints, especially when the constraints may involve multi-dimensional sums. Also, when the original mathematical formulation of the problem involves multiple unknown arrays like r and B, it spares you the effort of having to concatenate them into a single unknown vector.
So, I might be better off if I learn a new trick...

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by