From df791bed2da4bfdd69c495dbb40eb0ff7fa3f21d Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 7 Feb 2023 10:51:15 +1100 Subject: [PATCH 1/4] Add lp_intro in lectures --- lectures/_toc.yml | 1 + {in-work => lectures}/lp_intro.md | 26 ++++++++++---------------- 2 files changed, 11 insertions(+), 16 deletions(-) rename {in-work => lectures}/lp_intro.md (99%) diff --git a/lectures/_toc.yml b/lectures/_toc.yml index 051df6570..0279605e7 100644 --- a/lectures/_toc.yml +++ b/lectures/_toc.yml @@ -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 diff --git a/in-work/lp_intro.md b/lectures/lp_intro.md similarity index 99% rename from in-work/lp_intro.md rename to lectures/lp_intro.md index 11e2a9938..71878aaeb 100644 --- a/in-work/lp_intro.md +++ b/lectures/lp_intro.md @@ -3,8 +3,6 @@ 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 @@ -53,7 +51,7 @@ from matplotlib.patches import Polygon Let's start with some examples of linear programming problem. -+++ + ## Example 1: Production Problem @@ -141,13 +139,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. @@ -276,7 +274,7 @@ $$ \end{aligned} $$ -+++ + ### Computation: Using OR-Tools @@ -348,7 +346,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 @@ -439,7 +437,7 @@ $$ \end{aligned} $$ -+++ + ### Computation: Using SciPy @@ -501,7 +499,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 @@ -592,13 +590,13 @@ 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 @@ -606,13 +604,11 @@ You might notice the difference in the values of optimal solution using OR-Tools :label: 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 :class: dropdown @@ -675,8 +671,6 @@ else: ```{exercise-start} :label: ex2 ``` -### Exercise 2 - A carpenter manufactures $2$ products - $A$ and $B$. @@ -692,7 +686,7 @@ Find the number of units of $A$ and product $B$ that he should manufacture in or ```{exercise-end} ``` -+++ + Solution: From 8543d141141133d0f748fd1a9feea19223c42152 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 7 Feb 2023 11:00:39 +1100 Subject: [PATCH 2/4] fixes --- lectures/lp_intro.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lectures/lp_intro.md b/lectures/lp_intro.md index 71878aaeb..c6162b80a 100644 --- a/lectures/lp_intro.md +++ b/lectures/lp_intro.md @@ -9,6 +9,8 @@ kernelspec: 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 @@ -601,7 +603,7 @@ You might notice the difference in the values of optimal solution using OR-Tools ## Exercises ```{exercise-start} -:label: ex1 +:label: lp_intro_ex1 ``` 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. @@ -610,7 +612,7 @@ Implement a new extended solution for the Problem 1 where in the factory owner d ``` -```{solution-start} ex1 +```{solution-start} lp_intro_ex1 :class: dropdown ``` @@ -669,7 +671,7 @@ else: ``` ```{exercise-start} -:label: ex2 +:label: lp_intro_ex2 ``` A carpenter manufactures $2$ products - $A$ and $B$. @@ -687,10 +689,7 @@ Find the number of units of $A$ and product $B$ that he should manufacture in or ``` - -Solution: - -```{solution-start} ex1 +```{solution-start} lp_intro_ex1 :class: dropdown ``` From ad63d611079f1d32f37c70c271f57d5453ff5a4d Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 7 Feb 2023 11:05:33 +1100 Subject: [PATCH 3/4] hide output --- lectures/lp_intro.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lectures/lp_intro.md b/lectures/lp_intro.md index c6162b80a..d41831261 100644 --- a/lectures/lp_intro.md +++ b/lectures/lp_intro.md @@ -14,6 +14,9 @@ kernelspec: 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 ``` From 705f38c14d94e9c6052eb322436fc44c6994970f Mon Sep 17 00:00:00 2001 From: Smit-create Date: Tue, 7 Feb 2023 11:10:29 +1100 Subject: [PATCH 4/4] remove warnings --- lectures/cobweb.md | 8 ++++---- lectures/lp_intro.md | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lectures/cobweb.md b/lectures/cobweb.md index 9fdef72f6..37a2637c4 100644 --- a/lectures/cobweb.md +++ b/lectures/cobweb.md @@ -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). @@ -457,7 +457,7 @@ Show, in particular, that supply also cycles. ```{exercise-end} ``` -```{solution-start} ex1 +```{solution-start} cobweb_ex1 :class: dropdown ``` @@ -498,7 +498,7 @@ ts_plot_supply(m, 5, 15) ``` ```{exercise-start} -:label: ex2 +:label: cobweb_ex2 ``` **Backward looking average expectations** @@ -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 ``` diff --git a/lectures/lp_intro.md b/lectures/lp_intro.md index d41831261..cf151af56 100644 --- a/lectures/lp_intro.md +++ b/lectures/lp_intro.md @@ -478,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) @@ -570,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) @@ -692,7 +692,7 @@ Find the number of units of $A$ and product $B$ that he should manufacture in or ``` -```{solution-start} lp_intro_ex1 +```{solution-start} lp_intro_ex2 :class: dropdown ```