Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DataScienceUWL
GitHub Repository: DataScienceUWL/DS775
Path: blob/main/Homework/Lesson 12 HW - Scheduling and DA with Uncertainty/Homework_12.ipynb
871 views
Kernel: Python 3 (system-wide)
import numpy as np import pandas as pd from pyomo.environ import *

Lesson 12 - Scheduling and Decision Analysis with Uncertainty

When asking questions about homework in Piazza please use a tag in the subject line like HW1.3 to refer to Homework 1, Question 3. So the subject line might be HW1.3 question. Note there are no spaces in "HW1.3". This really helps keep Piazza easily searchable for everyone!

For full credit, all code in this notebook must be both executed in this notebook and copied to the Canvas quiz where indicated.

General Questions

Question 1: Practice fetching values from a triangular distribution. (2 points)

Given the triplet (8, 20, 50) that corresponds to the left, mode, and right sides of the triangle, fetch 10 random samples from a triangular distribution. Use a numpy random seed of 121. Use either a loop or a vector approach.

What is the last number in the draw?

  • 18

  • 20

  • 50

  • 43

  • 19

  • 26

Question 2: Practice Fetching Values from an Exponential Distribution. (2 points)

Using a numpy random seed of 121, fetch 10 values from an exponential distribution with a scale of 100. What is the last number in the draw, rounded to 2 digits?

  • 346.28

  • 11.80

  • 177.30

  • 81.15

  • 28.57

  • 16.48

#Your code here

Reliable Construction Scheduling, with Uncertainty

This intro is not included in Canvas

Reliable Construction wants to determine when each activity should start in a construction schedule in order to minimize the overall time it takes to construct a large commercial building. From past projects, they know that the time each task takes tends to follow a triangular distribution, where their optimistic estimate is the left (low) side of the triangle, their most likely estimate is the middle (mode) of the triangle, and their pessimistic estimate is the right (high) side of the triangle. Use these estimates and the solution to this problem from Lesson 3 to construct and solve a single Pyomo model to determine the optimal schedule that takes the minimal amount of time.

  • Use a numpy random seed of 121 at the beginning of your code cell. (If you write a function, put it inside, but at the beginning of the function.)

  • Be sure to use int() or .astype('int') in your code to round down to integers.

Note: We strongly encourage you to read all the instructions and use functions to modularize your code. Our solutions involve three functions - one to run the simulations (with any number of iterations, and with or without artifacts), one to figure profit, and one to figure probability. If you use a function, be sure to set your seed inside your function, so that it is set each time you call the function.

ActivityDescriptionImmediate PredecessorsOptimistic EstimateMost Likely EstimatePessimistic Estimate
AExcavate--71421
BLay the FoundationA142156
CPut up the rough wallB4263126
DPut up the roofC283570
EInstall the exterior plumbingC72835
FInstall the interior plumbingE283570
GPut up the exterior sidingD354277
HDo the exterior paintingE,G3556119
IDo the electrical workC214963
JPut up the wallboardF,I216363
KInstall the flooringJ212828
LDo the interior paintingJ193542
MInstall the exterior fixturesH71421
NInstall the interior fixturesK,L353563

Question 3: What is the minimum number of days that Reliable Construction will take? (2 points)

  • 301 days

  • 323 days

  • 400 days

  • 308 days

  • 289 days

# Your code here #hint - this is how we set up our function def construct_schedule(simSize=1, artifacts=False, debug=False): np.random.seed(121) #yourcodehere

Building a Larger Simulation

A single simulation doesn't really tell us much. We really need to run the simulation multiple times in order to get a good estimate of the minimum amount of days it might take. Use a loop or vector approach to run 1000 iterations of the Pyomo model. For each iteration, track the optimal (minimum) number of days it will take to complete the project. You should have a 1000 number numpy array when you are done.

The first value in your array should match the value found in Question 3.

Don't start a second function here, just continue to build the construct_schedule function above.

In the end that function should be able to run all of your simulations with and without artifacts

Also, to match our answers you should generate your random durations one set at a time inside the main solution loop.

If you generate all of the random durations in advance you likely will be working with a different sequence of random numbers.

Question 4: What is the average number of days it will take, rounded down to the nearest integer. (2 points)

Hint: use .astype("int"), not np.round() on the array of results from your simulation.

  • 316

  • 308

  • 324

  • 313

  • 298

#Your code here

Considering Profit

Note: This intro not included in Canvas.

Reliable is interested in estimating their total profit on this contract. Their base profit is $5.4 million dollars. But there is a bonus for completing the project early, and a daily penalty for finishing the project late.

Consider these factors.

  • The base amount that Reliable will earn is $5.4 million.

  • If Reliable completes the project in 280 days or less, they will get a bonus of $150,000 ($.15 million).

  • If Reliable misses the deadline of 329 days, there will be a $25,000 ($.025 million) penalty for each day over 329.

Use your results from your 1000 iteration simulation to calculate the average profit.

It's good practice to modularize your code so that each component performs a single task.
Use a separate function that takes an array of simulated schedule lengths and computes the corresponding profit.
Extend this function later (HW12.10) to include the case with artifacts.

Question 5: What is the average profit in millions, rounded to 2 digits? (2 points)

  • 5.12

  • 5.28

  • 6.0

  • 5.12

  • 5.21

#hint - this is how we set up a function to fetch the profit def fetchProfit(totalDays, artifacts=False): ''' Parameters: totalDays - a numpy array of the total days it took for the construction project on each iteration of the simulation artifacts - a flag to determien if we're simulating artifacts or not ''' #Your code here profit_list = np.zeros(...) profit = 5.4 for i,d in enumerate(totalDays) if d > 329: profit_list[i] = 5.4 - elif d <= 280 profit = .... profit_list.append(profit)

Question 6: What is the probability that Reliable Company will finish the bid in 280 days or fewer? Choose the closest answer. (2 points)

  • .290

  • .643

  • .192

  • .067

  • .218

Question 7: What is the probability that Reliable Company will finish the bid in more than 280 days, but 329 days or fewer? Choose the closest answer.(2 points)

  • .290

  • .643

  • .192

  • .067

  • .218

Question 8: What is the probability that Reliable Company will finish the bid in more than 329 days? Choose the closest answer.(2 points)

  • .290

  • .643

  • .192

  • .067

  • .218

#Hint: You could figure out 6-8 in one code cell. Add more if you need.

Add artifacts and include random cost

Note: This intro not included in Canvas

From past experience, we know that special artifacts are sometimes found in the area where Reliable Construction is planning this building project. When special artifacts are found, the excavation phase takes considerably longer and the entire project costs more - sometimes much more. They're never quite sure how much longer it will take, but it peaks around an extra 15 days, and takes at least an extra 7 days. They've seen some sites where relocating the special artifacts took as much as 365 extra days (yes - a whole year)! Change the parameters of the triangular distribution for the excavation task to include these additional days. Hint: the extra days ONLY impact the excavation task

In addition, there are usually unanticipated costs that include fines and other things. The accounting departments suggest that we model those costs with an exponential distribution with mean (scale) $100,000.

Run a second 1000-iteration simulation in which artifacts are always found.

Question 9: What is the average minimum number of days (rounded down) the project takes when artifacts are found? (2 points)

  • 400

  • 428

  • 475

  • 448

  • 399

# Your code here (You made a function right, so you're just calling it with artifacts this time, right? :)

Question 10: What is the average profit in millions when artifacts are found, rounded to 2 digits? (2 points)

  • 2.29

  • 3.00

  • 2.13

  • 2.87

  • 2.65

# your code here

Question 11: What is the probability that Reliable Company will finish the bid in less than 280 days when artifacts are found? (2 points)

  • .002

  • .927

  • .320

  • .508

  • .071

Question 12: What is the probability that Reliable Company will finish the bid in more than 280 days, but less than or equal to 329 days when artifacts are found? (2 points)

  • .002

  • .927

  • .320

  • .508

  • .071

Question 13: What is the probability that Reliable Company will finish the bid in more than 329 days when artifacts are found? (2 points)

  • .002

  • .927

  • .320

  • .508

  • .071

#Your code here. (If you have a get_probabilities function you could just call it with one line...)

Question 14 (Manually graded) (2 points)

Clearly dealing with artifacts can be very costly for Reliable Construction. It is known from past experience that about 30% of building sites in this area contain special artifacts. Fortunately, they can purchase an insurance policy - a quite expensive insurance policy. The insurance policy costs $500000 ($.5 million), but it covers all fines and penalities for delays in the event that special artifacts are found that require remediation. Effectively, this means that Reliable could expect the same profit with or without artifacts (minus the cost of the policy).

Given the estimated profit without artifacts, the estimated profit with artifacts, the cost of insurance, the 30% likelihood of finding artifacts, create a payoff table and determine what decision Reliable should make. Their decision should use as much information as is available. You should use round the simulated costs to nearest $100,000 and use units of millions of dollars so that, for example, $8,675,309 is 8.7 million dollars.

Provide appropriate evidence for the best decision such as a payoff table or picture of a suitable (small) decision tree.

Question 15: What is the expected payoff? (2 points)

  • 4.3

  • 5.2

  • 4.8

  • 5.3

  • 2.5

Question 16: Should Reliable Construction purchase insurance? (2 points)

  • No

  • Yes

  • It is impossible to tell from this analysis.

Posterior Probabilities and Full Decision Tree

Reliable has been contacted by an archeological consulting firm. They assess sites and predict whether special artifacts are present. They have a pretty solid track record of being right when artifacts are present - they get it right about 86% of the time. Their track record is less great when they there are no artifacts. They're right about 72% of the time.

Find the posterior probabilities for this decision tree, given the this prior information.

Question 17: What is the posterior probability of artifacts, given artifacts were predicted? (2 points)

  • .077

  • .432

  • .568

  • .923

Question 18: What is the posterior probability of no artifacts, given no artifacts were predicted? (2 points)

  • .077

  • .432

  • .568

  • .923

Be prepared to share a decision tree or some other mechanism of determining posterior probabilities, if asked.

Question 19: Create a Full Decision Tree (Manually graded) (6 points)

The consulting fee for the site in question is $50,000 ($.05 million).

Construct a decision tree to help Reliable decide if they should hire the consulting firm or not and if they should buy insurance or not. Again, you should round the simulated costs to nearest $100,000 and use units of millions of dollars (e.g. 3.8 million dollars) in your decision tree.

Include a picture of the tree exported from Silver Decisions.

Question 20: What is the best course of action for Reliable? (2 points)

  • Don't hire firm. Buy insurance.

  • Don't hire firm. Don't by insurance.

  • Hire firm. Don't buy insurance no matter what they predict.

  • Hire firm. Buy insurance no matter what they predict.

  • Hire firm. Buy insurance if they predict artifacts. Don't buy insurance if they don't predict artifacts.

Question 21: What is the expected payoff? (2 points)

  • $6 million

  • $4.9 million

  • $5 million

  • $2.5 million

  • $4.2 million

Question 22 (Manually graded) (4 points)

How confident are you in your decision? What could you do to increase your confidence?