Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is my homework assignment for this week. This is the last part out of the 3 different parts of the problem and I cannot

This is my homework assignment for this week. This is the last part out of the 3 different parts of the problem and I cannot figure it out.

Part 1: Running a simulation

In this part of the project you will have the computer run a large number of simulations of the pris- oners dilemma. We will assign both partnerAChoice and partnerBChoice a value using randint(), which will allows us to run hundreds of trials in very little time.

When your program begins, it should first prompt the user for a number of trials to simulate (between 10 and 500). After that, it should simulate each partner making the specified number of choices, each time assigning a random value to both partner A and partner B. After the specified number of trials have been completed, your program should print out the average number of years served by partner A across all trials, and the average number of years served by partner B across all trials.

To facilitate this, you should first write a recursive function named run_trials. This function should accept 1 argument named count that should indicate how many more trials should be run (and hence, how many more recursive calls should be made). This function should return 2 values: the first is the total number of years partner A has had to spend in jail, the second being the total number of years partner B has had to spend in jail. Here is an example of the syntax to return two values from a function in Python:

def returns_two(): partnerAyears = 20 partnerByears = 10 return partnerAyears, partnerByears 
partnerAcount, partnerBcount = returns_two() print("Partner A spent", partnerAcount, "years in jail") print("Partner B spent", partnerBcount, "years in jail") 

run_trials should first check whether or not more trials need to be run (recursive case), or if there are no more trials to run (base case) based on the value of the argument count. Both the base and recursive cases should return two values (the years partners A and B have spent in jail, respectively). In the base case (0 trials left), return 0s for both partner As years served and partner Bs years served (were not running any more trials, so they cannot be assigned any jail

time). In the recursive case, run_trials should use randint() to generate a choice (cooperate or stay silent) for both partner A and partner B. Once each partners choice is decided, it should calculate the number of years in jail each partner would spend given their choices in this trial. After that, it get the number of years from the recursive call, add on the number of years from the current trial, and return the results (for partners A and B).

Once you have run_trials working, you should write another function named main. main should not take any arguments. It should call run_trials with an argument of 100, then take the results (number of years served by partners A and B in total across all trials), and compute the average number of years served by each partner (total years served divided by number of trials). You should then call main at the end of your program.

Here is the output from a run of the desired program:

Partner A did an average of 2.90 years in prison Partner B did an average of 3.14 years in prison 

Part 2: User-specified number of trials

Next, you should modify your program to run a user-specified number of trials instead of always running 100 trials. You should allow the user to enter a value between 10 and 500. You must use input validation to ensure that the user enters a value between 10 and 500 (though if the user does not enter an integer, it is acceptable for your program to crash). To do this, create a recursive function named get_num_trials that takes no arguments and will continuously prompt the user for a number of trials until they enter an integer between 10 (inclusive) and 500 (inclusive).

Here is the output from a run of the desired program:

How many trials should be simulated? 5000 Please enter a number between 10 and 500 How many trials should be simulated? -1 Please enter a number between 10 and 500 How many trials should be simulated? 350 Partner A did an average of 3.06 years in prison Partner B did an average of 2.93 years in prison 

Part 3: Adding additional strategies

We will now add additional strategies for the computer to use in order to run more interesting simulations. For this project, we will add only two simple strategies: Only-Silent (always chooses to stay silent) and Only-Cooperates (always chooses to cooperate). Before being prompted for the number of trials to run, the user should set the strategy of each partner from the 3 available: Random, Only-Silent, Only-Cooperates.

In order to ensure that the user enters a valid strategy, you will need to write a recursive function named get_strategy that takes no arguments and will continuously prompt the user for a strategy until they enter a valid value.

You will also need to modify run_trials. First, you will need to add two additional arguments in addition to count. These arguments should be named partnerAstrategy and partnerBstrategy, and should specify what strategy each partner should user (Random, Only-Silent, or Only-Cooperates).

run_trials should then consult these arguments when determining each partners action each trial (i.e., decisions should now only be randomly made if the Random strategy is employed).

Here is output from a run of the desired program:

3 strategies available: 1: Random 2: Only-Silent 3: Only-Cooperates Please enter partner A's strategy: 5 Must enter a number between 1 and 3 Please try again 
Please enter partner A's strategy: -2 Must enter a number between 1 and 3 Please try again Please enter partner A's strategy: 2 Please enter partner B's strategy: 3 How many trials should be simulated? 500 Partner A did an average of 6.00 years in prison Partner B did an average of 0.00 years in prison 

This is what I have so far from the 2 previous parts of the assignment:

from random import randint print("You have the following choices:") print("Option1: Cooperate with the authorities") print("Option2: Stay silent") partnerAChoice = int(input("Enter 1 or 2: ")) print("You chose", partnerAChoice)

partnerBChoice= randint(1,2) print("The computer chose", partnerBChoice)

if partnerAChoice == 1: if partnerBChoice == 1: print("You both do 4 years in prison") if partnerAChoice == 1: if partnerBChoice == 2: print("The computer does 6 years in prison") print("You are released") if partnerAChoice == 2: if partnerBChoice == 2: print("You both do 2 years in prison") if partnerAChoice == 2: if partnerBChoice == 1: print("You do 6 years in prison") print("The computer is released")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions