# Intro: Linear/Nonlinear Optimization in Python

Python 提供多种库来解决最优化的问题，对线性规划，python提供了linprog,也可以直接使用minimize. 如以下的问题：
$$ \large 目标函数：\begin{equation}\begin{array}{l} min  f=-\ x_1-\ x_2-\ x_3 \end{array}\end{equation}  $$  
$$ \large s.t. \left\{  
\begin{array}{rcl}  
7 \ x_1 +3 \ x_2+9 \ x_3 \leq 1\\
8 \ x_1 +5 \ x_2+4 \ x_3 \leq 1\\
6 \ x_1 +9 \ x_2+5 \ x_3 \leq 1\\
x_1,x_2,x_3 \geq 0
\end{array} \right. $$


```python
import numpy as np
from scipy import optimize
import pandas as pd
```


```python
f = np.array([-1, -1, -1])
A_ub = np.array([[7, 3, 9],[8, 5, 4],[6, 9, 5]])
b_ub = np.array([1, 1, 1])
lb =((0,100),(0,100),(0,100));
optimize.linprog(f, A_ub, b_ub,bounds=lb)
```




         con: array([], dtype=float64)
         fun: -0.15415019419978693
     message: 'Optimization terminated successfully.'
         nit: 9
       slack: array([9.32575905e-09, 2.64429326e-07, 9.03222153e-09])
      status: 0
     success: True
           x: array([0.08695645, 0.03557314, 0.0316206 ])



Alternatively


```python
v = np.array([-1, -1, -1])
def f(x,v):
    return np.sum(x*v)
initial=np.array([0,0,0])
cons1={'type':'eq','fun':lambda x:f(x,A_ub[0])-b_ub[0]}
cons2={'type':'eq','fun':lambda x:f(x,A_ub[1])-b_ub[1]}
cons3={'type':'eq','fun':lambda x:f(x,A_ub[2])-b_ub[2]}
res=optimize.minimize(f,initial,v,constraints=[cons1,cons2,cons3],bounds=((0,100),(0,100),(0,100)))
res
```




         fun: -0.15415019762845847
         jac: array([-1., -1., -1.])
     message: 'Optimization terminated successfully.'
        nfev: 10
         nit: 2
        njev: 2
      status: 0
     success: True
           x: array([0.08695652, 0.03557312, 0.03162055])




```python
fun = lambda x: -1*x[0] - 1*x[1] - 1*x[2]
cons = ({'type': 'eq', 'fun': lambda x:  7*x[0] + 3*x[1] + 9*x[2] - 1},
        {'type': 'eq', 'fun': lambda x: 8*x[0] + 5*x[1] + 4*x[2]- 1},
        {'type': 'eq', 'fun': lambda x: 6*x[0] + 9*x[1] + 5*x[2] - 1})
bnds = ((0, 100), (0, 100), (0,1))
res = optimize.minimize(fun, (0,0,0), bounds=bnds, constraints=cons)
res
```




         fun: -0.15415019762845847
         jac: array([-1., -1., -1.])
     message: 'Optimization terminated successfully.'
        nfev: 10
         nit: 2
        njev: 2
      status: 0
     success: True
           x: array([0.08695652, 0.03557312, 0.03162055])



又或者可以用pulp库：使用pulp库之前，需要在cmd中输入如下的安装语句：
pip install pulp


```python
from pulp import *
```


```python
my_LpProblem = LpProblem("test1",LpMinimize)
x = LpVariable("x",0)
y = LpVariable("y",0)
z = LpVariable("z",0)

my_LpProblem += -x-y-z, "obj"
my_LpProblem += 7*x + 3*y + 9*z <= 1, "c1"
my_LpProblem +=  8*x + 5*y + 4*z <= 1, "c2"
my_LpProblem += 6*x + 9*y +5*z <= 1, "c3"

my_LpProblem.solve()

print("Status:",LpStatus[my_LpProblem.status])

for v in my_LpProblem.variables():
    print(v.name,"=", v.varValue)
```

    Status: Optimal
    x = 0.086956522
    y = 0.035573123
    z = 0.031620553


总结：optimize库提供的linprog可以用于简单的线性规划问题，pulp库可以应用于较为复杂的线性规划问题，
pulp库的使用更加直观。optimize库也提供了minimize函数，既可以用于线性规划，也可以用于非线性规划，
以下面的例子来说明minimize在非线性最优化的应用。


```python

```


```python

```


```python

```


```python

```


```python

```


```python

```

ff81633520bcb89cb560bb8caacdf64e0e23110a
ipynb
Jupyter Notebook
Optimization_python.ipynb
canbulajiji/Supply-Chain-Optimization
e7211aa68b061e2d31e5f4f2bda6c28a7117c758
Apache-2.0
Optimization_python.ipynb
canbulajiji/Supply-Chain-Optimization
e7211aa68b061e2d31e5f4f2bda6c28a7117c758
Apache-2.0
Optimization_python.ipynb
canbulajiji/Supply-Chain-Optimization
e7211aa68b061e2d31e5f4f2bda6c28a7117c758
Apache-2.0
2022-03-01T10:57:25.000Z
2022-03-01T10:57:25.000Z
Qwen/Qwen-72B
1. YES
2. YES