# 混合整数线性规划基础：基于问题

### 问题描述

`$\begin{array}{ccccc}Ingot& Weight\phantom{\rule{0.2777777777777778em}{0ex}}in\phantom{\rule{0.2777777777777778em}{0ex}}Tons& %\phantom{\rule{0.2777777777777778em}{0ex}}Carbon& %\phantom{\rule{0.2777777777777778em}{0ex}}Molybdenum& \frac{Cost}{Ton}\\ 1& 5& 5& 3& 350\\ 2& 3& 4& 3& 330\\ 3& 4& 5& 4& 310\\ 4& 6& 3& 4& 280\end{array}$`

`$\begin{array}{cccc}Alloy& %\phantom{\rule{0.2777777777777778em}{0ex}}Carbon& %\phantom{\rule{0.2777777777777778em}{0ex}}Molybdenum& \frac{Cost}{Ton}\\ 1& 8& 6& 500\\ 2& 7& 7& 450\\ 3& 6& 8& 400\\ Scrap& 3& 9& 100\end{array}$`

### 表示问题

```steelprob = optimproblem; ingots = optimvar('ingots',4,'Type','integer','LowerBound',0,'UpperBound',1); alloys = optimvar('alloys',3,'LowerBound',0); scrap = optimvar('scrap','LowerBound',0);```

```weightIngots = [5,3,4,6]; costIngots = weightIngots.*[350,330,310,280]; costAlloys = [500,450,400]; costScrap = 100; cost = costIngots*ingots + costAlloys*alloys + costScrap*scrap;```

`steelprob.Objective = cost;`

`totalWeight = weightIngots*ingots + sum(alloys) + scrap;`

```carbonIngots = [5,4,5,3]/100; carbonAlloys = [8,7,6]/100; carbonScrap = 3/100; totalCarbon = (weightIngots.*carbonIngots)*ingots + carbonAlloys*alloys + carbonScrap*scrap;```

```molybIngots = [3,3,4,4]/100; molybAlloys = [6,7,8]/100; molybScrap = 9/100; totalMolyb = (weightIngots.*molybIngots)*ingots + molybAlloys*alloys + molybScrap*scrap;```

```steelprob.Constraints.conswt = totalWeight == 25; steelprob.Constraints.conscarb = totalCarbon == 1.25; steelprob.Constraints.consmolyb = totalMolyb == 1.25;```

### 求解问题

`[sol,fval] = solve(steelprob);`
```Solving problem using intlinprog. Running HiGHS 1.6.0: Copyright (c) 2023 HiGHS under MIT licence terms Presolving model 3 rows, 8 cols, 24 nonzeros 3 rows, 8 cols, 18 nonzeros Solving MIP model with: 3 rows 8 cols (4 binary, 0 integer, 0 implied int., 4 continuous) 18 nonzeros Nodes | B&B Tree | Objective Bounds | Dynamic Constraints | Work Proc. InQueue | Leaves Expl. | BestBound BestSol Gap | Cuts InLp Confl. | LpIters Time 0 0 0 0.00% 0 inf inf 0 0 0 0 0.0s 0 0 0 0.00% 8125.6 inf inf 0 0 0 4 0.0s R 0 0 0 0.00% 8495 8495 0.00% 5 0 0 5 0.0s Solving report Status Optimal Primal bound 8495 Dual bound 8495 Gap 0% (tolerance: 0.01%) Solution status feasible 8495 (objective) 0 (bound viol.) 0 (int. viol.) 0 (row viol.) Timing 0.01 (total) 0.00 (presolve) 0.00 (postsolve) Nodes 1 LP iterations 5 (total) 0 (strong br.) 1 (separation) 0 (heuristics) Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 1e-06. The intcon variables are integer within tolerance, options.ConstraintTolerance = 1e-06. ```

`sol.ingots`
```ans = 4×1 1 1 0 1 ```
`sol.alloys`
```ans = 3×1 7.2500 0 0.2500 ```
`sol.scrap`
```ans = 3.5000 ```
`fval`
```fval = 8495 ```