Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# Finish the function below for deciding where to move agents def where_to_move(my_list, my_index, tol_prob=0.5, neighbor_N=4, print_output=False): ''' Given a population (my_list), and the index

# Finish the function below for deciding where to move agents def where_to_move(my_list, my_index, tol_prob=0.5, neighbor_N=4, print_output=False): ''' Given a population (my_list), and the index of an unhappy person (my_index), this function figures out where to move my_value so that it's happy. This assumes that my_value is unhappy where it is, by the way! This function then returns the index where my_value should move to in order to be happy. Note that the function has to take in the same tolerance probability and neighborhood size as the "is_happy" function to make sure it correctly moves people. ''' # Figure out what my current value is so that I can correctly move # myself to check for possible happiness my_value = my_list[my_index] # Create a value for tracking whether or not I'm happy happy = False # Create a variable to store where I should be move to # We set it to -1 so that we know if we failed to find # a happy place when we do our seearch new_index = -1 # Remove myself from the array so that I can insert myself # in a new location, store that new location in a temporary array # (the np.delete function might be new to you, # so make sure you understand what it does) temp_list = np.delete(my_list, my_index) # Create a loop that will try new positions until it finds one I'm happy in. # Initially we'll only look to move one spot in either direction. # We include a check to check for happiness and a check to make sure we aren't # searching too far. i = 1 while (happy == False) and (my_index - i >= 0 or my_index + i <= (len(my_list)-1)): # Try moving to the left (make sure we don't step out of the bounds of the array) # I can do this by "inserting" myself into the array I removed myself from # "np.insert()" is the function we want for this! # Check if this new location makes me happy. Remember, we have a function for this! # Try moving to the right (make sure we don't step out of the bounds of the array) # Use the same method as moving to the left # Check if this new location makes me happy. Remember, we have a function for this! # Check to see if moving to the left made me happy, # If so, define the new index that I need to move to. # If not, check if the move to the right made me happy # and update the index if it did. # You could also considering include a print statement # that says where you should move to be happy. # Make sure that if I found a location that made me happy # that I update the value of my "happy" variable so that the loop exits! # If neither option is a happy move expand our search by one # before returning to the top of the loop i += 1 # Finally, check to see if we succeeded in finding a place to move to. if new_index == -1: print ("something has gone wrong with where_to_move! probably couldn't find a happy place.") return new_index

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions