Skip to content

Commit 8b6f9e4

Browse files
authored
LECT: LP Intro (#61)
* Add lp_intro in lectures * fixes * hide output * remove warnings
1 parent c803c72 commit 8b6f9e4

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

lectures/_toc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ parts:
1313
- file: short_path
1414
- file: scalar_dynam
1515
- file: linear_equations
16+
- file: lp_intro
1617
- file: lln_clt
1718
- file: markov_chains
1819
- caption: Models

lectures/cobweb.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ TODO check / fix exercises
448448
## Exercises
449449

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

@@ -457,7 +457,7 @@ Show, in particular, that supply also cycles.
457457
```{exercise-end}
458458
```
459459

460-
```{solution-start} ex1
460+
```{solution-start} cobweb_ex1
461461
:class: dropdown
462462
```
463463

@@ -498,7 +498,7 @@ ts_plot_supply(m, 5, 15)
498498
```
499499

500500
```{exercise-start}
501-
:label: ex2
501+
:label: cobweb_ex2
502502
```
503503
**Backward looking average expectations**
504504

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

522-
```{solution-start} ex2
522+
```{solution-start} cobweb_ex2
523523
:class: dropdown
524524
```
525525

in-work/lp_intro.md renamed to lectures/lp_intro.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ jupytext:
33
text_representation:
44
extension: .md
55
format_name: myst
6-
format_version: 0.13
7-
jupytext_version: 1.14.4
86
kernelspec:
97
display_name: Python 3 (ipykernel)
108
language: python
119
name: python3
1210
---
1311

12+
(lp_intro)=
13+
1414
In this lecture, we will need the following library. Install [ortools](https://developers.google.com/optimization) using `pip`.
1515

1616
```{code-cell} ipython3
17+
---
18+
tags: [hide-output]
19+
---
1720
!pip install ortools
1821
```
1922

@@ -53,7 +56,7 @@ from matplotlib.patches import Polygon
5356

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

56-
+++
59+
5760

5861
## Example 1: Production Problem
5962

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

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

144-
+++
147+
145148

146149
### Computation: Using OR-Tools
147150

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

150-
+++
153+
151154

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

@@ -276,7 +279,7 @@ $$
276279
\end{aligned}
277280
$$
278281

279-
+++
282+
280283

281284
### Computation: Using OR-Tools
282285

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

349352
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\%$.
350353

351-
+++
354+
352355

353356
## Standard Form
354357

@@ -439,7 +442,7 @@ $$
439442
\end{aligned}
440443
$$
441444
442-
+++
445+
443446
444447
### Computation: Using SciPy
445448
@@ -475,7 +478,7 @@ Once we solve the problem, we can check whether the solver was successful in sol
475478
```{code-cell} ipython3
476479
# Solve the problem
477480
# we put a negative sign on the objective as linprog does minimization
478-
res_ex1 = linprog(-c_ex1, A_ub=A_ex1, b_ub=b_ex1, method='revised simplex')
481+
res_ex1 = linprog(-c_ex1, A_ub=A_ex1, b_ub=b_ex1)
479482
480483
if res_ex1.success:
481484
# We use negative sign to get the optimal value (maximized value)
@@ -501,7 +504,7 @@ See the [official documentation](https://docs.scipy.org/doc/scipy/reference/gene
501504
This problem is to maximize the objective, so that we need to put a minus sign in front of parameter vector c.
502505
```
503506
504-
+++
507+
505508
506509
### Example 2: Investment Problem
507510
@@ -567,7 +570,7 @@ Let's solve the problem and check the status using `success` attribute.
567570
```{code-cell} ipython3
568571
# Solve the problem
569572
res_ex2 = linprog(-c_ex2, A_eq=A_ex2, b_eq=b_ex2,
570-
bounds=bounds_ex2, method='revised simplex')
573+
bounds=bounds_ex2)
571574
572575
if res_ex2.success:
573576
# We use negative sign to get the optimal value (maximized value)
@@ -592,29 +595,27 @@ SciPy tells us that the best investment strategy is:
592595
593596
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\% $.
594597
595-
+++
598+
596599
597600
```{note}
598601
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.
599602
```
600603
601-
+++
604+
602605
603606
## Exercises
604607
605608
```{exercise-start}
606-
:label: ex1
609+
:label: lp_intro_ex1
607610
```
608611
609-
### Exercise 1
610612
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.
611613
612614
```{exercise-end}
613615
```
614616
615-
+++
616617
617-
```{solution-start} ex1
618+
```{solution-start} lp_intro_ex1
618619
:class: dropdown
619620
```
620621
@@ -673,10 +674,8 @@ else:
673674
```
674675
675676
```{exercise-start}
676-
:label: ex2
677+
:label: lp_intro_ex2
677678
```
678-
### Exercise 2
679-
680679
681680
A carpenter manufactures $2$ products - $A$ and $B$.
682681
@@ -692,11 +691,8 @@ Find the number of units of $A$ and product $B$ that he should manufacture in or
692691
```{exercise-end}
693692
```
694693
695-
+++
696-
697-
Solution:
698694
699-
```{solution-start} ex1
695+
```{solution-start} lp_intro_ex2
700696
:class: dropdown
701697
```
702698

0 commit comments

Comments
 (0)