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. <stopping criteria details>
接下来,使用 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. <stopping criteria details>
要将 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 不能。
输入参数
优化问题或方程问题,指定为 OptimizationProblem 对象或 EquationProblem 对象。使用 optimproblem 创建优化问题;使用 eqnproblem 创建方程问题。
警告
基于问题的方法不支持在目标函数、非线性等式和非线性不等式中使用复数值。如果某函数计算具有复数值,即使是作为中间值,最终结果也可能不正确。
示例: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 = cons1;
示例: prob = eqnproblem; prob.Equations = eqs;
变量名称,指定为字符向量或字符串。
示例: 'x'
数据类型: char | string
输出参量
问题变量的线性索引,以结构体或整数向量形式返回。如果您使用 prob2struct 将 prob 转换为问题结构体,则 idx 将在生成的问题结构体中给出变量索引,这些索引对应于 prob 中的变量。
当您调用
idx = varindex(prob)时,返回的idx是结构体。该结构体的字段名称是prob中的变量名称。每个字段的值是关联的基于求解器的问题变量中的变量映射到的线性索引的整数向量。当您调用
idx = varindex(prob,varname)时,返回的idx是关联的基于求解器的问题变量中的变量varname映射到的线性索引的向量。
请参阅获取问题索引。
版本历史记录
在 R2019a 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)