INEN Computer Applications in IE
Spring 2022
Instructions:
Imagine the following scenario. You a part of an engineering team developing a simulation to study work flow at your company’s manufacturing site. The site operates as a job shop where jobs are processed step-by-step moving from one workstation to the next until completed. Each workstation has one or more tools and room for a single operator. In your simulation you refer to the workstation and operator as a “server.” Each workstation also has an area where parts can be placed to await being worked on. In the simulation you call this a “queue.”
Your supervisor has provided you with some existing Python code that a previous team member created. The code simulates a simple manufacturing process involving two servers in a line layout. Parts first go to Server A and to be worked on. Once Server A is done, the parts are moved to Server B. Once Server B is done the parts are complete. Server A’s queue can hold no more than 10 parts. If Server A’s queue is full new job requests must be rejected until a space opens in the queue. Server B can only have 5 parts in its queue and if it becomes full, Server A must stop work until a space opens.
As a new member of the team with some Python programing skills your supervisor has asked you to make some improvements to the existing code. Below are listed the 3 requirements that your supervisor has given you. Your job is to modify the code to meet the requirements. There is also a bonus challenge for you to do.
The Python script 2022PracticalExam01v01.py contains the code that your supervisor has provided. This should be a functional python script. Open in in the Python IDLE and execute it to make certain it works.
What You Will Turn In for a Grade
You will turn in a Microsoft Word Document and your edited python script in order to get credit for the exam.
In the word document, make sections labeled Requirement 1, Requirement 2, Requirement 3, and Bonus Challenge. Paste in the code that you modified or added for each requirement and for the bonus challenge. Don’t copy the entire script, just copy the relevant sections. If you only edited part of the code that you paste into Word, you the yellow highlighter to show the parts you changed.
If you cannot get your code to work properly for any of the requirements it is very important that you write an explanation of what you did in the Word Document. You will only get the benefit of the no anxiety clause if you show me that you really thought about the problem and tried to solve it. If you code works, technically you do not need to write anything, but you are encouraged to give some explanation just in case there is a problem you are not aware.
Requirement 1:
Currently the simulation parameters can only be modified by editing the python script directly. Modify the script so that the user is prompted for the following parameters each time the script is run
• The number of time steps in the simulation
• The probability that a new job will be requested at each time step.
Hints:
• Find the corresponding variables in COMMENT BLOCK 01.
• Remember that the input() function returns a string variable. Use int() to covert a string to an integer and use float() to convert a string to a number with decimal places.
• The user must input a number between 0 and 1 to represent probability. You do not have to write error-handling code to check if the users did this correctly.
Requirement 2:
Three variables, Job_1_Prob, Job_2_Prob, and Job_3_Prob are used to determine what type of job each new job is. Modify the script to provide the following:
1) Ask the user if he or she wants to change the job type probabilities 2) If the user say yes:
a. Inform the user that there are three job types and the sum of the probabilities for all three types must equal 1.
b. Prompt the user to enter the three probabilities
c. Check to make sure the sum of the three probabilities equals 1. If it does not:
i. Inform the user that the sum did not equal 1
ii. Do not execute the rest of the script. The script should just end and the user can re-run it if they want to try again. Hints
• Find the corresponding variables in COMMENT BLOCK 01.
• Remember that the input() function returns a string variable. Use float() to convert a string to a number with decimal places.
• Use nested if/else statements to make decisions and determine which code should be run.
• Python has a function called quit() which you can use to immediately end your script. If you include quit() with no arguments python will open a dialog box to ask you if you want to kill the script, just click yes. You may or may not need to use quit() depending on how you structure your code.
Requirement 3:
In the current code, the number of time steps that it will take a server to complete an individual job is determined randomly using random.randint(). You can see this in the code following COMMENT BLOCK 02 and COMMENT BLOCK 06. The function random.randint() is creating a discrete uniform distribution, meaning each of the results have an equal probability of occurring. Data that your team has collected from time studies show that a discrete uniform distribution is not the best choice for Server B. The table below show the results of your time studies:
Time Step Frequency of Occurrence for Server B by Job Type
Time Steps: 1 2 3 4 5 6 7 8
Job Type 1 0 1 3 2 1 0 0 0
Job Type 2 2 4 1 0 0 0 0 0
Job Type 3 0 0 1 3 1 2 4 1
Update the Code following COMMENT BLOCK 02 to use a probability distribution matching the table above when assigning the value to B_Steps.
Hints:
• Create a list for each Job Type, for example JT1 = [2, 3, 3, 3, 4, 4, 5] that has the correct number of entries for the corresponding time steps. The example above corresponds to Job Type 1 in the table. Notice how in the table there are 0 results for a time step of 1 so 1 does not occur in the list, there is 1 result for time step 2 so it occurs once in the list, there are 3 results for time step 3 so it occurs three times, there are 2 results for time step 4 so it occurs twice in the list, and there is one result for time step 5 so it occurs once.
• Use random.randint(0, len(JT1)-1) to pick a random integer that will correspond to the index number for TJ1. You can use that random integer to select the time step value from the JT1 list. In this way, for example, it is more likely that you will select times step 3 than time step 5 because 3 shows up 3 times in the list and 5 only shows up once.
• Remember that you select an item from a list using brackets [] and the first item in the list is item 0, the second item is item 1, and so on.
Bonus Challenge
In order to get the above rewards, your code must work successfully, there is not partial credit. You are strongly encouraged to try the bonus challenge even if you don’t think you can get it.
Instead of printing the results to Shell, each time the user runs the code, prompt for a filename and write all the output data to the name file. The data you write to the file should include everything after COMMENT BLOCK 09 and COMMENT BLOCK 10
Hints
• You can simply save the file to the same folder that the python script is located in.
• You can use F = open(filename, “w” ). The “w” argument will make open and overwrite an existing file or create a new file. The filename argument should be the name of the file the user provides (an example might be Sim001Data.txt). The variable F will be the file object that you create with this line of code.
• When you are done writing to the file be sure to close it with F.close().
• The F.write() method only accepts a single string as an argument. This is different from the print() function which can accept multiple argument to print, as well as the sep and end arguments. For example two lines of code from below COMMENT BLOCK 09 are
print(“Step:”, step, end = ” \t”)
print(“Server A: Job Type:”, A_Job, “Steps:”, A_Steps, “A queue: “, end = “”)
You could convert these to a string like this:
s = “Step: ” + str(step) + ” \t Server A: Job Type: ” + str(A_Job) + ” Steps: ” + str(A_Steps) + ” A queue: ”
and concatenate additional items to it to get all the information you need in order to write a line to the file. Be sure to concatenate a “\n” newline escape character at the end of each line.