/////////////////////////////////////////////////////////////////////////////// // PredPreyData1.pde - Processing program to simulate predator-prey cycles // (Skeleton with data and starting point for the simulation loop) // Your job: Implement the Lotka-Volterra equations where indicated! // (c) 2013 Marie desJardins /////////////////////////////////////////////////////////////////////////////// // Variable declarations and initializations int prey=80; // initial number of predators int predators=40; // initial number of predators float b_prey=2.0; // birth rate of prey float d_prey=0.001; // death rate of prey float b_pred=0.005; // birth rate of predators float d_pred=0.6; // death rate of predators int time=0; // time starts at zero int max_cycles=200; // maximum number of time steps to simulate int max_pred=5000; // limit on the number pf predators int max_prey=5000; // limit on the number of prey int new_prey; // temp variable for new prey population each cycle int new_predators; // temp variable for new predator population each cycle int height = 500; // height of display region for graph int width = 2000; // width of display region for graph int border = 30; // additional border space on all four sides of graph float graph_x = border + (width * time / max_cycles); // initial x position (for t=0) float prey_graph_y = border + height - (height * prey / max_prey); // initial y position for prey graph float pred_graph_y = border + height - (height * predators / max_pred); // initial y position for predator graph float prev_graph_x; // temp variable for previous x position in graph float prev_pred_graph_y; // temp variable for previous y position of predator line float prev_prey_graph_y; // temp variable for previous y position of prey line color pred_color = color (255, 0, 0); // predator line is red color prey_color = color(0, 0, 255); // prey line is blue PrintWriter output = createWriter ("outfile.csv"); // open file for recording data /////////////////////////////////////////////////////////////////////////////// // Program // Initialize output file and display size (width + 2*border, height + 2*border); // Simulation loop while (time < max_cycles) { // YOUR MISSION, SHOULD YOU CHOOSE TO ACCEPT IT: SIMULATE A PREDATOR-PREY SIMULATION // YOU WILL HAVE TO: KEEP TRACK OF TIME, SET THE VALUE FOR NEW_PREY AND NEW_PREDATORS, // UPDATE THE VALUES FOR PREY AND PREDATORS, AND FINALLY PRINT OUT THE TIME, PREY, // AND PREDATOR VALUES. (HINT: YOU HAVE TO MAKE SURE THAT THE PREY AND PREDATORS ARE // ALWAYS INTEGER VALUES. USE THE "FLOOR" FUNCTION TO CHANGE A FLOAT TO AN INT!) // THE LINES BELOW ARE USED TO MAKE SURE THE PREY/PREDATOR VALUES DON'T GET TOO HIGH // OR TOO LOW -- THEY SHOUDL BE AFTER YOU COMPUTE NEW_PREY AND NEW_PREDATORS BUT BEFORE // YOU SAVE THEM BACK TO THE PREY AND PREDATORS VARIABLES AND PRINT THE VALUES. // // YOU SHOULD END UP WITH THREE LINES OF CODE BEFORE THE LINES BELOW, AND THREE // LINES OF CODE AFTER. // Make sure new prey and predator values are in the range [1, max_prey] new_prey = min (max_prey, max (1, new_prey)); new_predators = min (max_pred, max (1, new_predators)); }