//--------------------------------------------------------------------------- #pragma hdrstop #include "MoveInd.h" StochasticLib1 sto1move(SEED); ofstream pathfile; bool write_paths = WRITE_PATHS; #if VCL void draw_landscape(int,int,int,int); void draw_path(int,int,double,double,int,int,int,int); #if WRITE_INDPATH const string Int2Str(const int); #endif #endif bool write_headers = 1; //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- // Functions concerned with animal movement int move_ind(Animal* pAnimal, Landscape* pLand, locn start, int startpatch, int parameterset, char* ctrlID, int maxsteps) { int step = 0; // step number int dest = -1; // set to target number when target destination reached int success = 0; locn curr,newloc,goal; //to hold current, new and goal locations bool inbounds = 1; // to indicate if move goes beyond bounds of landscape int xmax,ymax,target; Square* sq; movedata move; // distance and cost of current move double path_lgth = 0.0; // total length of path taken double path_cost = 0.0; // total cost of path taken if (write_paths && write_headers) { pathfile.open("paths.csv"); if (!pathfile.is_open()) { return 999; } pathfile<<"ControlID,Landscape,ParameterSet"; #if VCL pathfile<<",PRMethod,PR,DP,GB,MemSize"; #endif pathfile<<",Ind"; #if VCL pathfile<<",StartX,StartY,StartPatch,GoalType,GoalX,GoalY"; #endif pathfile<<",Success,Steps,PathLgth,PathCost,Target,FinalX,FinalY"<get_location(); // get boundaries of landscape xmax = pLand->get_bounds().x; ymax = pLand->get_bounds().y; locn origin = pLand->get_origin(); int cellsize = pLand->get_cellsize(); #if VCL #if WRITE_INDPATH string file = "path_" + Int2Str(pAnimal->get_id()) + ".txt"; ofstream indpath (file.c_str()); indpath<<"ID,Step,X,Y"<get_id()<<",0" <<","<<(cellsize*curr.x) + origin.x + cellsize/2 <<","<<(cellsize*curr.y) + origin.y + cellsize/2<get_pr(); double dp = pAnimal->get_dp(); pAnimal->set_dp(10.0 * dp); #endif while(step < maxsteps && dest < 0 && inbounds) { //keep moving until target reached (and not starting patch) or maxsteps moved move = pAnimal->nbrmove(pLand,step); if (move.dist < 0) { // indicates original square was NODATA // should not occur - abort the program dest = -99; inbounds = 0; // to exit the while loop #if !VCL cout<get_id()<<" at (" <get_location(); if (newloc.x<0 || newloc.x>=xmax || newloc.y<0 || newloc.y>=ymax ) { // move has taken individual out of bounds dest = -99; inbounds = 0; } step++; #if VCL // draw the move if (pAnimal->get_goaltype() == 0) draw_path(xmax,ymax,newloc.x,newloc.y,step-1,0,0,0); // black else { draw_path(xmax,ymax,newloc.x,newloc.y,step-1,255,0,0); // red } #if WRITE_INDPATH indpath<get_id()<<","<locate_square(newloc.x,newloc.y); if (sq == 0) { // moved into 'no data' area - terminate path dest = -9; inbounds = 0; } else { // increment visits to the new square sq->inc_visits(); target = sq->get_target(); #if KICKSTART if (dp > 0.0) { // still in kick-start mode if (target < 10) { // animal has left starting patch if (pr > 0) pr--; // starting patch not out of PR yet else { pAnimal->set_dp(dp); // reset animal's dp to original value dp = -999.9; // terminate kick-start mode } } } #endif if (target > 9) { if (target != startpatch) { success = 1; dest = target; } } else { } } } } // end of while loop if (write_paths) { int goaltype = pAnimal->get_goaltype(); locn goal; if (goaltype == 0) { goal.x = goal.y = 0; } else goal = pAnimal->get_goal(); locn origin = pLand->get_origin(); int cellsize = pLand->get_cellsize(); int finalx = (newloc.x * cellsize) + origin.x; int finaly = (newloc.y * cellsize) + origin.y; pathfile<get_id()<<","<get_prmethod()<<","<get_pr()<<","<get_dp() <<","<get_gb()<<","<get_memsize() #endif <<","<get_id() #if VCL <<","<<(start.x * cellsize) + origin.x<<","<<(start.y * cellsize) + origin.y <<","<