Question: Ecosystem Simulation (6pts) For this final assignment, we are creating a simple simulation of an ecosystem with a real-time visualization using turtle (Links to an
Ecosystem Simulation (6pts) For this final assignment, we are creating a simple simulation of an ecosystem with a real-time visualization using turtle (Links to an external site.)Links to an external site. graphics such that entities in the simulation are defined with objects and classes. In contrast to previous assignments, there is large amount of given code that you have to understand but only a relatively small amount of code has to be written to solve this assignment.
Step 1: Understand the Code and Experiment As your first step, you should copy the provided code below into a new file. It defines classes for Entity, Grass, Sheep and sets up a screen to continuously simulate and visualize an ecosystem.
import random, turtle
# Set up screen screen = turtle.Screen() screen.setup(width=420, height=420) screen.setworldcoordinates(0, 0, 20, 20) screen.tracer(0, 0)
class Entity(turtle.Turtle): """An abstract entity in the world such as vegetation or an animal."""
def __init__(self, x, y): """Move to the given coordinates without drawing.""" super().__init__() self.speed('fastest') self.up() self.goto(x, y)
def other_entities_here(self): """Returns a list of other entities living at the provided coordinates.""" res = [] x, y = self.xcor(), self.ycor() for e in screen.turtles(): if e.xcor() == x and e.ycor() == y and e is not self: res.append(e) return res
def neighboring_entities(self): """Returns a list of other entities living at neighboring coordinates.""" res = [] x, y = self.pos() for e in screen.turtles(): if e.xcor() == x - 1 and e.ycor() == y \ or e.xcor() == x + 1 and e.ycor() == y \ or e.xcor() == x and e.ycor() == y - 1 \ or e.xcor() == x and e.ycor() == y + 1: res.append(e) return res
class Grass(Entity): """Grass is a green square that regrows after some time."""
def __init__(self, x, y): super().__init__(x, y) self.color("green") self.shape("square") self.growth = 100
def simulate(self): """Grass slowly regrows.""" if self.growth < 100: self.growth += 4 if self.growth == 100: self.color("green")
def eaten(self): self.growth = 0 self.color("brown")
class Sheep(Entity): """Sheep eat grass and reproduce to other cells."""
max_health = 20
def __init__(self, x, y): super().__init__(x, y) self.setheading(random.randrange(4) * 90) self.color("white") self.shape("turtle") self.shapesize(0.5) self.health = self.max_health // 2
def simulate(self): """Sheep move, eat grass and reproduce."""
# Turn left, right or keep straight rot = random.choice(['left', 'right', 'straight']) if rot == 'left': self.left(90) elif rot == 'right': self.right(90)
# Move one step but prevent leaving the screen self.forward(1) if self.xcor() < 0 or self.xcor() > 20 or self.ycor() < 0 or self.ycor() > 20: self.undo() self.left(90) self.goto(round(self.xcor()), round(self.ycor()))
# Moving requires energy self.health -= 1
# Is the sheep standing on fully grown grass? for entity in self.other_entities_here(): if isinstance(entity, Grass) and entity.growth == 100: # then eat the grass which recovers energy entity.eaten() self.health += 2 self.color('white')
# Reproduce if at full health if self.health >= self.max_health: Sheep(self.xcor(), self.ycor()) self.health -= 2
# Dies if reaches 0 health if self.health <= 0: screen._turtles.remove(self) else: # Set sheep size depending on health self.shapesize(self.health / self.max_health, self.health / self.max_health)
def mainloop(): """Performs one step of simulation and draws the world.""" for e in screen.turtles(): e.simulate() screen.update() screen.ontimer(mainloop, 500) # Wait 500 milliseconds
# Initialize world with a 20x20 grid full of grass for x in range(0, 21): for y in range(0, 21): Grass(x, y)
# Put one sheep at (10, 10) Sheep(10, 10)
# Start main program screen.update() screen.ontimer(mainloop, 500) screen.mainloop() Read through the provided code. It may not be possible to follow all of it but try to understand the rules for grass growth and the simulation method of the sheep (which look like white turtles).
Then, 1) change the time interval between simulation steps from 500 milliseconds to 10 milliseconds to speed up the simulation. Is the ecosystem stable and does it run forever? 2) Reduce the growth rate of grass by 50%. How does this affect the ecosystem? Finally, 3) increase the maximum health of sheep. Does this positively or negatively affect the population in the ecosystem?
Apply these changes directly into the provided code and answer the questions above by writing comments into the code. There are no right or wrong answers because this course is primarily about programming and not biology. However, you should still try to understand the code and its dynamic behavior through experimentation and observation.
Step 2: Add Predators This type of simulation is useful for understanding complex dynamic systems (Links to an external site.)Links to an external site.. In particular, it is possible to model forest fires (Links to an external site.)Links to an external site. with two-dimensional continuous simulations similar to the one given above. As a second step, you need to extend this Ecosystem model (Links to an external site.)Links to an external site. with predators.
Add a new "Wolf" class definition to the code below the Sheep class. You can start by copying the existing Sheep class and then apply the following changes:
Change the color from white to black. Change the shape to "arrow". Replace the code for eating grass with code that checks all neighboring tiles for sheep. If there is a sheep and the wolf has less than half of its health, the wolf should move to that tile, reduce the sheep's health to 0, set its own health to maximum and print a message. In this simulation, wolfs do not reproduce. (The code for moving, starvation and sizing based on health can be same as in the Sheep class.) Now, add a wolf instance to the world by invoking the constructor at the end of the program with coordinates different from the sheep and run the simulation.
Finally, experiment with the maximum health of sheep and wolf until the ecosystem is stable enough to allow the wolf to eat at least three sheep.
(You can find more background information about this type of predator-prey simulations on Wikipedia, e.g. Lotko-Volterra equations (Links to an external site.)Links to an external site..)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
