You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lectures/lp_intro.md
+20-24Lines changed: 20 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,20 @@ jupytext:
3
3
text_representation:
4
4
extension: .md
5
5
format_name: myst
6
-
format_version: 0.13
7
-
jupytext_version: 1.14.4
8
6
kernelspec:
9
7
display_name: Python 3 (ipykernel)
10
8
language: python
11
9
name: python3
12
10
---
13
11
12
+
(lp_intro)=
13
+
14
14
In this lecture, we will need the following library. Install [ortools](https://developers.google.com/optimization) using `pip`.
15
15
16
16
```{code-cell} ipython3
17
+
---
18
+
tags: [hide-output]
19
+
---
17
20
!pip install ortools
18
21
```
19
22
@@ -53,7 +56,7 @@ from matplotlib.patches import Polygon
53
56
54
57
Let's start with some examples of linear programming problem.
55
58
56
-
+++
59
+
57
60
58
61
## Example 1: Production Problem
59
62
@@ -141,13 +144,13 @@ The intersection of the feasible set and the highest orange line delineates the
141
144
142
145
In this example, the optimal set is the point $(2.5, 5)$.
143
146
144
-
+++
147
+
145
148
146
149
### Computation: Using OR-Tools
147
150
148
151
Let's try to solve the same problem using the package *ortools.linear_solver*
149
152
150
-
+++
153
+
151
154
152
155
The following cell instantiates a solver and creates two variables specifying the range of values that they can have.
153
156
@@ -276,7 +279,7 @@ $$
276
279
\end{aligned}
277
280
$$
278
281
279
-
+++
282
+
280
283
281
284
### Computation: Using OR-Tools
282
285
@@ -348,7 +351,7 @@ OR-Tools tells us that the best investment strategy is:
348
351
349
352
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\%$.
350
353
351
-
+++
354
+
352
355
353
356
## Standard Form
354
357
@@ -439,7 +442,7 @@ $$
439
442
\end{aligned}
440
443
$$
441
444
442
-
+++
445
+
443
446
444
447
### Computation: Using SciPy
445
448
@@ -475,7 +478,7 @@ Once we solve the problem, we can check whether the solver was successful in sol
475
478
```{code-cell} ipython3
476
479
# Solve the problem
477
480
# we put a negative sign on the objective as linprog does minimization
# 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
501
504
This problem is to maximize the objective, so that we need to put a minus sign in front of parameter vector c.
502
505
```
503
506
504
-
+++
507
+
505
508
506
509
### Example 2: Investment Problem
507
510
@@ -567,7 +570,7 @@ Let's solve the problem and check the status using `success` attribute.
567
570
```{code-cell} ipython3
568
571
# Solve the problem
569
572
res_ex2 = linprog(-c_ex2, A_eq=A_ex2, b_eq=b_ex2,
570
-
bounds=bounds_ex2, method='revised simplex')
573
+
bounds=bounds_ex2)
571
574
572
575
if res_ex2.success:
573
576
# We use negative sign to get the optimal value (maximized value)
@@ -592,29 +595,27 @@ SciPy tells us that the best investment strategy is:
592
595
593
596
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\% $.
594
597
595
-
+++
598
+
596
599
597
600
```{note}
598
601
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.
599
602
```
600
603
601
-
+++
604
+
602
605
603
606
## Exercises
604
607
605
608
```{exercise-start}
606
-
:label: ex1
609
+
:label: lp_intro_ex1
607
610
```
608
611
609
-
### Exercise 1
610
612
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.
611
613
612
614
```{exercise-end}
613
615
```
614
616
615
-
+++
616
617
617
-
```{solution-start} ex1
618
+
```{solution-start} lp_intro_ex1
618
619
:class: dropdown
619
620
```
620
621
@@ -673,10 +674,8 @@ else:
673
674
```
674
675
675
676
```{exercise-start}
676
-
:label: ex2
677
+
:label: lp_intro_ex2
677
678
```
678
-
### Exercise 2
679
-
680
679
681
680
A carpenter manufactures $2$ products - $A$ and $B$.
682
681
@@ -692,11 +691,8 @@ Find the number of units of $A$ and product $B$ that he should manufacture in or
0 commit comments