# Pyomo - Getting started

Pyomo installation: see http://www.pyomo.org/installation

```
pip install pyomo
```

See also these excellent tutorials that details how to install Pyomo and several solvers (GLPK, COIN-OR CBC, COIN-OR Ipopt, ...):
- https://nbviewer.jupyter.org/github/jckantor/ND-Pyomo-Cookbook/blob/master/notebooks/01.01-Installing-Pyomo.ipynb
- https://nbviewer.jupyter.org/github/jckantor/ND-Pyomo-Cookbook/blob/master/notebooks/01.02-Running-Pyomo-on-Google-Colab.ipynb
- https://nbviewer.jupyter.org/github/jckantor/ND-Pyomo-Cookbook/blob/master/notebooks/01.04-Cross-Platform-Installation-of-Pyomo-and-Solvers.ipynb


```python
from pyomo.environ import *
```

## Example 1


```python
model = ConcreteModel(name="Getting started")

model.x = Var(bounds=(-10, 10))

model.obj = Objective(expr=model.x)

model.const_1 = Constraint(expr=model.x >= 5)

# @tail:
opt = SolverFactory('glpk')  # "glpk" or "cbc"

res = opt.solve(model)    # solves and updates instance

model.display()

print()
print("Optimal solution: ", value(model.x))
print("Cost of the optimal solution: ", value(model.obj))
# @:tail
```

## Example 2

$$
\begin{align}
    \max_{x_1,x_2} & \quad 4 x_1 + 3 x_2 \\
    \text{s.t.}    & \quad   x_1 +   x_2 \leq 100 \\
                   & \quad 2 x_1 +   x_2 \leq 150 \\
                   & \quad 3 x_1 + 4 x_2 \leq 360 \\
                   & \quad x_1, x_2 \geq 0
\end{align}
$$

```
Optimal total cost is:  350.0

x_1 = 50.
x_2 = 50.
```


```python
model = ConcreteModel(name="Getting started")

model.x1 = Var(within=NonNegativeReals)
model.x2 = Var(within=NonNegativeReals)

model.obj = Objective(expr=4. * model.x1 + 3. * model.x2, sense=maximize)

model.ineq_const_1 = Constraint(expr=model.x1 + model.x2 <= 100)
model.ineq_const_2 = Constraint(expr=2. * model.x1 + model.x2 <= 150)
model.ineq_const_3 = Constraint(expr=3. * model.x1 + 4. * model.x2 <= 360)

# @tail:
opt = SolverFactory('glpk')  # "glpk" or "cbc"

results = opt.solve(model)    # solves and updates instance

model.display()

print()
print("Optimal solution: ({}, {})".format(value(model.x1), value(model.x2)))
print("Gain of the optimal solution: ", value(model.obj))
# @:tail
```

e5086a43cf1ca59397f82e4adc1f1099d94b012d
ipynb
Jupyter Notebook
nb_dev_python/python_pyomo_getting_started_1.ipynb
jdhp-docs/python-notebooks
91a97ea5cf374337efa7409e4992ea3f26b99179
MIT
2017-05-03T12:23:36.000Z
2020-10-26T17:30:56.000Z
nb_dev_python/python_pyomo_getting_started_1.ipynb
jdhp-docs/python-notebooks
91a97ea5cf374337efa7409e4992ea3f26b99179
MIT
nb_dev_python/python_pyomo_getting_started_1.ipynb
jdhp-docs/python-notebooks
91a97ea5cf374337efa7409e4992ea3f26b99179
MIT
2020-10-26T17:30:57.000Z
2020-10-26T17:30:57.000Z
Qwen/Qwen-72B
1. YES
2. YES