yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Simulating a beehive with for loops | Intro to CS - Python | Khan Academy


5m read
·Nov 10, 2024

Let's design a simulation with for loops. We want to answer the question: How much honey does a beehive produce over a certain period of time?

Now, there are a lot of variables that might impact honey production, like the geography, the weather, and what flowers are nearby. These are all important factors to consider in designing our simulation. But we need to think about what simplifications we can reasonably make. Based on observation, we know that a typical forager bee will make several trips to collect nectar over the course of a day.

I'm thinking the easiest way to model this might be to use a state machine where a bee moves between different states. There's a state where they've left the hive and are collecting nectar, and there's a state where they have nectar, and they're returning to the hive to drop it off. So, say maybe every hour, a bee can optionally move from the needs nectar state to the has nectar state if it's picked up from enough flowers, or it can move from the has nectar state to the needs nectar state if it's dropped off at the hive.

Our simulation then can just repeat these state transitions over some number of hours to calculate the total amount of nectar deposited. We can then use that nectar amount to calculate how much honey that typically produces. To account for some environmental factors, we can use randomness to decide how many bees move from one state to the other each hour.

To start, let's decide what variables we'll need. We want to keep track of the total number of bees in each state, so we'll want a needs nectar and a has nectar. Then we also want to count the total number of nectar deposits that we've made, so we can calculate the amount of honey at the end. All of our bees are going to start out in the needs nectar state; they're waiting in the hive, ready to go. None of them have nectar yet, so we'll initialize that to 20,000 because that's approximately how many forager bees we have.

Before we add the loop, let's just try and get one hour of collection working. Each hour, a random number of bees in the needs nectar state are going to finish collecting nectar. Sounds like we're going to need the random module, so I'm going to add that import statement at the top. The number of bees that pick up each hour is influenced by a lot of factors, but we're just going to simplify that down and say it's somewhere between 1/4 and 3/4 of the bees in the needs nectar state.

All right, now we need to handle the state transitions. Any bees that pick up move from the needs nectar state to the has nectar state, so we need to update both of those variables. Has nectar increases by the number of pickups, and needs nectar decreases by the number of pickups. We're not adding or removing these here, so the total across both states should still equal 20,000. Let's add some print statements to check that and run it a few times.

Okay, let's add our loop now to repeat this hour of activity. We want to calculate how much honey we can produce in a week, so it makes sense to use a for loop here since we already know the number of repetitions. We'll name our loop variable hour since each iteration represents an hour, and we want to repeat for 7 * 24 hours. That's not quite accurate, because bees do sleep; they're not going to be collecting for all 24 hours. So let's change that to 7 * 18.

We want to initialize all our variables before the loop, so we'll leave them up here, but we do want to repeat the state transition on every loop iteration, so we're going to indent all of these lines inside the loop. Now, when we run our simulation, we see that all of our bees move into the has nectar state, and they all eventually complete a pickup. Now we just need to model the drop-off, so the bees can move back from the has nectar state to the needs nectar state.

Each hour, a random number of bees who have nectar drop off that nectar at the hive. We're going to say it's at the same rate, so between a quarter and 3/4 of the bees in the has nectar state. Every drop-off we make is a permanent nectar deposit that other bees in the hive can then use to produce honey. So every hour, we want to increment nectar deposits by the number of drop-offs.

Okay, but when I run it, all of my bees are still stuck in the has nectar state. We still need to transition the bees from the has nectar state to the needs nectar state when they drop off. So, has nectar should decrease by the number of drop-offs, and needs nectar should increase by the number of drop-offs. But I think there's kind of a subtle bug here. What do you think?

At the top of the hour, we let some bees drop off, which means they move back to the needs nectar state, and then at the bottom of the hour, we let some bees in the needs nectar state pick up. That means the same bee could both drop off and pick up within the same hour, which doesn't seem realistic. To fix this, we need to make sure we're not updating the number of bees in each state until after we've calculated the number of drop-offs and the number of pickups because those should be calculated based on the number of bees in each state at the start of the hour.

So I'm just going to move all of the variable updates to the bottom of the loop. Awesome! Our state transitions are looking good, so let's check in on our nectar deposits. It looks like they're steadily increasing by a random amount on each iteration, which is what we'd expect. It should be increasing by the number of drop-offs, so if that total is looking good, it's time to calculate how much honey we produced.

We only want to calculate this once at the end of the week, so we don't want to put this inside of the loop body. We want it to go after the loop, indented outside of it. From our research, it seems like it takes a single bee about 90 trips to make a single gram of honey, so we're going to divide our total number of nectar deposits by 90, which should give us approximately the number of grams of honey.

I'm going to round that so we don't get a weird decimal, and if we run our simulation now a few times, we have our answer: our hive can produce around 7,000 grams of honey a week. This simulation, of course, made a lot of assumptions. There's a lot more things we can model here with more time and research, so as with most simulations, we should think critically about the results that we get.

More Articles

View All
Will Berkshire Hathaway Stock Crash Without Warren Buffett? (w/ @InvestingwithTom)
[Music] Hey guys, welcome back to the channel. Uh, today we are continuing with the new money advent calendar. We’re going strong, and I’ve got another great guest on the channel for today. We have Tom from Investing with Tom. How you doing, buddy? Prett…
Core spiritual ideas of Buddhism | World History | Khan Academy
What I’d like to do in this video is explore the core spiritual ideas of Buddhism, and we’re going to do it relative to the core spiritual ideas of Hinduism as described in the Upanishads. One, because there are significant parallels, and also because Bud…
Responding to a Capsized Boat with the U.S. Coast Guard - Smarter Every Day 277
Hey, it’s me, Destin. Welcome back to Smarter Every Day! Today, on Smarter Every Day, we’re going to continue our deep dive with the US Coast Guard, and we’re going to see how they accomplish their mission of saving people in peril and protecting the nati…
Evaluating composite functions: using graphs | Mathematics III | High School Math | Khan Academy
So we have the graphs of two functions here. We have the graph (y) equals (f(x)) and we have the graph (y) is equal to (g(x)). And what I wanna do in this video is evaluate what (g(f(…)). Let me do the (f(…)) in another color. (f(-5)) is… (f(-5)) is… An…
How to be Stoic in a Crisis
When a crisis is upon us, how can we deal with it in a Stoic way? When we look at Stoic literature, we’ll find some good advice that we can apply during times of hardship. Crises come in many different forms. We can have personal crises on a micro level, …
Why I Love The 'Comfort Zone'
We hear this one all the time: get out of your comfort zone. People tell us that success can be found in the places that we fear and that we should leave familiar territories to explore the unknown. Not getting out of your comfort zone leads to an unsucce…