# Linear Programming

## Introduction
* Widely used
* Used to represent many practical problems
* Elements
    * A linear objective function
    * Linear (in)equalities
    

## The standard Form
\begin{align}
\text{minimize}\  & f(x) \\
\text{subject to } & \\
& a_1x &&\geq b_1 \\
& a_2x + c && \geq b_2 \\
& x &&\geq 0
\end{align}



# Gurobi Basics: Linear Model
## Mathematical Model
\begin{align}
\text{minimize}\  & 5x + 4y \\
\text{subject to } & \\
& \ \ x+\ \ y &&\geq \ \ 8 \\
& 2x + \ \ y &&\geq 10 \\
& \ \ x + 4y &&\geq 11 \\
& \ \ x &&\geq \ \ 0 \\
& \ \ \ \ \ \ \ \ \ \ \ y &&\geq \ \ 0
\end{align}

## Graphical representation of the problem



```python
from IPython.display import Image
from IPython.display import display
graphs = ['fr', 'fr_o1', 'fr_o3']
for g in graphs:
    display(Image(filename = g+'.png'))
```

# Code in Python using gurobipy
## Step 1: Importing gurobipy package


```python
from gurobipy import *
```

## Step 2: Create an optimization model
Model constructor. Initially, no variables or constraints.
``` python
Model(name = '')
```


```python
opt_mod = Model(name = "linear program")
```

## Step 3: Add decision variables
Add a decision variable to a model.
``` python

Model.addVar(lb = 0.0,               #(optional) lower bound
             ub = float('inf'),      #(optional) upper bound
             obj = 0.0,              #(optional) objective coefficient
             vtype = GRB.CONTINUOUS, #(optional) variable type
             name = "")              #(optional) name
             
```


```python
x = opt_mod.addVar(name = 'x', vtype = GRB.CONTINUOUS, lb = 0)
y = opt_mod.addVar(name = 'y', vtype = GRB.CONTINUOUS, lb = 0)
```

## Step 4: Define the objective function
Set the model objective equal to a expression
``` python

Model.setObjective(expr,            #New objective expression 
                   sense = None)    #GRB.MINIMIZE for minimization, 
                                    #GRB.MAXIMIZE for maximization
```


```python
obj_fn = 5*x + 4*y
opt_mod.setObjective(obj_fn, GRB.MINIMIZE)
```

## Step 5: Add the constraints
Add a constraint to a model. 
```python
Model.addConstr(constr,  # constraint object 
                name="") # name of the constraint
```


```python
c1 = opt_mod.addConstr(  x +   y >=  8, name = 'c1')
c2 = opt_mod.addConstr(2*x +   y >= 10, name = 'c2')
c3 = opt_mod.addConstr(  x + 4*y >= 11, name = 'c3')
```

## Step 6: Solve the model

``` python
Model.optimize() # optimize the model

Model.write(filename) # write model to a file
```


```python
opt_mod.optimize() # solve the model
opt_mod.write("linear_model.lp") # output the LP file of the model
```

## Step 7: Output the result


```python
print('Objective Function Value: %f' % opt_mod.objVal)
# Get values of the decision variables
for v in opt_mod.getVars():
    print('%s: %g' % (v.varName, v.x))
```

e63554b9421e43f7a6ea8052d54aecdbc4881de6
ipynb
Jupyter Notebook
mathematicalProgramming/Video02/video_02.ipynb
codingperspective/videoMaterials
8c9665466d8912c6f0c701c25ad9eb4802fb73a3
CC0-1.0
mathematicalProgramming/Video02/video_02.ipynb
codingperspective/videoMaterials
8c9665466d8912c6f0c701c25ad9eb4802fb73a3
CC0-1.0
mathematicalProgramming/Video02/video_02.ipynb
codingperspective/videoMaterials
8c9665466d8912c6f0c701c25ad9eb4802fb73a3
CC0-1.0
2021-11-21T05:02:50.000Z
2022-02-17T04:44:57.000Z
Qwen/Qwen-72B
1. YES
2. YES