Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diet model notebook #88

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/requirements-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ipykernel>=6.15
jupytext>=1.14
matplotlib>=3.5
nbconvert>=6.5
openpyxl>=3.0
scikit-learn>=1.0
seaborn==0.13.2
gurobipy>=10.0
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ myst-parser==0.19.2
nbconvert==7.2.5
nbsphinx==0.8.9
numpydoc==1.6.0
openpyxl==3.1.5
scikit-learn==1.5.0
seaborn==0.13.2
sphinx-copybutton==0.5.2
Expand Down
1 change: 1 addition & 0 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ the notebooks.
.. toctree::
:maxdepth: 1

examples/diet
examples/projects
examples/regression
examples/unit-commitment
Expand Down
Binary file added docs/source/examples/data/diet.xlsx
Binary file not shown.
86 changes: 86 additions & 0 deletions docs/source/examples/diet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
jupytext:
formats: ipynb,md:myst
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.1
---

# The Stigler Diet Problem

```{code-cell}
import pandas as pd
import gurobipy as gp
import gurobipy_pandas as gppd
from gurobipy import GRB

gppd.set_interactive()
```

```{code-cell}
foods = pd.read_excel("data/diet.xlsx", sheet_name="Foods")
foods.head()
```

```{code-cell}
categories = pd.read_excel("data/diet.xlsx", sheet_name="Categories")
categories.head()
```

```{code-cell}
nutrition = pd.melt(
pd.read_excel("data/diet.xlsx", sheet_name="Nutrition"),
id_vars=["food"],
var_name="nutrient",
)
nutrition.head()
```

```{code-cell}
env = gp.Env()
model = gp.Model(env=env)
```

```{code-cell}
buy = gppd.add_vars(model, foods.set_index("food"), name="buy")
buy.head()
```

```{code-cell}
amounts = (
nutrition.join(buy, on="food")
.set_index(["food", "nutrient"])
.pipe(lambda df: df["value"] * df["buy"])
.groupby("nutrient").sum()
)

limits = categories.set_index("category")

gppd.add_constrs(
model,
amounts,
GRB.GREATER_EQUAL,
limits["minimum"],
)

gppd.add_constrs(
model,
amounts[limits["maximum"].notnull()],
GRB.LESS_EQUAL,
limits["maximum"].dropna(),
)
```

```{code-cell}
model.setObjective((buy * foods.set_index("food")["cost"]).sum())
```

```{code-cell}
model.optimize()
```

```{code-cell}
buy.gppd.X.pipe(lambda s: s[s >0])
```