Homework 5: Algorithmic Bias and Agent-based Modeling (EXTRA CREDIT)#
✅ Put your name here.#
CMSE 201 – Spring 2025#
Learning Goals#
In this homework, you will:
Access and manipulate 2D arrays
Implement and explore an Agent-based model
Reflect on Data Ethics and Algorithmic Bias
Practice goals#
Using functions to optimize code development
Debugging complex code
Part 0. Academic integrity statement (2 points)#
In the markdown cell below, paste your personal academic integrity statement. By including this statement, you are confirming that you are submitting this as your own work and not that of someone else.
✎ Put your personal academic integrity statement here.
Part 1 (15 points): The Algorithm#
✅ Part 1.1 (6 points)#
Go to https://whatsthealgorithm.com/index.html and play the game (you can also play this game from your phone). When you have finished this game, come back to this notebook and answer the following questions:
What do you think the developers of this game wanted the players to learn from it?
If you were able to find your data on the internet that determines your algorithm, what are five keywords you think would show up?
What are your thoughts and feelings about companies having access to data showing your likes and dislikes of things to tailor products and ads specific to you. Should there be restrictions? Should you have access to manage this data?
✎ Put your answer here.
✅ Part 1.2 (9 points)#
Now go to the resources tab on the website and select one resource to go through and learn more about. When you have finished, come back and answer these questions:
Which resource did you choose to learn about and how long did it take you to go through?
Summarize the resource and tell us what you learned from it (~3-5 sentences).
What are some broader implications of content being tailored to specific likes and dislikes?
✎ Put your answer here.
Part 2: Modeling Traffic Flow with Agent-based Modeling (20 points)#
In this problem, you will be exploring how we can model traffic flow using Agent-based models (and what we can learn from it!). Below, there are three research questions. You must choose one of them to explore for this section. Make sure you answer all of the questions (beginning below the working basic model).
RQ1: How does the density of cars on a road affect the likelihood of traffic jams forming?
RQ2: What is the optimal speed limit that maximizes traffic flow while minimizing congestion?
RQ3: How do different braking behaviors impact overall traffic flow?
✅ 2.1 Question (2 points)#
Write down which research question you will explore in the cell below!
✎ Put your answer here!
Running the Basic Model#
Run the two cells below to see how the basic traffic flow model works. Take a look at the different functions used to manage and run the simulation. Take note of what different parameters you see and how they are used in the model. Once you have run those cells, move on to the questions below!
Note: You will likely see some code that is unfamiliar to you. If that is the case, first try to use your resources (e.g. office hours, Teams, Python documentation, HiTA) to figure it out!
import numpy as np
import matplotlib.pyplot as plt
def create_road(length=100, n_cars=20):
road = np.zeros(length) # 0 = empty, 1 = car
car_positions = np.random.choice(length, n_cars, replace=False)
car_speeds = np.zeros(n_cars)
for pos in car_positions:
road[pos] = 1
return road, car_positions, car_speeds
def update_car_speed(current_speed, road_ahead, max_speed=5):
if road_ahead:
return max(0, current_speed - 1)
return min(max_speed, current_speed + 1)
def move_cars(road, positions, speeds):
new_positions = []
new_speeds = []
for i, (pos, speed) in enumerate(zip(positions, speeds)):
# Check if there's a car ahead
road_ahead = any(abs(p - pos) < speed + 1 for p in positions if p > pos)
# Update speed and position
new_speed = update_car_speed(speed, road_ahead)
new_pos = (pos + new_speed) % len(road)
new_positions.append(new_pos)
new_speeds.append(new_speed)
# Update road state
new_road = np.zeros_like(road)
for pos in new_positions:
new_road[int(pos)] = 1
return new_road, np.array(new_positions), np.array(new_speeds)
# Initialize the simulation
road_length = 50 # Smaller road for easier visualization
num_cars = 10
road, positions, speeds = create_road(road_length, num_cars)
# Function to run simulation for n steps
def run_simulation(n_steps=20):
current_road = road
current_positions = positions
current_speeds = speeds
for step in range(n_steps):
# Display current state
print(f"Step {step}")
print(''.join(['X' if x == 1 else '_' for x in current_road]))
# Update simulation
current_road, current_positions, current_speeds = move_cars(
current_road, current_positions, current_speeds
)
# Optional: add a small delay to see the animation
plt.pause(0.5)
# Run the simulation
run_simulation()
Step 0
_____________X__X_XX_____X____X___X_X____X___X____
Step 1
______________X__X_XX_____X____X___X_X____X___X___
Step 2
________________X__X__X_____X____X___X_X____X___X_
Step 3
_X_________________XX_X__X_____X____X_X___X____X__
Step 4
_X___X_______________XX_X____X_____X__X_X_____X___
Step 5
_X__X_____X___________XX___X______X___XX___X______
Step 6
_____X__X______X______X__X_____X______X_XX_____X__
Step 7
__X_____X____X______X__X____X_______X____X__X_____
Step 8
_______X____X_____X_____XX______X_______X__X____X_
Step 9
___X_______X_____X_____X___XX________X_____X__X___
Step 10
X_______X_______X_____X____X_X__X_________X__X____
Step 11
_____X_______X_______X____X___X_X____X________X_X_
Step 12
__X_______X_______X______X___X__X___X_____X______X
Step 13
___X___X_______X_______X____X__X___X_____X_____X__
Step 14
__X___X_____X_______X______X__X___X____X______X___
Step 15
_X____X___X______X_______X____X__X____X_____X_____
Step 16
_____X___X_____X______X______X__X____X_____X_____X
Step 17
____X___X____X______X______X____X__X______X_____X_
Step 18
___X____X___X_____X______X_____X__X____X_______X__
Step 19
__X____X___X_____X_____X______X___X__X______X_____
✅ Question 2.2 (3 points)#
For the research question you chose, what parameter(s) do you plan to change to explore your research questions? What values will you explore? Provide reasoning for your choice(s).
✎ Put your answer here!
✅ Question 2.3 (10 points)#
In the cell(s) below, make changes to the base simulation to explore your research question and record your results (with words and/or plots you save).
✎ Put your answer here!
# put any code here
✅ Question 2.4: Reflection (5 points)#
What did you learn about your research question? Support your claims with evidence from your simulations.
✎ Put your answer here!
Part 3: Making your Own (simple) Agent-based Model (25 points)#
You now have two examples of Agent-based models. Your task in this section is to develop your own Agent-based model that is different from the traffic flow model and the forest fire (though feel free to re-use code!). There is an activity on HiTA to help you navigate this problem! Navigate to HiTA and select the Activity Labeled “Homework 5 (optional): Part 3. You can also try this direct link.
Broadly, you will need to decide a context (e.g. disease spread, game of life) for your model. You will then do your best to code the basic model and get it to run for multiple time steps. This is a challenging problem that also leaves space for creativity! To get you started, here are the approximate steps for what you will need to do for your idea:
Decide what kind of model you are going to try to code (disease spread, game of life, ants, etc.). If you are having trouble coming up with an idea to try, go back to NetLogo for some additional ideas!
Define how you will set up your board (how big should it be? how will you define the initial state?)
Visualize your initial set up
Determine the rules for advancing your board (feel free to use OnBoard() and getNeighborValues from the Forest-fire model directly!)
Determine how long your model should run
Demonstrate what your model does over multiple time steps
Note: The grading is as follows for this problem
25 out of 25 points for a model that runs and includes an implementation for all of the above steps
20 out of 25 points for a model that runs but is missing one of the steps
10 out of 25 points for a model that runs but is missing multiple steps
5 out of 25 points for an attempt that does not run and/or has multiple bugs
# put any code here (feel free to make additional cells!)
✎ Put any explanatory text here!
Assignment instructions#
Work through the following assignment, making sure to follow all of the directions and answer all of the questions.
This assignment is due at 11:59 p.m. on Friday, April 25th. It should be uploaded into the “Homework Assignments” dropbox folder for Homework #5. Submission instructions can be found at the end of the notebook. Note: Individual sections may adjust this deadline to be later, please reach out to your section instructor!