Which of the following are true about simulations over real-world experiments? Choose all that apply.
Simulations can be sped up, slowed down, or paused, whereas experiments run in real time.
Simulations are always more accurate than real-world experiments.
Simulations only include the factors we program in, but experiments include all possible factors.
Simulations are fairly cheap, and experiments can be expensive.
Simulations are always much faster to set up than real-world experiments.
All of the above.
Solution: A, C, D.
[24.2] Matching: Model View Controller
Match the definitions below to the correct terms:
Term
Model
View
Controller
Definition
Repeatedly displays current state of the model
Updates components to make changes in the simulation
Stores the core components and rules of the simulation
Solution:
Model: Stores the core components and rules of the simulation
View: Repeatedly displays current state of the model.
Controller: Updates components to make changes in the simulation
[24.3]
Name a few examples of real-world experiments or events that we could create simulations for.
Solution: (just examples, there are many correct answers!))
Ant colonies, Covid-19 (pandemics), Gravity, Climate change, Wildfires, Animal migration, Rumor mills, etc.
[24.4]
Let’s say you run a lemonade stand and you want to predict how much lemonade you’ll sell on a given day.
What should the model’s rules and components be?
Solution (wording may vary):
Components:
Cups of lemonade sold
Price per cup of lemonade
Temperature outside
Types of lemonade available (number of flavors)
Rules:
Some number of people will consider buying lemonade every day
More people will buy lemonade when it’s hot outside
People will be less likely to buy lemonade if prices are higher
More flavors increases the number of customers (to an extent)
[24.5]
To try and reduce the devastation caused by wildfires, you want to create a model that predicts how wildfires spread.
What should the model’s rules and components be?
Solution (wording may vary):
Components:
2D land map (can be simplified into a grid) representing the different environments (cities, forest, water, etc.)
Dryness level: variable representing how dry the conditions/potential fuel are
Wind: can store both magnitude and direction (e.g. North at 15mph)
Rules:
Each cell is either “burning” or “not burning”
If a cell is burning, it has a certain probability (P) of igniting the cells next to it
P increases if wind is blowing in the direction of the neighbor
P decreases if the neighboring cell is water
P increases if the neighbor is uphill of/more elevated than the original cell
[24.6]
You want to model a local park to see if the grass will survive the summer, based on the weather and the number of visitors.
What should the model’s rules and components be?
Solution (wording may vary):
Components:
Grass health (can represent as a percentage from 0%-100%)
Daily foot traffic (number of people walking on the grass on a given day)
Inches of rain
Temperature
Rules:
Grass health increases slightly after every day it rains
Grass health decreases faster if the temperature is above 90°F without rain
P increases if wind is blowing in the direction of the neighbor
For every 100 people who walk on the grass, grass health decreases by a certain percentage (set a damage value)
If the grass health reaches 0%, it becomes dirt and cannot increase/improve
[24.7] Imagine you’re trying to build a model for an amusement park to estimate how much profit can be made from different configurations of the park.
What would happen if you didn’t include a variable for weather?
Solution: (wording may vary)
Ignoring weather in your simulation would be an example of incorporating bias and error into your simulation.
If you didn’t factor in weather as a variable, you wouldn’t account for events like rainy days, where people would be less likely to come to the park, or summer being a more popular season due to warmer temperatures.
This would make your profit estimates less accurate.
Remember : Simulations Rely on the Model! Simulations are powerful, but they can also be suspect to error and bias, because the results are influenced by what is included in the model.
[24.8] What are the two ways we can run simulation rules? Hint: These are known as controllers.
Time based simulations (over a period of time), Event based simulations (when events happen)
Code Reading: Color-Changing Cube
Scenario: Imagine we want to draw a square (100x100 pixels) and have its color change over time. Answer the following questions based on this scenario.
[24.9] What should our components be? (Select all that apply)
Hint: Remember that components hold any values that might change!
Transparency
Size
Color
Shape
Solution:
B. Size
C. Color
[22.10] What should our rules do?
Hint: Rules describe how the model changes over time!
Draw a new square in a different color than the previous one
Change the color of the square every time the runRules function is called
Create a new shape in the model
Remove the color of the square
Solution:
B. Change the color of the square every time the runRules function is called
[24.11] What should our view do?
Hint: The view displays our model!
Solution: (wording may vary)
Draw the square in the middle of our canvas; set the square’s color based on the color in the model
Code Writing: Color-Changing Cube
Scenario: Here is the code implementation for the Color-Changing Cube above. You can copy and paste this directly into Thonny!
def makeModel(data):
# put variables in data here
data["color"] = "red"
data["size"] = 50
def makeView(data, canvas):
# (200, 200) is center point
# make sure to reference data for the parts that change!
canvas.create_rectangle(200-data["size"], 200-data["size"],
200+data["size"], 200+data["size"],
fill=data["color"])
def runRules(data, call):
import random
# Let's pick a color randomly!
newColor = random.choice(["red", "orange", "yellow",
"green", "blue", "purple"])
data["color"] = newColor # update data to change the model
Note: Make sure your indentation stays consistent when pasting into Thonny!.
[24.12] In Thonny, Modify the function above to randomly change the outline color of the square. Hint: We’re creating a new value that’s changing, so we’ll need to add a component to the model, and then change it in runRules and access it while drawing the square in makeView.
Solution:
def makeModel(data):
# put variables in data here
data["color"] = "red"
data["size"] = 50
data["outline"] = "black"
def makeView(data, canvas):
# (200, 200) is center point
# make sure to reference data for the parts that change!
canvas.create_rectangle(200-data["size"], 200-data["size"],
200+data["size"], 200+data["size"],
fill=data["color"],
outline=data["outline"])
def runRules(data, call):
import random
# Let's pick a color randomly!
newColor = random.choice(["red", "orange", "yellow",
"green", "blue", "purple"])
data["color"] = newColor # update data to change the model
data["outline"] = random.choice(["black", "white", "red", "green"])
[24.13] In Thonny, modify the function above to include a mousePressed function so that the square/cube gets smaller when the mouse is pressed.
Solution:
def makeModel(data):
# put variables in data here
data["color"] = "red"
data["size"] = 50
def makeView(data, canvas):
# (200, 200) is center point
# make sure to reference data for the parts that change!
canvas.create_rectangle(200-data["size"], 200-data["size"],
200+data["size"], 200+data["size"], fill=data["color"])
def runRules(data, call):
import random
# Let's pick a color randomly!
newColor = random.choice(["red", "orange", "yellow", "green", "blue", "purple"])
data["color"] = newColor # update data to change the model
def mousePressed(data, event):
data["size"] -= 10 # decrease size parameter on mouse press
[24.14] Based on what we did in both of the previous problems, modify the above function so that the square changes color to blue if the mouse is clicked within the shape, and otherwise changes to purple.
Solution:
def makeModel(data):
# put variables in data here
data["color"] = "red"
data["size"] = 50
def makeView(data, canvas):
# (200, 200) is center point
# make sure to reference data for the parts that change!
canvas.create_rectangle(200-data["size"], 200-data["size"],
200+data["size"], 200+data["size"], fill=data["color"])
def mousePressed(data, event):
# choose color based on if the click is in the square or not
if (200-data["size"] <= event.x <= 200+data["size"]) and
(200-data["size"] <= event.y <= 200+data["size"]):
data["color"] = "blue"
else:
data["color"] = "purple"