# OptimizationProblem

## 属性

```prob = optimproblem('Objective',5*brownies + 2*cookies) % or prob = optimproblem; prob.Objective = 5*brownies + 2*cookies```

```constrs = struct('TrayArea',10*brownies + 20*cookies <= traysize,... 'TrayWeight',12*brownies + 18*cookies <= maxweight); prob = optimproblem('Constraints',constrs) % or prob.Constraints.TrayArea = 10*brownies + 20*cookies <= traysize prob.Constraints.TrayWeight = 12*brownies + 18*cookies <= maxweight```

`prob.Constraints.TrayArea = [];`

## 对象函数

 `optimoptions` 创建优化选项 `prob2struct` 将优化问题或方程问题转换为求解器形式 `show` 显示有关优化对象的信息 `solve` 求解优化问题或方程问题 `solvers` Determine default and valid solvers for optimization problem or equation problem `varindex` 将问题变量映射到基于求解器的变量索引 `write` 保存优化对象描述

## 示例

`prob = optimproblem('ObjectiveSense','max');`

```x = optimvar('x',2,1,'LowerBound',0); prob.Objective = x(1) + 2*x(2);```

```cons1 = x(1) + 5*x(2) <= 100; cons2 = x(1) + x(2) <= 40; cons3 = 2*x(1) + x(2)/2 <= 60; prob.Constraints.cons1 = cons1; prob.Constraints.cons2 = cons2; prob.Constraints.cons3 = cons3;```

`show(prob)`
``` OptimizationProblem : Solve for: x maximize : x(1) + 2*x(2) subject to cons1: x(1) + 5*x(2) <= 100 subject to cons2: x(1) + x(2) <= 40 subject to cons3: 2*x(1) + 0.5*x(2) <= 60 variable bounds: 0 <= x(1) 0 <= x(2) ```

`sol = solve(prob);`
```Solving problem using linprog. Optimal solution found. ```
`sol.x`
```ans = 2×1 25 15 ```