def compute_phase_diagram(hunting_rate_x, hunting_rate_y, duration, start_x, start_y): ''' Compute multiple simulations with parameters taken from the arguments. Create graph that shows the simulation results one after the other. ARGUMENTS --------- hunting_rate_x: hunting rate for species 1 (small fish) hunting_rate_y: hunting rate for species 2 (predatory fish) duration: list of duration for each simulation start_x: list of start values for species 1 start_y list of start values for species 2 ''' model = EnhancedModel() #create a simulation object instance figure() #create new figure window #max_i = len(duration)-1 xmax, ymax = -1e100, -1e100 xmin, ymin = 1e100, 1e100 #do simulations with the different parameter values, #plot results into phase plane for i in range(len(duration)): model.init_hunting(hunting_rate_x, hunting_rate_y, start_x[i], start_y[i], duration[i]) model.simulateDynamic() #solve ODE res = model.getResults() #get results as a storage.DictStore object #color_tuple = cm.jet(i/max_i) plot(res['x'], res['y'], color='black', linestyle='-') #find maximum figure dimensions for quiver plot xmax = max(xmax, amax(res['x'])) ymax = max(ymax, amax(res['y'])) xmin = min(xmin, amin(res['x'])) ymin = min(ymin, amin(res['y'])) #Sample differentials at different points in phase plane, #plot field of arrows X, Y = mgrid[xmin:xmax:20j,ymin:ymax:20j] U, V = zeros(X.shape), zeros(X.shape) for i in range(X.shape[0]): for j in range(X.shape[1]): s_dt = model.dynamic(0, [X[i,j], Y[i,j]]) U[i,j],V[i,j] = s_dt[0], s_dt[1] #The axes don't have the same scale, therefore scale the arrows #TODO: this is a bad hack; future keyword 'angles' should do the trick scale_xy = (ymin-ymax)/(xmin-xmax) * 1.3 quiver(X, Y, scale_xy*U, V, pivot='center', units='inches', color='red',zorder=0) #finishing touches on plot xlabel('x - prey') ylabel('y - predators') legend() title('Predator prey with hunting - hx = %g, hy = %g' % (hunting_rate_x, hunting_rate_y))
def compute_scenario(hunting_rate_x, hunting_rate_y, duration): ''' Compute multiple simulations with parameters taken from the arguments. Create graph that shows the simulation results one after the other. ARGUMENTS --------- hunting_rate_x: list of hunting rates for species 1 (small fish) hunting_rate_y: list of hunting rates for species 2 (predatory fish) duration: list of duration for each simulation ''' model = EnhancedModel() #create a simulation object instance figure() #create new figure window max_i = len(duration)-1 x, y = 1, 0.1 cum_duration = 0 #do simulations with the different parameter values, plot results for i in range(len(duration)): model.init_hunting(hunting_rate_x[i], hunting_rate_y[i], x, y, duration[i]) model.simulateDynamic() #solve ODE res = model.getResults() #get results as a storage.DictStore object color_tuple = cm.jet(i/max_i) #label_str = 'r1=%g' % r1 #create descriptive string label_str = 'hx: %g, hy: %g' % (hunting_rate_x[i], hunting_rate_y[i]) plot(res['time'] + cum_duration, res['x'], label='x: '+label_str, color=color_tuple, linestyle=':') plot(res['time'] + cum_duration, res['y'], label='y: ', color=color_tuple, linestyle='--') plot(res['time'] + cum_duration, res['hunting_yield'], label='yield: ', color=color_tuple, linestyle='-') #start next simulation with state values from previous simulation x = res['x'][-1] y = res['y'][-1] #cumulative duration to put the simulation's plots after each other cum_duration += duration[i] #finishing touches on plot xlabel('time') ylabel('x: prey, y: predators') legend() title('Predator prey with hunting')