varindex
将问题变量映射到基于求解器的变量索引
说明
示例
获取问题索引
创建一个优化问题。
x = optimvar('x',3); y = optimvar('y',3,3); prob = optimproblem('Objective',x'*y*x);
将问题转换为结构体。
problem = prob2struct(prob);
获得 problem
中所有 prob
变量的线性索引。
idx = varindex(prob); disp(idx.x)
1 2 3
disp(idx.y)
4 5 6 7 8 9 10 11 12
仅获得 y
索引。
idxy = varindex(prob,'y')
idxy = 1×9
4 5 6 7 8 9 10 11 12
用两种方法求解问题
此示例说明如何使用基于问题的方法或基于求解器的方法获取大部分相同的信息。首先创建一个问题,并使用基于问题的方法来求解它。
x = optimvar('x',3,1,'LowerBound',1,'UpperBound',1); y = optimvar('y',3,3,'LowerBound',-1,'UpperBound',1); prob = optimproblem('Objective',x'*y*x + [2 3 4]*x); rng default x0.x = rand(3, 1); x0.y = rand(3, 3); [solp,fvalp,exitflagp,outputp] = solve(prob,x0);
Solving problem using fmincon. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
接下来,使用 prob2struct
将问题转换为基于求解器的形式。要使 fmincon
求解器在问题中使用自动梯度,请将 SpecifyObjectiveGradient
选项设置为 true
。
solverprob = prob2struct(prob,x0);
solverprob.options = optimoptions(solverprob.options,"SpecifyObjectiveGradient",true);
使用 fmincon
求解问题。
[sols,fvals,exitflags,outputs] = fmincon(solverprob);
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
要将 fmincon
解转换为 solve
返回的结构体形式,请使用 varindex
创建适当的结构体。
idx = varindex(prob); sol.x = sols(idx.x); sol.y = sols(idx.y);
y
使用的 varindex
索引是线性索引。将变量 sol.y
重构为大小为 x0.y
。
sol.y = reshape(sol.y,size(x0.y));
检查两个解结构体是否相同。
isequal(sol,solp)
ans = logical
1
这两种方法不完全等效的原因是 fmincon
可以返回更多参数,如拉格朗日乘数,而 solve
不能。
输入参数
prob
— 优化问题或方程问题
OptimizationProblem
对象 | EquationProblem
对象
优化问题或方程问题,指定为 OptimizationProblem
对象或 EquationProblem
对象。使用 optimproblem
创建优化问题;使用 eqnproblem
创建方程问题。
警告
基于问题的方法不支持目标函数、非线性等式或非线性不等式中使用复数值。如果某函数计算具有复数值,即使是作为中间值,最终结果也可能不正确。
示例: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 = cons1;
示例: prob = eqnproblem; prob.Equations = eqs;
varname
— 变量名称
字符向量 | 字符串
变量名称,指定为字符向量或字符串。
示例: 'x'
数据类型: char
| string
输出参数
idx
— 问题变量的线性索引
结构体 | 整数向量
问题变量的线性索引,以结构体或整数向量形式返回。如果您使用 prob2struct
将 prob
转换为问题结构体,则 idx
将在生成的问题结构体中给出变量索引,这些索引对应于 prob
中的变量。
当您调用
idx = varindex(prob)
时,返回的idx
是结构体。该结构体的字段名称是prob
中的变量名称。每个字段的值是关联的基于求解器的问题变量中的变量映射到的线性索引的整数向量。当您调用
idx = varindex(prob,varname)
时,返回的idx
是关联的基于求解器的问题变量中的变量varname
映射到的线性索引的向量。
请参阅获取问题索引。
版本历史记录
在 R2019a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)