# optimproblem

## 语法

``prob = optimproblem``
``prob = optimproblem(Name,Value)``

## 说明

``prob = optimproblem` 用默认属性创建优化问题。`

````prob = optimproblem(Name,Value)` 使用一个或多个 `Name,Value` 对组参量指定的其他选项。例如，要指定最大化问题而不是最小化问题，请使用 `prob = optimproblem('ObjectiveSense','maximize')`。 注意一个优化问题中的所有名称必须具有唯一性。具体来说，所有变量名称、目标函数名称和约束函数名称都不能相同。 ```

## 示例

`prob = optimproblem`
```prob = OptimizationProblem with properties: Description: '' ObjectiveSense: 'minimize' Variables: [0x0 struct] containing 0 OptimizationVariables Objective: [0x0 OptimizationExpression] Constraints: [0x0 struct] containing 0 OptimizationConstraints No problem defined. ```

`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 ```

```x = optimvar("x",2,LowerBound=-2,UpperBound=2); prob = optimproblem; prob.Objective.first = norm(x)^2; prob.Objective.second = norm(x - [1;0])^2;```

```rng default % For reproducibility sol = solve(prob);```
```Solving problem using gamultiobj. gamultiobj stopped because the average change in the spread of Pareto solutions is less than options.FunctionTolerance. ```

`paretoplot(sol)`

`sol(9).x`
```ans = 2×1 0.5544 -0.0306 ```

## 输入参数

### 名称-值参数

• 对于标量（单目标）问题，将目标函数指定为标量优化表达式或以标量优化表达式为值的结构体。

• 对于多目标问题，将目标函数指定为向量值优化表达式、优化表达式数组或优化表达式结构体。例如，以下目标是包含以标量优化变量 `x`表示的优化表达式的结构体：

```prob = optimproblem; prob.Objective.first = x^2; prob.Objective.second = (x + 1)^2;```

`ObjectiveSense` 可以是具有 `'minimize'``'min'``'maximize'``'max'` 值的结构体。当问题目标是结构体时，可以使用这种形式。`Objective``ObjectiveSense` 结构体应具有相同的字段名称，因此 `ObjectiveSense` 适用于对应的 `Objective`。例如，

```x = optimvar('x',2,"UpperBound",2,"LowerBound",-2); prob = optimproblem; prob.Objective.first = norm(x)^2; prob.Objective.second = -norm(x - [1;0])^2; prob.ObjectiveSense.first = "min"; prob.ObjectiveSense.second = "max";```