Top Banner
their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional to how often hares & lynxes meet, i.e., the chance of a lynx catching a hare. The lynx birth rate is also proportional to how often hares & lynxes meet, i.e., the food available for each lynx family. Lynxes only die from natural causes, and their death rate is constant.
21

The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Dec 16, 2015

Download

Documents

Melvyn Cross
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional to how often hares & lynxes meet, i.e., the chance of a lynx catching a hare.

The lynx birth rate is also proportional to how often hares & lynxes meet, i.e., the food available for each lynx family. Lynxes only die from natural causes, and their death rate is constant.

Page 2: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Computational Thinking

Recipe (Algorithm)

Relevant input

Answer

Abst

racti

on

Auto

mati

on

Problem description

Computational goalInformation extractionAlgorithm designAlgorithm implementation

Page 3: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Given , , , , , , .

Repeat these steps for :

Plot for .Plot for .

1. Generate population data.a. Repeatedly, generate next population.

2. Display population data.

Page 4: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Improve Algorithm

• Separate parts

• Generalize

• Articulate goals

Page 5: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Given , , , , , , .

Repeat these steps for :

Plot for .Plot for .

1. Generate population data.a. Repeatedly, generate next population.

2. Display population data.

Page 6: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Given , , , , , , .

Repeat these steps for :

Return and .

Page 7: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Why Separate Parts?

• Simpler

• Independently useful

Page 8: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Generalize beyond Hares & LynxesGiven , , , , , , .

Repeat these steps for :

Return and .

Page 9: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Articulate GoalsPopulations:

Given , , , , , , .Returns the predicted populations of two species, given their initial populations, the prey’s growth rate, the predation rate, the predator’s food conversion rate, the predator’s death rate, and the number of years to predict.

Repeat these steps for :

Return and .

Page 10: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Express as Python Code

Page 11: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Use What Kinds of Python Data?Populations:

Given , , , , , , .Returns the predicted populations of two species, given their initial populations, the prey’s growth rate, the predation rate, the predator’s food conversion rate, the predator’s death rate, and the number of years to predict.

Repeat these steps for :

Return and .

Page 12: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

and as Lists

𝑝𝑟𝑒𝑦 0𝑝𝑟𝑒𝑦 1𝑝𝑟𝑒𝑦 2𝑝𝑟𝑒𝑦 3𝑝𝑟𝑒𝑦 4 …𝑝𝑟𝑒𝑦=¿

𝑝𝑟𝑒𝑑0𝑝𝑟𝑒𝑑1𝑝𝑟𝑒𝑑2𝑝𝑟𝑒𝑑3𝑝𝑟𝑒𝑑4 …𝑝𝑟𝑒𝑑=¿

Page 13: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Translate Piece by PiecePopulations:

Given , , , , , , .Returns the predicted populations of two species, given their initial populations, the prey’s growth rate, the predation rate, the predator’s food conversion rate, the predator’s death rate, and the number of years to predict.

def populations(prey0,pred0,growth,predation,conversion,death,years):

"""Returns the predicted populations of two species, given their initial populations, the prey's growth rate, the predation rate, the predator's food conversion rate, the predator's death rate, and the number of years to predict."""

prey = [prey0]pred = [pred0]

Page 14: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Translate Piece by PieceRepeat these steps for :

for y in range(years):

Page 15: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

prey.append( )

Translate Piece by Piece

prey[y] prey[y] *+ (growth-predation*pred[y])

pred.append( )pred[y] pred[y] *+ (conversion*prey[y]-death)

Page 16: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Translate Piece by PieceReturn and .

return prey pred,

Page 17: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Put the Function’s Pieces Togetherdef populations(prey0,pred0,growth,predation,conversion,death,years): """Returns the predicted populations of two species, given their initial populations, the prey's growth rate, the predation rate, the predator's food conversion rate, the predator's death rate, and the number of years to predict."""

prey = [prey0] pred = [pred0]

for y in range(years): prey.append(prey[y] + prey[y] * (growth-predation*pred[y])) pred.append(pred[y] + pred[y] * (conversion*prey[y]-death))

return prey, pred

Page 18: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

How to Use The Code?

pred, prey = populations(100,50,.4,.003,.004,.2,10)

Page 19: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

A Useful Asidedef populations(prey0,pred0,growth,predation,conversion,death,years): """Returns the predicted populations of two species, given their initial populations, the prey's growth rate, the predation rate, the predator's food conversion rate, the predator's death rate, and the number of years to predict."""

prey = [prey0] pred = [pred0]

for y in range(years): print "y = ", y print "prey = ", prey print "pred = ", pred

prey.append(prey[y] + prey[y] * (growth-predation*pred[y])) pred.append(pred[y] + pred[y] * (conversion*prey[y]-death))

return prey, pred

Page 20: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Plottingimport matplotlib.pyplot as plt

def plotPopulations(times,prey,pred,preyName,predName): """Displays a plot of two populations over the given times."""

# Prey use circles connected by solid line. preyPlot = plt.plot(times, prey, 'o-' )

# Predators use squares connected by dotted line. predPlot = plt.plot(times, pred, 's:' )

# Place legend box in "best" location. plt.legend((preyPlot, predPlot), (preyName, predName), 'best')

plt.xlabel('Years') plt.ylabel('Population') plt.title('Predator-Prey Model') plt.show()

Page 21: The hare birth rate is constant, as their food supply is unlimited. Hares only die when eaten by a lynx, and the number of hares eaten is proportional.

Putting Everything Togetherimport matplotlib.pyplot as plt

def populations(…): …def plotPopulations(…): …

prey, pred = populations(100,50,.4,.003,.004,.2,10)times = range(len(prey))plotPopulations(times,prey,pred,"Hare","Lynx")