# Linear Programming in Pyomo

Pyomo is an example of a recent generation of modeling languages that are fully integrated with an underlying scripting language. 

The purpose of this tutorial is to introduce the basics of constructing Pyomo models. In this first example we consider the production of two products, 'A' and 'B', subject to constraints. The objective is to maximize profit

\begin{align}
\max_{x,y} \mbox{profit} =  40 x + 30 y
\end{align}
subject to
\begin{align}
x & \leq 40 \\
x + 2y & \leq 80 \\
x + y & \leq 100
\end{align}

where $x$ refers the production of 'A', and $y$ to the production of 'B'.

## Starting the Model

At the highest level, Pyomo employs two objects -- a model and a solver -- to generate a resulting solution. 


```python
from pyomo.environ import *

model = ConcreteModel()

solver = SolverFactory('glpk')
```


```python
from pyomo.environ import *

# create a model
model = ConcreteModel()

# index for x
idx = ['Product A', 'Product B']

# create decision variables
model.x = Var(idx, domain=NonNegativeIntegers)

# create objective
model.maxprofit = Objective(expr = 40*model.x['Product A'] + 30*model.x['Product B'], sense=maximize)

# create constraints
model.constraints = ConstraintList()
model.constraints.add(model.x['Product A'] <= 40)
model.constraints.add(model.x['Product A'] + model.x['Product B'] <= 80.5)
model.constraints.add(model.x['Product A'] + model.x['Product B'] <= 100)

solver = SolverFactory('glpk')

results = solver.solve(model)

for i in idx:
    print(i, model.x[i]())
```

    Product A 40.0
    Product B 40.0



```python

```


```python
data = {'penny':1, 'nickel':5, 'dime':10, 'quarter':25, 'half-dollar':50}
data
```




    {'penny': 1, 'nickel': 5, 'dime': 10, 'quarter': 25, 'half-dollar': 50}




```python
data.keys()
```




    dict_keys(['penny', 'nickel', 'dime', 'quarter', 'half-dollar'])




```python
data.values()
```




    dict_values([1, 5, 10, 25, 50])




```python
model = ConcreteModel()

model.x = Var(data.keys(), domain=NonNegativeIntegers)

model.ncoins = Objective(expr = sum(model.x[c] for c in data.keys()), sense=minimize)

model.cons = Constraint(expr = 83 == sum(data[c]*model.x[c] for c in data.keys()))

solver = SolverFactory('glpk')
solver.solve(model)
model.pprint()

```

    1 Set Declarations
        x_index : Size=1, Index=None, Ordered=False
            Key  : Dimen : Domain : Size : Members
            None :     1 :    Any :    5 : {'dime', 'half-dollar', 'nickel', 'penny', 'quarter'}
    
    1 Var Declarations
        x : Size=5, Index=x_index
            Key         : Lower : Value : Upper : Fixed : Stale : Domain
                   dime :     0 :   0.0 :  None : False : False : NonNegativeIntegers
            half-dollar :     0 :   1.0 :  None : False : False : NonNegativeIntegers
                 nickel :     0 :   1.0 :  None : False : False : NonNegativeIntegers
                  penny :     0 :   3.0 :  None : False : False : NonNegativeIntegers
                quarter :     0 :   1.0 :  None : False : False : NonNegativeIntegers
    
    1 Objective Declarations
        ncoins : Size=1, Index=None, Active=True
            Key  : Active : Sense    : Expression
            None :   True : minimize : x[penny] + x[nickel] + x[dime] + x[quarter] + x[half-dollar]
    
    1 Constraint Declarations
        cons : Size=1, Index=None, Active=True
            Key  : Lower : Body                                                                    : Upper : Active
            None :  83.0 : x[penny] + 5*x[nickel] + 10*x[dime] + 25*x[quarter] + 50*x[half-dollar] :  83.0 :   True
    
    4 Declarations: x_index x ncoins cons



```python

```


```python

```

02f913b96519d416b0b399bbd9ad85649c5ac023
ipynb
Jupyter Notebook
Mathematics/Mathematical Modeling/06.05-Linear-Programming-in-Pyomo.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
Mathematics/Mathematical Modeling/06.05-Linear-Programming-in-Pyomo.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
Mathematics/Mathematical Modeling/06.05-Linear-Programming-in-Pyomo.ipynb
okara83/Becoming-a-Data-Scientist
f09a15f7f239b96b77a2f080c403b2f3e95c9650
MIT
2022-02-09T15:41:33.000Z
2022-02-11T07:47:40.000Z
Qwen/Qwen-72B
1. YES
2. YES