ssfile="seal-ss.txt" filename="seal-monte.txt" ################################# # Module import from math import floor from random import random # random number generator is seeded on import # parameters in param.py from param import * def prob_round(x): """ Probabalistically round x to the integer above or below For x.y, return x+1 with prob y and x with prob 1-y Use of this justified in Clark/Mangel, p. 115 """ whole = int(floor(x)) frac = x-whole if random()=haul_time: # haulout to surface x1 = prob_round(x-haul_time*moving) y1 = prob_round(yk) t1 = t+haul_time elif h==1: # at surface if d==0 and tmax-t>=haul_time: # surface to haulout x1 = prob_round(x-haul_time*moving) y1 = prob_round(yk) t1 = t+haul_time elif d==1: # stay at surface x1 = prob_round(x-basal) y1 = prob_round(y+base_o_gain+max_o_gain/(y+1)) t1 = t+1 elif d==2 and tmax-t>=dive_time: # surface to depth x1 = prob_round(x-dive_time*moving) y1 = prob_round(y-dive_time*diving_o_cost) t1 = t+dive_time elif h==2: # at depth if d==1 and tmax-t>=dive_time: # depth to surface x1 = prob_round(x-dive_time*moving) y1 = prob_round(y-dive_time*diving_o_cost) t1 = t+dive_time elif d==2: # stay at depth if rv(encounter_prob): # see some prey... if rv(catch_prob): # ...caught it x1 = prob_round(x+prey_gain-chasing) y1 = prob_round(y-chasing_o_cost) t1 = t+1 else: # ...missed it x1 = prob_round(x-chasing) y1 = prob_round(y-chasing_o_cost) t1 = t+1 else: # still looking... x1 = prob_round(x-moving) y1 = prob_round(y-diving_o_cost) t1 = t+1 x1=max(0,min(x1,xmax)) # stay in-bounds y1=max(0,min(y1,ymax)) return (x1,y1,t1) print "Loading data..." #f = fromfile(fitfile, Float, (xmax+1,ymax+1,hmax,tmax+1)) dec = fromfile(decfile, Int, (xmax+1,ymax+1,hmax,tmax+1)) data = open(filename, 'w') ssdata = open(ssfile, 'w') ssdata.write("Sim Num\tTime\tEnergy\tOxygen\tHabitat\tDecision\tEndCond\n") runs=10 for i in range(runs): x=2 y=6 h=0 t=0 last=() print "Running simulation %i..." % (i) terminal=0 while t<=tmax and not terminal: d=dec[x,y,h,t] # report any changes in its state if (x,y,h,d)!=last: data.write("Time %i, State (%i,%i), Habitat %i, Decision %i\n" % (t,x,y,h,d) ) ssdata.write( ss_line(i,t,x,y,h,d,terminal) ) last=(x,y,h,d) # possible terminal conditions if x<=0: data.write("Starved to death.\n") terminal=1 if y<0: data.write("Drowned.\n") terminal=2 if rv(1-survive_prob[h,d]): data.write("Eaten.\n") terminal=3 # figure out what happens next if not terminal: (x,y,t) = next_state(x,y,h,d,dec) # prepare for the next cycle h=d if terminal==0: # we got out alive terminal=4 data.write("Simulation ends at time %i\n\n" % (t)) ssdata.write( ss_line(i,t,x,y,h,d,terminal) ) data.close ssdata.close