Unit 4

Simulation I

Conceptual Questions

[24.1]

Which of the following are true about simulations over real-world experiments? Choose all that apply.

  1. Simulations can be sped up, slowed down, or paused, whereas experiments run in real time.

  2. Simulations are always more accurate than real-world experiments.

  3. Simulations only include the factors we program in, but experiments include all possible factors.

  4. Simulations are fairly cheap, and experiments can be expensive.

  5. Simulations are always much faster to set up than real-world experiments.

  6. All of the above.

[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

[24.3]

Name a few examples of real-world experiments or events that we could create simulations for.

[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?

[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?

[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?

[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?

[24.8] What are the two ways we can run simulation rules?
Hint: These are known as controllers.

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!

  1. Transparency
  2. Size
  3. Color
  4. Shape

[22.10] What should our rules do?
Hint: Rules describe how the model changes over time!

  1. Draw a new square in a different color than the previous one
  2. Change the color of the square every time the runRules function is called
  3. Create a new shape in the model
  4. Remove the color of the square

[24.11] What should our view do?
Hint: The view displays our 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.

[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.

[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.