Dynamic Modeling and Python

Download Report

Transcript Dynamic Modeling and Python

Dynamic Modeling and
Python
Greg Baker, November 2003
Dynamic Modeling
Dynamic Modeling
Assumption: individuals make decisions to
maximize reproductive fitness

Need fitness of possible decisions.
Look at an individual in a certain time
window

Maximize fitness at the end of time window.
Fitness?
What determines fitness?




Depends on the situation.
Fat stores? Location? Stress? Not dying.
Immediate vs. lifetime reproduction
Biology.
Keep track of each factor for the individual.


“State variables”
eg. x = energy, s = stress level
Discretizing Parameters
We can’t handle an infinite number of
values for each state variable.
We will have to pick a fixed number of
values that can be used for each


eg. energy, x = 0, 1, 2, 3, …, 30
time, t = 0, 1, 2, 3, …, 20
Examine every possible combination.
Dynamic array
Track fitness values for every combination


Will have to be stored as we calculate
Fitness:
t
0
1
2
…
T
0
1
2
x
…
30
Want t = T
values big
Filling in the array
Fitness at t = T must be determined
initially



An approximation of total lifetime fitness.
Based on values of state variables.
Biology.
Calculate backwards from time T to 0 and
fill in the table


“backwards iteration”
“dynamic programming”
Example
0
1
2
t
…
T
0
0
1
0
2
0
x
?
…
Eat
Don’t eat
0
2
2
10
4
Fitness function
Need to calculate fitness for each possible
decision

… and choose the largest.
How do we calculate the fitness?



Fitness after the decision
… adjusted by predation risk, energy loss,
etc.
Biology.
Decision Variables
To do useful stuff later, also store the
decisions themselves

Another array like the fitness one.
Lets us look at behaviour, not just fitness.

Next week.
Linear Interpolation
We might get a fractional value for some
variable

eg. After 1 time unit, x goes from 5 to 4.8
No x = 4.8 entry in the array

Approximate from surrounding values.
f(4.8) ≈ 0.2 f(4) + 0.8 f(5)
f
4.8
4
X
5
Two Dimensional
Same idea, but more complicated with two
state variables.
Python Programming
First steps
Programming

Giving instructions to a computer to carry out
calculations for you.
Why Python?



Free, widely available
Easy to read/learn
Good free documentation
Python Example 1
Code:
for i in range(3):
print i
Output:
0
1
2
Python Example 2
Code:
x=2
if x<5:
print "Yes"
else:
print "No"
Output:
Yes
Example Model #1
Patch selection from Clark, Mangel
Individual has 3 choices each day:



Visit feeding patch A (decision 0)
Visit feeding patch B (decision 1)
Visit reproductive patch (decision 2)
Variables:


t: time in 1 day increments
x: energy from 0-30.
Example Model #2
Seal foraging from Alejandro
Variables:




t: time in 10s intervals
x: energy from 1-10
t: oxygen from 1-10
h: habitat (0=haulout, 1=surface, 2=diving)
On to the code…