Skip to content
Merged
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 lectures/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ parts:
- file: short_path
- file: scalar_dynam
- file: linear_equations
- file: lp_intro
- file: lln_clt
- file: markov_chains
- caption: Models
Expand Down
8 changes: 4 additions & 4 deletions lectures/cobweb.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ TODO check / fix exercises
## Exercises

```{exercise-start}
:label: ex1
:label: cobweb_ex1
```
Using the default Market model and naive expectations, plot a time series simulation of supply (rather than the price).

Expand All @@ -457,7 +457,7 @@ Show, in particular, that supply also cycles.
```{exercise-end}
```

```{solution-start} ex1
```{solution-start} cobweb_ex1
:class: dropdown
```

Expand Down Expand Up @@ -498,7 +498,7 @@ ts_plot_supply(m, 5, 15)
```

```{exercise-start}
:label: ex2
:label: cobweb_ex2
```
**Backward looking average expectations**

Expand All @@ -519,7 +519,7 @@ Simulate and plot the price dynamics for $\alpha \in \{0.1, 0.3, 0.5, 0.8\}$ whe
```{exercise-end}
```

```{solution-start} ex2
```{solution-start} cobweb_ex2
:class: dropdown
```

Expand Down
44 changes: 20 additions & 24 deletions in-work/lp_intro.md → lectures/lp_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ jupytext:
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.4
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---

(lp_intro)=

In this lecture, we will need the following library. Install [ortools](https://developers.google.com/optimization) using `pip`.

```{code-cell} ipython3
---
tags: [hide-output]
---
!pip install ortools
```

Expand Down Expand Up @@ -53,7 +56,7 @@ from matplotlib.patches import Polygon

Let's start with some examples of linear programming problem.

+++


## Example 1: Production Problem

Expand Down Expand Up @@ -141,13 +144,13 @@ The intersection of the feasible set and the highest orange line delineates the

In this example, the optimal set is the point $(2.5, 5)$.

+++


### Computation: Using OR-Tools

Let's try to solve the same problem using the package *ortools.linear_solver*

+++


The following cell instantiates a solver and creates two variables specifying the range of values that they can have.

Expand Down Expand Up @@ -276,7 +279,7 @@ $$
\end{aligned}
$$

+++


### Computation: Using OR-Tools

Expand Down Expand Up @@ -348,7 +351,7 @@ OR-Tools tells us that the best investment strategy is:

4. At the end of the third year, the mutual fund will get payouts from the annuity and corporate bond and repay its loan from the bank. At the end it will own $ \$141018.24 $, so that it's total net rate of return over the three periods is $ 41.02\%$.

+++


## Standard Form

Expand Down Expand Up @@ -439,7 +442,7 @@ $$
\end{aligned}
$$

+++


### Computation: Using SciPy

Expand Down Expand Up @@ -475,7 +478,7 @@ Once we solve the problem, we can check whether the solver was successful in sol
```{code-cell} ipython3
# Solve the problem
# we put a negative sign on the objective as linprog does minimization
res_ex1 = linprog(-c_ex1, A_ub=A_ex1, b_ub=b_ex1, method='revised simplex')
res_ex1 = linprog(-c_ex1, A_ub=A_ex1, b_ub=b_ex1)

if res_ex1.success:
# We use negative sign to get the optimal value (maximized value)
Expand All @@ -501,7 +504,7 @@ See the [official documentation](https://docs.scipy.org/doc/scipy/reference/gene
This problem is to maximize the objective, so that we need to put a minus sign in front of parameter vector c.
```

+++


### Example 2: Investment Problem

Expand Down Expand Up @@ -567,7 +570,7 @@ Let's solve the problem and check the status using `success` attribute.
```{code-cell} ipython3
# Solve the problem
res_ex2 = linprog(-c_ex2, A_eq=A_ex2, b_eq=b_ex2,
bounds=bounds_ex2, method='revised simplex')
bounds=bounds_ex2)

if res_ex2.success:
# We use negative sign to get the optimal value (maximized value)
Expand All @@ -592,29 +595,27 @@ SciPy tells us that the best investment strategy is:

4. At the end of the third year, the mutual fund will get payouts from the annuity and corporate bond and repay its loan from the bank. At the end it will own $ \$141018.24 $, so that it's total net rate of return over the three periods is $ 41.02\% $.

+++


```{note}
You might notice the difference in the values of optimal solution using OR-Tools and SciPy but the optimal value is the same. It is because there can be many optimal solutions for the same problem.
```

+++


## Exercises

```{exercise-start}
:label: ex1
:label: lp_intro_ex1
```

### Exercise 1
Implement a new extended solution for the Problem 1 where in the factory owner decides that number of units of Product 1 should not be less than the number of units of Product 2.

```{exercise-end}
```

+++

```{solution-start} ex1
```{solution-start} lp_intro_ex1
:class: dropdown
```

Expand Down Expand Up @@ -673,10 +674,8 @@ else:
```

```{exercise-start}
:label: ex2
:label: lp_intro_ex2
```
### Exercise 2


A carpenter manufactures $2$ products - $A$ and $B$.

Expand All @@ -692,11 +691,8 @@ Find the number of units of $A$ and product $B$ that he should manufacture in or
```{exercise-end}
```

+++

Solution:

```{solution-start} ex1
```{solution-start} lp_intro_ex2
:class: dropdown
```

Expand Down