Practice Exam
Practice Exam¶
OPIM 5641: Business Decision Modeling – University of Connecticut
Copyright By PowCoder代写 加微信 powcoder
Instructor:
Questions adapted from Chapter 2 of Nagraj et al.
Please add detailed comments to all code so that I can follow your solution. If I cannot understand your code then you will not receive full credit.
Your Name Here
Your NetID Here
%matplotlib inline
from pylab import * # for graphing
import shutil
import sys
import os.path
if not shutil.which(“pyomo”):
!pip install -q pyomo
assert(shutil.which(“pyomo”))
if not (shutil.which(“cbc”) or os.path.isfile(“cbc”)):
if “google.colab” in sys.modules:
!apt-get install -y -qq coinor-cbc
!conda install -c conda-forge coincbc
assert(shutil.which(“cbc”) or os.path.isfile(“cbc”))
from pyomo.environ import *
import numpy as np
import pandas as pd
|████████████████████████████████| 9.6 MB 7.1 MB/s
|████████████████████████████████| 49 kB 5.6 MB/s
Selecting previously unselected package coinor-libcoinutils3v5.
(Reading database … 155320 files and directories currently installed.)
Preparing to unpack …/0-coinor-libcoinutils3v5_2.10.14+repack1-1_amd64.deb …
Unpacking coinor-libcoinutils3v5 (2.10.14+repack1-1) …
Selecting previously unselected package coinor-libosi1v5.
Preparing to unpack …/1-coinor-libosi1v5_0.107.9+repack1-1_amd64.deb …
Unpacking coinor-libosi1v5 (0.107.9+repack1-1) …
Selecting previously unselected package coinor-libclp1.
Preparing to unpack …/2-coinor-libclp1_1.16.11+repack1-1_amd64.deb …
Unpacking coinor-libclp1 (1.16.11+repack1-1) …
Selecting previously unselected package coinor-libcgl1.
Preparing to unpack …/3-coinor-libcgl1_0.59.10+repack1-1_amd64.deb …
Unpacking coinor-libcgl1 (0.59.10+repack1-1) …
Selecting previously unselected package coinor-libcbc3.
Preparing to unpack …/4-coinor-libcbc3_2.9.9+repack1-1_amd64.deb …
Unpacking coinor-libcbc3 (2.9.9+repack1-1) …
Selecting previously unselected package coinor-cbc.
Preparing to unpack …/5-coinor-cbc_2.9.9+repack1-1_amd64.deb …
Unpacking coinor-cbc (2.9.9+repack1-1) …
Setting up coinor-libcoinutils3v5 (2.10.14+repack1-1) …
Setting up coinor-libosi1v5 (0.107.9+repack1-1) …
Setting up coinor-libclp1 (1.16.11+repack1-1) …
Setting up coinor-libcgl1 (0.59.10+repack1-1) …
Setting up coinor-libcbc3 (2.9.9+repack1-1) …
Setting up coinor-cbc (2.9.9+repack1-1) …
Processing triggers for man-db (2.8.3-2ubuntu0.1) …
Processing triggers for libc-bin (2.27-3ubuntu1.3) …
/sbin/ldconfig.real: /usr/local/lib/python3.7/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link
Problem 1¶
Solve the following problem using the graphical method.
$Max(Z) = 2X + 4Y$
subject to:
$2X + 4Y \leq 72$
$3X + 6Y \geq 27$
$-3X + 10Y \geq 0$
$X,Y \geq 0$ (nonnegativity)
$-3X + 10Y \geq 0$
$-3X + 10Y = 0$
$X = 40$ –> $-3*40 + 10Y = 0$ –> $-120 + 10Y = 0$ –> $10Y = 120$ –> $Y = 12$
# this is the example from Pyomo cookbook
# pylab makes it easy to make plots
figure(figsize=(6,6))
#subplot(111, aspect=’equal’)
axis([0,40,0,40])
xlabel(‘X’)
ylabel(‘Y’)
# Constraint 1
y = array([18, 0])
x = array([0,36])
plot(x,y,’r’,lw=2)
fill_between([0,36,40], # x area
[18,0,0], # y area
[40,40,40], # upper area
color=’red’, # color
alpha=0.15) # transparency
# Constraint 2
y = array([27/6, 0])
x = array([0,27/3])
plot(x,y,’orange’,lw=2)
fill_between([0,9], # x area
[0,0], # y area
[27/6,0], # upper area
color=’orange’, # color
alpha=0.15) # transparency
# Constraint 3
y = array([0,12])
x = array([0,40])
plot(x,y,’black’,lw=2)
fill_between([0,40], # x area
[0,0], # y area
[0,12], # upper area
color=’black’, # color
alpha=0.15) # transparency
# legend([‘Constraint 1’])
# y axis and orange
A = np.array([[1, 0], [3, 6]])
B = np.array([0, 27])
point_1 = np.linalg.solve(A,B)
print(“Point 1: “, point_1)
# y axis and red
A = np.array([[1, 0], [2, 4]])
B = np.array([0, 72])
point_2 = np.linalg.solve(A,B)
print(“Point 2: “, point_2)
# black and red
A = np.array([[-3, 10], [2, 4]])
B = np.array([0, 72])
point_3 = np.linalg.solve(A,B)
print(“Point 3: “, point_3)
# black and orange
A = np.array([[-3, 10], [3, 6]])
B = np.array([0, 27])
point_4 = np.linalg.solve(A,B)
print(“Point 4: “, point_4)
Point 1: [0. 4.5]
Point 2: [ 0. 18.]
Point 3: [22.5 6.75]
Point 4: [5.625 1.6875]
print(2*point_1[0] + 4*point_1[1])
print(2*point_2[0] + 4*point_2[1])
print(2*point_3[0] + 4*point_3[1])
print(2*point_4[0] + 4*point_4[1])
Point 2: [ 0 , 18]
Is an optimal solution.
Can you prove to me algebraically that this is an optimal solution?
Problem 2¶
Formulate the dual of Problem 1. Can you find a feasible solution that proves that the solution from Problem 1 that you found is optimal?
Solve the following problem using the graphical method.
$Max(Z) = 2X + 4Y$
subject to:
$2X + 4Y \leq 72$
$3X + 6Y \geq 27$
$-3X + 10Y \geq 0$
$X,Y \geq 0$ (nonnegativity)
$2X + 4Y \leq 72$
$-3X – 6Y \leq -27$
$3X – 10Y \leq 0$
$X,Y \geq 0$ (nonnegativity)
This is the dual. I formulate this by having a new variable for each constraint, and the objective function becomes the right hand side of the equations mutliplied by the variables, and the constraints ensure that I find a combination that is better than the objective function.
$\min 72 z_1 – 27 z_2 + 0 z_3$
Subject to
$2 z_1 – 3 z_2 + 3 z_3 \geq 2$
$4 z_1 – 6 z_2 – 10 z_3 \geq 4$
$z_1, z_2, z_3 \geq 0$
The solution (1,0,0) is feasible. Now let me prove that it is optimal.
First off, what is the objective value in the dual for the solution (1,0,0)?
Plugging in to objective we get:
721−270+0*0 = 72
Therefore, the optimal value can be no more than 72. But how do I know that 72 is the best possible value? Let’s find a combination of the constraints that PROVES it.
($4 z_1 – 6 z_2 – 10 z_3 \geq 4$ ) * 18
$72 z_1 – 108 z_2 – 180 z_3 \geq 72$
It should therefore be clear that any feasible solution to the dual has to satisfy that
$72 z_1 – 108 z_2 – 180 z_3 \geq 72$
If I think about it:
$72 z_1 – 27 z_2 \geq 72 z_1 – 108 z_2 – 180 z_3 \geq 72$
I then know that:
$72 z_1 – 27 z_2 \geq 72$
Claim: z_1 = 1, z_2 = 0, z_3 = 0 is the optimal solution to the dual.
The solution is feasible to the dual, and, the value matches the value of the solution (0,18) to the primal.
A + C VERSUS B + D? NO IDEA
A + C VERSUS B + D? YES!!! greater than or equal
Problem 3¶
A small motor manufacturer makes two types of motor, models A and B. The assembly process for each is similar in that both require a certain amount of
wiring, drilling, and assembly. Each model A takes
3 hours of wiring, 2 hours of drilling, and 1.5 hours
of assembly. Each model B must go through 2 hours of wiring, 1 hour of drilling, and 0.5 hours of assembly. During the next production period, 240 hours of wiring time, 210 hours of drilling time, and 120
hours of assembly time are available. Each model A
sold yields a profit of \$22. Each model B can be sold
for a \$15 profit. Assuming that all motors that are
assembled can be sold, find the best combination of
motors to yield the highest profit.
Formulate the problem as a linear programming model and solve via the graphically method, and also using the brute force method.
$X$: number of type A models
$Y$: number of type B models
$22 \cdot X + 15 \cdot Y$
Constraints
wiring: $3X + 2Y \leq 240$
drilling: $2X + 1Y \leq 210$
assembly: $1.5X + 0.5Y \leq 120$
non-negativity: $X \geq 0, Y \geq 0$
best_profit_so_far = 0
best_x = 0
best_y = 0
for x in range(81):
for y in range(121):
if 3*x + 2*y > 240:
# drilling
if 2*x + 1*y > 210:
# assembly
if 1.5*x + 0.5*y > 120:
candidate_profit = 22*x + 15*y
if candidate_profit > best_profit_so_far:
best_profit_so_far = candidate_profit
best_x = x
best_y = y
print(best_x)
print(best_y)
print(best_profit_so_far)
for y in range(121):
Problem 4¶
The profit per each model is uncertain. Suppose that the profit for model A has a plus/minus 5\% margin, and that the profit for model B has a plus/minus 15\% margin. Produce a histogram that displays the distribution of profits for the best solution found in Problem 3.
generated_profits = []
n_simulations = 1000
for sim_index in range(n_simulations):
profit_from_x = np.random.uniform(1-0.05,1+0.05) * 22 * best_x
profit_from_y = np.random.uniform(1-0.15,1+0.15) * 15 * best_y
total_profit = profit_from_x + profit_from_y
generated_profits.append(total_profit)
plt.hist(generated_profits)
(array([100., 98., 93., 109., 90., 102., 105., 90., 112., 101.]),
array([1530.09218953, 1584.03642399, 1637.98065846, 1691.92489292,
1745.86912738, 1799.81336184, 1853.7575963 , 1907.70183077,
1961.64606523, 2015.59029969, 2069.53453415]),
)
best_profit_so_far = 0
best_x = 0
best_y = 0
n_simulations = 1000
for x in range(0,81,5):
for y in range(0,121,5):
if 3*x + 2*y > 240:
# drilling
if 2*x + 1*y > 210:
# assembly
if 1.5*x + 0.5*y > 120:
generated_profits = []
for sim_index in range(n_simulations):
profit_from_x = np.random.uniform(1-0.05,1+0.05) * 22 * x
profit_from_y = np.random.uniform(1-0.15,1+0.15) * 15 * y
total_profit = profit_from_x + profit_from_y
generated_profits.append(total_profit)
random_mean_profit = np.mean(generated_profits)
if random_mean_profit > best_profit_so_far:
best_profit_so_far = random_mean_profit
best_x = x
best_y = y
print(best_x)
print(best_y)
print(best_profit_so_far)
1801.4743106545693
generated_profits = []
n_simulations = 1000
for sim_index in range(n_simulations):
profit_from_x = np.random.uniform(1-0.05,1+0.05) * 22 * best_x
profit_from_y = np.random.uniform(1-0.15,1+0.15) * 15 * best_y
total_profit = profit_from_x + profit_from_y
generated_profits.append(total_profit)
np.mean(generated_profits)
1799.438439958911
np.var(generated_profits)
22730.208065337072
np.std(generated_profits)
150.76540738955032
generated_profits = []
n_simulations = 1000
for sim_index in range(n_simulations):
profit_from_x = np.random.uniform(1-0.05,1+0.05) * 22 * 80
profit_from_y = np.random.uniform(1-0.15,1+0.15) * 15 * 0
total_profit = profit_from_x + profit_from_y
generated_profits.append(total_profit)
np.mean(generated_profits)
1760.2823304523836
np.var(generated_profits)
2616.984938223384
np.std(generated_profits)
51.156475037119044
Problem 5¶
Suppose you are considering not just profit, but you also care about the variance in your profit. Find a solution that both maximizes expected profit but also minimizes variance. What are the possible solutions you should consider?
for variance_limit in np.arange(2700,50000,1000):
best_profit_so_far = 0
best_x = 0
best_y = 0
n_simulations = 1000
for x in range(0,81,5):
for y in range(0,121,5):
if 3*x + 2*y > 240:
# drilling
if 2*x + 1*y > 210:
# assembly
if 1.5*x + 0.5*y > 120:
generated_profits = []
for sim_index in range(n_simulations):
profit_from_x = np.random.uniform(1-0.05,1+0.05) * 22 * x
profit_from_y = np.random.uniform(1-0.15,1+0.15) * 15 * y
total_profit = profit_from_x + profit_from_y
generated_profits.append(total_profit)
random_var_profit = np.var(generated_profits)
if random_var_profit > variance_limit:
random_mean_profit = np.mean(generated_profits)
if random_mean_profit > best_profit_so_far:
best_profit_so_far = random_mean_profit
best_x = x
best_y = y
print(best_x, best_y, round(best_profit_so_far,2), variance_limit)
70 15 1764.81 2700
60 30 1769.5 3700
50 45 1775.2 4700
50 45 1772.41 5700
40 60 1780.91 6700
40 60 1781.51 7700
40 60 1778.71 8700
30 75 1787.2 9700
30 75 1787.7 10700
30 75 1783.74 11700
30 75 1781.98 12700
20 90 1788.23 13700
20 90 1790.45 14700
20 90 1794.52 15700
20 90 1787.77 16700
20 90 1789.24 17700
20 90 1789.95 18700
10 105 1797.92 19700
10 105 1802.17 20700
10 105 1799.9 21700
10 105 1799.92 22700
10 105 1802.94 23700
0 120 1800.32 24700
0 120 1804.17 25700
0 120 1800.91 26700
10 105 1795.0 27700
10 105 1797.27 28700
0 120 1797.62 29700
0 120 1813.81 30700
0 120 1802.62 31700
0 120 1805.42 32700
0 120 1798.16 33700
0 120 1802.38 34700
0 120 1799.23 35700
0 120 1803.55 36700
0 120 1804.21 37700
0 120 1798.93 38700
0 120 1795.43 39700
10 105 1801.57 40700
0 120 1803.01 41700
0 120 1801.26 42700
0 120 1798.16 43700
0 120 1801.27 44700
0 120 1791.96 45700
20 90 1797.97 46700
10 105 1795.46 47700
10 105 1803.42 48700
0 120 1802.38 49700
for variance_limit in np.arange(2700,100000,10000):
print(variance_limit)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com