char *ft_strtrim(char const *s) { size_t end; size_t start; size_t i; char *ret; if (s) { i = 0; start = fstart(s); end = fend(s); if ((int)(end - start) >= 0) ret = (char *)malloc(sizeof(ret) * ((end - start) + 1)); while ((s[start] != '\0') && (start <= end)) { ret[i] = s[start]; i++; start++; } ret[i] = '\0'; if (ret == NULL) return (NULL); return (ret); } return (NULL); }
int main(int argc, const char * argv[]) { // SERIAL FARMING WRAPPER // ====================== // // ARG #1: LHS SAMPLING SIZE // ARG #2: NUMBER OF JOBS TO BE RUN // ARG #3: MONTE CARLO RUNS (in total) // if (argc<3) { cout <<endl << " ERROR in Serial Farming Wrapper ('main_SF_Calibration.cpp'): not enough arguments" << endl; exit(1); } system("date"); system("pwd"); ofstream fstart("SF_Calibration_timestamp.txt"); fstart<<"dummy"<<endl; // ============================= // === C A L I B R A T I O N === // === SERIAL FARMING === // ============================= // Parameter space sampling int nLHS = atoi(argv[1]); // Serial Farming int nJobs = atoi(argv[2]); // Monte Carlo runs for each LHS int nMC = atoi(argv[3]); string fname = _DIR_CALIB + "calib_LHS_SF_input_" ; // Healthy checks // nJobs MUST divide nMC if (! (nMC%nJobs==0)) { cout << "ERROR main_SF_Calibration.cpp: nJobs("<<nJobs <<"MUST divide nMC("<<nMC<<")"<<endl; exit(1); } unsigned int nMCperJob = nMC/nJobs; // Generate founding individuals system("Rscript generateIndividuals.R > generateIndividuals.out"); // Runs the calibration using the // parameters limits defined in the files above for (int i_job = 1; i_job <= nJobs; i_job++) { // Sharcnet command line string sharc_cmd = "sqsub -o alljobs.out -r 1d --mpp 1G ./stiagent_U " + int2string(i_job) + " " + int2string((nLHS))+ " " + int2string(nMCperJob); // Standard Unix-like command line string local_cmd = "./tphiv_U_CALIB_GPP " + int2string(i_job) + " " + int2string(nLHS)+ " " + int2string(nMCperJob) +" &"; // Choose environment string cmd_line = local_cmd; // Execute system(cmd_line.c_str()); cout << "Calibration Unit job " + int2string(i_job) + " launched" <<endl; } return 0; }
bool Pathfinder::search(const Vector2i &start, Vector2i end, NodePath &path, Map *map, const GameObject *forObj) { if (!map || start.x < 0 || start.x >= map->getMapWidth() || start.y < 0 || start.y >= map->getMapHeight() || end.x < 0 || end.x >= map->getMapWidth() || end.y < 0 || end.y >= map->getMapHeight() || (start.x == end.x && start.y == end.y)) { return false; } mMap = map; int startGroup = map->mMapData[start.x][start.y].group; int endGroup = map->mMapData[end.x][end.y].group; if(startGroup != endGroup) { mMap = NULL; return false; } if (!map->isValidGridLocation(end.x, end.y, forObj)) { // Move back towards the start position until we do find a passable location. Engine *engine = Engine::getEngine(); Vector2f fstart(start); Vector2f fend(end); Vector2f toStart(fend.sub(fstart)); int numSteps = static_cast<int>(toStart.length() * 2.0f); toStart.normalise(); toStart.scale(0.5f); for (int step = 0; step < numSteps; step++) { fend = fend.sub(toStart); Vector2i grid(fend); if (map->isValidGridLocation(grid.x, grid.y, forObj)) { break; } } end.x = round(fend.x); end.y = round(fend.y); endGroup = map->mMapData[end.x][end.y].group; if(startGroup != endGroup) { mMap = NULL; return false; } } mOpenList.clear(); mClosedList.clear(); mNodeUseCounter++; mOpenList.push_back(&map->mMapData[start.x][start.y]); AStarNode *endNode = &map->mMapData[end.x][end.y]; endNode->parent = NULL; while(!mOpenList.empty()) { AStarNode *node = mOpenList.front(); if(node->useCounter < mNodeUseCounter) { node->g = 0; node->useCounter = mNodeUseCounter; node->parent = NULL; } if (node == endNode) { // Complete getPath(node, path); mMap = NULL; return true; } else { mOpenList.erase(mOpenList.begin()); mClosedList.push_back(node); mNeighbors.clear(); getNeighbors(node->gridPosition, forObj); for(auto iter = mNeighbors.begin(); iter != mNeighbors.end(); ++iter) { AStarNode *n = *iter; if (!Utils::listContains(mOpenList, n) && !Utils::listContains(mClosedList, n)) { if (n->useCounter < mNodeUseCounter) { n->g = 0; n->useCounter = mNodeUseCounter; } n->g += node->g; n->f = n->g + manhattanDistance(n->position, endNode->position); n->parent = node; mOpenList.push_back(n); } } mNeighbors.clear(); sortAStarList(mOpenList); } } // NO PATH! D: mMap = NULL; return false; }
int main(int argc, const char * argv[]) { // ============================================== // SERIAL FARMING WRAPPER FOR MONTE CARLO RUNS // ============================================== // // Integrity checks stopif(argc<=1,"Serial Farming: not enough arguments!"); // retrieve the type of execution std::string arg1(argv[1]); bool doSingleRun = (arg1=="singlerun"?true:false); bool doScenario = (arg1=="scenario"?true:false); stopif(!doSingleRun && !doScenario, "Serial Farming: unknown execution type!"); // DEBUG informations system("date"); system("pwd"); // Clean all previous output files // (some files are incremental, so can get huge) system("./cleanout"); // simple time stamp ofstream fstart("SF_MC_timestamp.txt"); fstart<<"dummy"<<endl; // retrieve execution parameters int nMCtotal = getParameterFromFile("MCiter", _DIR_IN + "in_simulation.csv"); int nCPU = getParameterFromFile("nCPU", _DIR_IN + "in_simulation.csv"); int nMCperCPU = nMCtotal/nCPU; coutline(80); cout << "*** SINGLE SIMULATION WITH "<<nMCtotal<<" MC ITERATIONS ***"<<endl; // Mandatory code checks //CODECHECK_mandatory(); // Launch unit jobs (1 for each CPU) for (int i_job = 1; i_job <= nCPU; i_job++) { // -- for sharcnet -- // string sharc_cmd = "sqsub -o alljobs.out -r 1d --mpp 1G ./stiagent_U " // + int2string(i_job) + " " + int2string(nrows)+ " " + int2string(nMC_calibration); // Determine the type of execution string unit_name; if (doSingleRun) unit_name = "./tphiv_U_MC "; if (doScenario) unit_name = "./tphiv_U_SCEN "; // DEBUG if (doScenario) cout <<endl<<" SCENARIO MODE"<<endl; // ----- // Execution string local_cmd = unit_name + int2string(i_job) + " " + int2string(nMCperCPU) + " &"; string cmd_line = local_cmd; system(cmd_line.c_str()); cout << "Unit job " + int2string(i_job) + "/"<<nCPU<<" launched..." <<endl; } return 0; }