主要内容

查看或修改优化问题

审查问题使用show要么

创建优化问题后,您可以通过使用审核其配方show。对于大问题,使用反而。例如,

prob = optimproblem; x = optimvar('x',2,'LowerBound',0); prob.Objective = x(1) - 2*x(2); prob.Constraints.cons1 = x(1) + 2*x(2) <= 4; prob.Constraints.cons2 = -x(1) + x(2) <= 1; show(prob)
优化问题: Solve for: x minimize : x(1) - 2*x(2) subject to cons1: x(1) + 2*x(2) <= 4 subject to cons2: -x(1) + x(2) <= 1 variable bounds: 0 <= x(1) 0 <= x(2)

This review shows the basic elements of the problem, such as whether the problem is to minimize or maximize, and the variable bounds. The review shows the index names, if any, used in the variables. The review does not show whether the variables are integer valued.

更改默认求解器或选项

尝试改善解决方案或处理的速度ion, examine and change the default solver or options.

要查看默认求解器和选项,请使用optimoptions(prob)。例如,

rng默认x = Optimvar('x',3,'低行',0);expr = sum((rand(3,1)。* x)。^ 2);prob = OptimProblem('目标',expr);prob.constraints.lincon = sum(sum(randn(size(x))。* x))<= RANDN;选项= Optimoptions(prob)
options = lsqlin options: Options used by current Algorithm ('interior-point'): (Other available algorithms: 'trust-region-reflective') Set properties: No options set. Default properties: Algorithm: 'interior-point' ConstraintTolerance: 1.0000e-08 Display: 'final' LinearSolver: 'auto' MaxIterations: 200 OptimalityTolerance: 1.0000e-08 StepTolerance: 1.0000e-12 Show options not used by current Algorithm ('interior-point')

此问题的默认解算器是lsqlin, and you can see the default options.

To change the solver, set the'求解'名称值对solve。要查看不同的求解器的适用选项,请使用optimoptionsto pass the current options to the different solver. For example, continuing the problem,

选项= Optimoptions('quadprog',选项)
options = quadprog options: Options used by current Algorithm ('interior-point-convex'): (Other available algorithms: 'trust-region-reflective') Set properties: ConstraintTolerance: 1.0000e-08 MaxIterations: 200 OptimalityTolerance: 1.0000e-08 StepTolerance: 1.0000e-12 Default properties: Algorithm: 'interior-point-convex' Display: 'final' LinearSolver: 'auto' Show options not used by current Algorithm ('interior-point-convex')

要更改选项,请使用optimoptions要么dot notation to set options, and pass the options tosolve在里面'Options'name-value pair. See常用选项:调整和故障排除。继续这个例子,

options.Display = 'iter'; sol = solve(prob,'Options',options,'Solver','quadprog');
ITER FVAL PRIMAL INFEAS双INFEAS互补0 1.500359E + 00 3.068423E-01 2.275423E-01 2.500000E-01 1 1.728717E-01 0.000000E + 00 7.719860E-03 3.637874E-02 2 2.6041080-02 0.0000000-02 0.000000E + 000.000000E + 00 5.245260E-03 3 7.822161E-03 0.000000E + 00 2.775558E-17 1.40791858E-17 2.909218E-03 0.000000E + 00 6.938894C-18 2.0707894C-18 2.0707894E-18 2.0707894E-18 1.931264C-03 1.931264C-03 1.931264E-03 0.000000E +00 1.734723E-18 2.907724E-05 6 1.797508E-03 0.000000E + 00 2.602085E-18 4.083167E-18 7 1.775398E-03 0.000000E + 00 4.336809E-19 5.1024530-07 5.177253C-07 5 1.772971C-03 1.772971E-03 0.00000000+00 2.632684E-19 3.064243E-08 9 1.772848E-03 0.000000E + 00 5.228973E-19 4.371356E-11发现满足约束的最小值。优化完成,因为目标函数在可行的方向上是非减小的,以便在最优耐受性的值内,并且对约束公差的值满足约束。

Correct a Misspecified Problem

To check that your problem is correct, review all its aspects. For example, create an optimization problem to solve a Sudoku problem by running this script.

x = optimvar (9 ' x ', 9日,9日下界,0,' UpperBound ',1); cons1 = sum(x,1) == 1; cons2 = sum(x,2) == 1; cons3 = sum(x,3) == 1; prob = optimproblem; prob.Constraints.cons1 = cons1; prob.Constraints.cons2 = cons2; prob.Constraints.cons3 = cons3; mul = ones(1,1,9); mul = cumsum(mul,3); prob.Objective = sum(sum(sum(x,1),2).*mul); cons4 = optimconstr(3,3,9); for u = 1:3 for v = 1:3 arr = x(3*(u-1)+1:3*(u-1)+3,3*(v-1)+1:3*(v-1)+3,:); cons4(u,v,:) = sum(sum(arr,1),2) <= ones(1,1,9); end end prob.Constraints.cons4 = cons4; B = [1,2,2; 1,5,3; 1,8,4; 2,1,6; 2,9,3; 3,3,4; 3,7,5; 4,4,8; 4,6,6; 5,1,8; 5,5,1; 5,9,6; 6,4,7; 6,6,5; 7,3,7; 7,7,6; 8,1,4; 8,9,8; 9,2,3; 9,5,4; 9,8,2]; for u = 1:size(B,1) x.LowerBound(B(u,1),B(u,1),B(u,1)) = 1; end

This script has some errors that you can find by examining the variables, objective, and constraints. First, examine the variablex

x
x = 9×9×9优化Variable阵列具有属性:数组 - 范围属性:名称:'x'类型:'连续'索引序列:{{} {} {}}元素属性:低行:[9×9×9双]上行:[9×9×9双]看到展示变量。看到展示的边界。

此显示显示变量的类型是连续的。变量应为整数值。更改类型。

x.Type = 'integer'
x = 9×9×9具有属性的优化阵列:数组范围内容:名称:'x'类型:'整数'indexnames:{{} {} {}}元素级属性:低行:[9×9×9双]上行:[9×9×9双]看到展示变量。看到展示的边界。

检查界限。应该有21个下限,值1,每行1个B。Becausex是一个大的数组,将界限写入文件,而不是在命令行上显示它们。

写作(x,'xbounds.txt')

搜索文件xbounds.txt适用于所有实例1 <=。Only nine lower bounds having the value 1, in the variablesx(1,1,1),x(2,2,2),......,x(9,9,9)。To investigate this discrepancy, examine the code where you set the lower bounds:

for u = 1:size(B,1) x.LowerBound(B(u,1),B(u,1),B(u,1)) = 1; end

The line inside the loop should sayX.LowerBound(B(U,1),B(U,2),B(U,3))= 1;。将所有下限重置为零,然后运行更正的代码。

x.LowerBound = 0; for u = 1:size(B,1) x.LowerBound(B(u,1),B(u,2),B(u,3)) = 1; end writebounds(x,'xbounds.txt')

xbounds.txtnow has the correct number of lower bound entries that are 1.

检查目标函数。目标函数表达式很大,因此将表达式写入文件。

写(prob.Objective,'objectivedescription.txt')
x(1, 1, 1) + x(2, 1, 1) + x(3, 1, 1) + x(4, 1, 1) + x(5, 1, 1) + x(6, 1, 1) + x(7, 1, 1) + x(8, 1, 1) + x(9, 1, 1) + x(1, 2, 1) + x(2, 2, 1) + x(3, 2, 1) + x(4, 2, 1) + x(5, 2, 1) + x(6, 2, ... 9*x(7, 8, 9) + 9*x(8, 8, 9) + 9*x(9, 8, 9) + 9*x(1, 9, 9) + 9*x(2, 9, 9) + 9*x(3, 9, 9) + 9*x(4, 9, 9) + 9*x(5, 9, 9) + 9*x(6, 9, 9) + 9*x(7, 9, 9) + 9*x(8, 9, 9) + 9*x(9, 9, 9)

The objective function looks reasonable, because it is a sum of scalar expressions.

Write the constraints to files for examination.

写(prob.constraints.cons1,'cons1.txt')写(prob.constraints.cons2,'cons2.txt')写(prob.constraints.cons3,'cons3.txt')写(prob.constraints.cons4,'cons4.txt')

Reviewcons4.txt而且你看到了一个错误。所有约束都是不平等而非平等。更正创建此约束的代码行,并在问题中置于纠正的约束。

cons4 = OptimConstr(3,3,9);对于v = 1:3的v = 1:3 arr = x(3 *(u-1)+1:3 *(u-1)+ 3,3 *(v-1)+1:3 *(v-1)+3,:);cons4(u,v,:) = sum(sum(arr,1),2)== a(1,1,9);结束end prob.constraints.cons4 = cons4;

在这些更改后,您可以成功解决问题。

sol =解决(prob);x =圆形(sol.x);Y = y =(尺寸(x));对于k = 2:9 y(:,:,k)= k;每个深度k结束s = x的%乘法器。* y;%乘以其深度s = sum的每个条目(s,3);%s是9-by-9并握住求助的拼图拉丝(s)

重复变量名称

If you recreate a variable, but already have an expression that uses the old variable, then you can get errors when incorporating the expressions into a single problem. See禁止具有重复名称的变量

See Also

|||||||

Related Topics