00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <iostream>
00030 using namespace std;
00031
00032 #include <sbpl/headers.h>
00033 #include <sbpl_dynamic_planner/envDBubble.h>
00034 #include <sbpl_dynamic_planner/envTime.h>
00035 #include <sbpl_dynamic_planner/envInterval.h>
00036 #include <sbpl_dynamic_planner/sipp.h>
00037 #include <sbpl_dynamic_planner/weightedAStar.h>
00038 #include <sbpl_dynamic_planner/intervalPlanner.h>
00039
00040 #define PRINT_SPREAD_STATS 0
00041 #define TEMPORAL_PADDING 1
00042
00043 double halfwidth = 0;
00044 double halflength = 0;
00045 double allocated_time_secs = 300.0;
00046 bool bsearchuntilfirstsolution = true;
00047 double initialEps = 100.0;
00048 double dec_eps = 1.0;
00049 bool bforwardsearch = true;
00050 double timeRes = 0.1;
00051
00052 int getNumDynObsCollisions(vector<SBPL_DynamicObstacle_t> dynObs, double px, double py, int pt){
00053 int collisions = 0;
00054 for(unsigned int i=0; i < dynObs.size(); i++){
00055 SBPL_DynamicObstacle_t obs = dynObs.at(i);
00056 for(unsigned int j=0; j < obs.trajectories.size(); j++){
00057 SBPL_Trajectory_t traj = obs.trajectories.at(j);
00058 unsigned int k = 0;
00059 for(; k < traj.points.size(); k++){
00060 if(traj.points.at(k).t > pt)
00061 break;
00062 }
00063
00064 if(k==0){
00065
00066
00067
00068 continue;
00069 }
00070 else if(k == traj.points.size()){
00071
00072
00073
00074 if(!traj.existsAfter)
00075 continue;
00076
00077 SBPL_Traj_Pt_t p = traj.points.at(k-1);
00078 double dist = (p.x-px)*(p.x-px) + (p.y-py)*(p.y-py);
00079 if(sqrt(dist) <= obs.radius + 3*p.std_dev){
00080 printf("collide rad=%f from k=%d at (%d %d %d)\n", obs.radius, k, CONTXY2DISC(traj.points.at(k).x, 0.025), CONTXY2DISC(traj.points.at(k).y, 0.025), traj.points.at(k).t);
00081 collisions++;
00082 }
00083 }
00084 else{
00085
00086 bool hit = false;
00087 SBPL_Traj_Pt_t p1, p2;
00088 p1 = traj.points.at(k-1);
00089 double dist = (p1.x-px)*(p1.x-px) + (p1.y-py)*(p1.y-py);
00090 if(abs(traj.points.at(k-1).t-pt) <= TEMPORAL_PADDING && sqrt(dist) <= obs.radius + 3*p1.std_dev)
00091 hit = true;
00092 p2 = traj.points.at(k);
00093 dist = (p2.x-px)*(p2.x-px) + (p2.y-py)*(p2.y-py);
00094 if(abs(traj.points.at(k).t-pt) <= TEMPORAL_PADDING && sqrt(dist) <= obs.radius + 3*p2.std_dev)
00095 hit = true;
00096
00097 if(hit){
00098 printf("collide rad=%f from k=%d at (%d %d %d), robot at (%d %d %d)\n",
00099 obs.radius, k,
00100 CONTXY2DISC(traj.points.at(k).x, 0.025),
00101 CONTXY2DISC(traj.points.at(k).y, 0.025),
00102 traj.points.at(k).t,
00103 CONTXY2DISC(px, 0.025),
00104 CONTXY2DISC(py, 0.025),
00105 pt);
00106 collisions++;
00107 }
00108 }
00109
00110 }
00111 }
00112 return collisions;
00113 }
00114
00115
00116 vector<SBPL_DynamicObstacle_t> ReadDynamicObstacles(char* dynObs_filename){
00117 FILE* fDynObs = fopen(dynObs_filename, "r");
00118 char sTemp[1024], sTemp1[1024];
00119 int iTemp;
00120 vector<SBPL_DynamicObstacle_t> dynamicObstacles;
00121
00122
00123
00124
00125 if(fscanf(fDynObs, "%s", sTemp) != 1){
00126 printf("ERROR: ran out of dynamic obstacle file early\n");
00127 exit(1);
00128 }
00129 strcpy(sTemp1, "NumberOfDynamicObstacles:");
00130 if(strcmp(sTemp1, sTemp) != 0){
00131 printf("ERROR: dynamic obstacle file has incorrect format\n");
00132 printf("Expected %s got %s\n", sTemp1, sTemp);
00133 exit(1);
00134 }
00135 if(fscanf(fDynObs, "%s", sTemp) != 1){
00136 printf("ERROR: ran out of dynamic obstacle file early\n");
00137 exit(1);
00138 }
00139 int numObs = atoi(sTemp);
00140
00141
00142 for(int i=0; i < numObs; i++){
00143
00144
00145 if(fscanf(fDynObs, "%s", sTemp) != 1){
00146 printf("ERROR: ran out of dynamic obstacle file early\n");
00147 exit(1);
00148 }
00149 strcpy(sTemp1, "DynamicObstacleID:");
00150 if(strcmp(sTemp1, sTemp) != 0){
00151 printf("ERROR: dynamic obstacle file has incorrect format\n");
00152 printf("Expected %s got %s\n", sTemp1, sTemp);
00153 exit(1);
00154 }
00155 if(fscanf(fDynObs, "%s", sTemp) != 1){
00156 printf("ERROR: ran out of dynamic obstacle file early\n");
00157 exit(1);
00158 }
00159 iTemp = atoi(sTemp);
00160 if(iTemp != i){
00161 printf("ERROR: dynamic obstacle file has incorrect format\n");
00162 printf("Expected %d got %d\n", i, iTemp);
00163 exit(1);
00164 }
00165 SBPL_DynamicObstacle_t obs;
00166
00167
00168 if(fscanf(fDynObs, "%s", sTemp) != 1){
00169 printf("ERROR: ran out of dynamic obstacle file early\n");
00170 exit(1);
00171 }
00172 strcpy(sTemp1, "ObstacleRadius:");
00173 if(strcmp(sTemp1, sTemp) != 0){
00174 printf("ERROR: dynamic obstacle file has incorrect format\n");
00175 printf("Expected %s got %s\n", sTemp1, sTemp);
00176 exit(1);
00177 }
00178 if(fscanf(fDynObs, "%s", sTemp) != 1){
00179 printf("ERROR: ran out of dynamic obstacle file early\n");
00180 exit(1);
00181 }
00182
00183 obs.radius = atof(sTemp) + sqrt(halfwidth*halfwidth + halflength*halflength);
00184
00185
00186 if(fscanf(fDynObs, "%s", sTemp) != 1){
00187 printf("ERROR: ran out of dynamic obstacle file early\n");
00188 exit(1);
00189 }
00190 strcpy(sTemp1, "NumberOfTrajectories:");
00191 if(strcmp(sTemp1, sTemp) != 0){
00192 printf("ERROR: dynamic obstacle file has incorrect format\n");
00193 printf("Expected %s got %s\n", sTemp1, sTemp);
00194 exit(1);
00195 }
00196 if(fscanf(fDynObs, "%s", sTemp) != 1){
00197 printf("ERROR: ran out of dynamic obstacle file early\n");
00198 exit(1);
00199 }
00200 int numTraj = atoi(sTemp);
00201
00202
00203 double trajProbSum = 0;
00204 for(int j=0; j < numTraj; j++){
00205
00206
00207 if(fscanf(fDynObs, "%s", sTemp) != 1){
00208 printf("ERROR: ran out of dynamic obstacle file early\n");
00209 exit(1);
00210 }
00211 strcpy(sTemp1, "TrajectoryID:");
00212 if(strcmp(sTemp1, sTemp) != 0){
00213 printf("ERROR: dynamic obstacle file has incorrect format\n");
00214 printf("Expected %s got %s\n", sTemp1, sTemp);
00215 exit(1);
00216 }
00217 if(fscanf(fDynObs, "%s", sTemp) != 1){
00218 printf("ERROR: ran out of dynamic obstacle file early\n");
00219 exit(1);
00220 }
00221 iTemp = atoi(sTemp);
00222 if(iTemp != j){
00223 printf("ERROR: dynamic obstacle file has incorrect format\n");
00224 printf("Expected %d got %d\n", j, iTemp);
00225 exit(1);
00226 }
00227 SBPL_Trajectory_t traj;
00228
00229
00230 if(fscanf(fDynObs, "%s", sTemp) != 1){
00231 printf("ERROR: ran out of dynamic obstacle file early\n");
00232 exit(1);
00233 }
00234 strcpy(sTemp1, "TrajectoryProbability:");
00235 if(strcmp(sTemp1, sTemp) != 0){
00236 printf("ERROR: dynamic obstacle file has incorrect format\n");
00237 printf("Expected %s got %s\n", sTemp1, sTemp);
00238 exit(1);
00239 }
00240 if(fscanf(fDynObs, "%s", sTemp) != 1){
00241 printf("ERROR: ran out of dynamic obstacle file early\n");
00242 exit(1);
00243 }
00244 traj.prob = atof(sTemp);
00245 if(traj.prob < 0 || traj.prob > 1){
00246 printf("ERROR: dynamic obstacle file has incorrect format\n");
00247 printf("Expected TrajectoryProbability on the interval [0,1] but got %f\n", traj.prob);
00248 exit(1);
00249 }
00250 trajProbSum += traj.prob;
00251
00252
00253 if(fscanf(fDynObs, "%s", sTemp) != 1){
00254 printf("ERROR: ran out of dynamic obstacle file early\n");
00255 exit(1);
00256 }
00257 strcpy(sTemp1, "NumberOfPoints:");
00258 if(strcmp(sTemp1, sTemp) != 0){
00259 printf("ERROR: dynamic obstacle file has incorrect format\n");
00260 printf("Expected %s got %s\n", sTemp1, sTemp);
00261 exit(1);
00262 }
00263 if(fscanf(fDynObs, "%s", sTemp) != 1){
00264 printf("ERROR: ran out of dynamic obstacle file early\n");
00265 exit(1);
00266 }
00267 int numPoints = atoi(sTemp);
00268
00269
00270 int prev_t = 0;
00271 for(int k=0; k < numPoints; k++){
00272
00273 SBPL_Traj_Pt_t pt;
00274 if(fscanf(fDynObs, "%s", sTemp) != 1){
00275 printf("ERROR: ran out of dynamic obstacle file early\n");
00276 exit(1);
00277 }
00278 pt.x = atof(sTemp);
00279 if(fscanf(fDynObs, "%s", sTemp) != 1){
00280 printf("ERROR: ran out of dynamic obstacle file early\n");
00281 exit(1);
00282 }
00283 pt.y = atof(sTemp);
00284 if(fscanf(fDynObs, "%s", sTemp) != 1){
00285 printf("ERROR: ran out of dynamic obstacle file early\n");
00286 exit(1);
00287 }
00288
00289 pt.t = CONTTIME2DISC(atof(sTemp),timeRes);
00290
00291 if(prev_t > pt.t && k != 0){
00292 printf("ERROR: dynamic obstacle file has incorrect format\n");
00293 printf("dynamic obstacle trajectory times can't decrease!\n");
00294 exit(1);
00295 }
00296 prev_t = pt.t;
00297
00298 if(fscanf(fDynObs, "%s", sTemp) != 1){
00299 printf("ERROR: ran out of dynamic obstacle file early\n");
00300 exit(1);
00301 }
00302 pt.std_dev = atof(sTemp);
00303
00304
00305 traj.points.push_back(pt);
00306 }
00307
00308
00309
00310 if(fscanf(fDynObs, "%s", sTemp) != 1){
00311 printf("ERROR: ran out of dynamic obstacle file early\n");
00312 exit(1);
00313 }
00314 strcpy(sTemp1, "ObstacleExistsAfterTrajectory:");
00315 if(strcmp(sTemp1, sTemp) != 0){
00316 printf("ERROR: dynamic obstacle file has incorrect format\n");
00317 printf("Expected %s got %s\n", sTemp1, sTemp);
00318 exit(1);
00319 }
00320 if(fscanf(fDynObs, "%s", sTemp) != 1){
00321 printf("ERROR: ran out of dynamic obstacle file early\n");
00322 exit(1);
00323 }
00324 traj.existsAfter = atoi(sTemp);
00325 if(traj.existsAfter != 0 && traj.existsAfter != 1){
00326 printf("ERROR: dynamic obstacle file has incorrect format\n");
00327 printf("ObstacleExistsAfterTrajectory is a boolean and needs to be 0 or 1\n");
00328 exit(1);
00329 }
00330
00331
00332 obs.trajectories.push_back(traj);
00333 }
00334
00335
00336 if(fabs(trajProbSum - 1.0) > ERR_EPS){
00337 printf("ERROR: dynamic obstacle file has incorrect format\n");
00338 printf("Probabilities for trajectories of dynamic obstacle %d sum to %f instead of 1\n", i, trajProbSum);
00339 exit(1);
00340 }
00341
00342
00343 dynamicObstacles.push_back(obs);
00344
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361 fclose(fDynObs);
00362 return dynamicObstacles;
00363 }
00364
00365
00366 int planxythetatimelat(char* env_filename, char* mprim_filename, vector<SBPL_DynamicObstacle_t> dynObs, vector< vector<double> >& stats, int test_idx){
00367
00368 int bRet = 0;
00369
00370
00371 vector<sbpl_2Dpt_t> perimeterptsV;
00372 perimeterptsV.clear();
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 int eps_idx = stats.size()-1;
00397 for(double eps=initialEps; eps>0.999; eps -= dec_eps){
00398 printf("eps = %f\n", eps);
00399
00400
00401 EnvironmentNAVXYTHETATIMELAT environment_navxythetatimelat;
00402
00403 if(!environment_navxythetatimelat.InitializeEnv(env_filename, perimeterptsV, mprim_filename)){
00404 printf("ERROR: InitializeEnv failed\n");
00405 exit(1);
00406 }
00407 if(!environment_navxythetatimelat.setDynamicObstacles(dynObs, false)){
00408 printf("The start state is in collision!\nSolution does not exist\n\n");
00409 stats[eps_idx][4] = false;
00410 eps_idx--;
00411 continue;
00412 }
00413
00414
00415 vector<int> solution_stateIDs_V;
00416 WeightedAStar planner(&environment_navxythetatimelat, bforwardsearch);
00417
00418 planner.set_initialsolution_eps(eps);
00419
00420
00421 planner.set_search_mode(bsearchuntilfirstsolution);
00422
00423 if(planner.set_start(environment_navxythetatimelat.getStartID()) == 0){
00424 printf("ERROR: failed to set start state\n");
00425 exit(1);
00426 }
00427 if(planner.set_goal(environment_navxythetatimelat.getGoalID()) == 0){
00428 printf("ERROR: failed to set goal state\n");
00429 exit(1);
00430 }
00431
00432
00433 printf("start planning...\n");
00434 try{
00435 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00436 }
00437 catch(SBPL_Exception* e){
00438 printf("EXCEPTION: in XYTHETATIME!\n");
00439 for(;eps_idx >= 0; eps_idx--){
00440 stats[eps_idx][0] = -1;
00441 stats[eps_idx][1] = -1;
00442 stats[eps_idx][2] = -1;
00443 stats[eps_idx][3] = -1;
00444 stats[eps_idx][4] = 0;
00445 }
00446 return 0;
00447 }
00448 printf("done planning\n");
00449 std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00450
00451 environment_navxythetatimelat.PrintTimeStat(stdout);
00452
00453 vector<SBPL_4Dpt_t> xythetatimePath;
00454 environment_navxythetatimelat.ConvertStateIDPathintoXYThetaPath(&solution_stateIDs_V, &xythetatimePath);
00455 printf("solution size=%d\n", (int)xythetatimePath.size());
00456
00457
00458
00459
00460
00461
00462
00463 environment_navxythetatimelat.PrintTimeStat(stdout);
00464 int collisions = 0;
00465 for(unsigned int pind=0; pind<xythetatimePath.size(); pind++){
00466 SBPL_4Dpt_t pt = xythetatimePath[pind];
00467 collisions += getNumDynObsCollisions(dynObs,
00468 DISCXY2CONT(CONTXY2DISC(pt.x, 0.025), 0.025),
00469 DISCXY2CONT(CONTXY2DISC(pt.y, 0.025), 0.025),
00470 CONTTIME2DISC(pt.t,timeRes));
00471 }
00472 printf("Number of collisions with dynamic obstacles = %d\n", collisions);
00473
00474 if(bRet)
00475 printf("Solution is found\n");
00476 else
00477 printf("Solution does not exist\n");
00478
00479 printf("\n");
00480 fflush(NULL);
00481
00482 bool solutionFound;
00483 int numExpands;
00484 int solutionCost;
00485 double searchTime;
00486 planner.getSearchStats(&solutionFound, &numExpands, &solutionCost, &searchTime);
00487 stats[eps_idx][0] = numExpands;
00488 stats[eps_idx][1] = solutionCost;
00489 stats[eps_idx][2] = searchTime;
00490 stats[eps_idx][3] = collisions;
00491 stats[eps_idx][4] = solutionFound;
00492 eps_idx--;
00493 }
00494
00495 return bRet;
00496 }
00497
00498 int planxythetatimebubblelat(char* env_filename, char* mprim_filename, vector<SBPL_DynamicObstacle_t> dynObs, vector< vector<double> >& stats, int test_idx){
00499
00500 int bRet = 0;
00501
00502
00503 vector<sbpl_2Dpt_t> perimeterptsV;
00504 perimeterptsV.clear();
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528 int eps_idx = stats.size()-1;
00529 for(double eps=initialEps; eps>0.999; eps -= dec_eps){
00530 printf("eps = %f\n", eps);
00531
00532
00533 EnvDBubbleLat environment_navxythetatimebubblelat;
00534
00535 if(!environment_navxythetatimebubblelat.InitializeEnv(env_filename, perimeterptsV, mprim_filename)){
00536 printf("ERROR: InitializeEnv failed\n");
00537 exit(1);
00538 }
00539 environment_navxythetatimebubblelat.setDynamicObstacles(dynObs);
00540
00541
00542 vector<int> solution_stateIDs_V;
00543 WeightedAStar planner(&environment_navxythetatimebubblelat, bforwardsearch);
00544
00545 planner.set_initialsolution_eps(eps);
00546
00547
00548 planner.set_search_mode(bsearchuntilfirstsolution);
00549
00550 if(planner.set_start(environment_navxythetatimebubblelat.getStartID()) == 0){
00551 printf("ERROR: failed to set start state\n");
00552 exit(1);
00553 }
00554 if(planner.set_goal(environment_navxythetatimebubblelat.getGoalID()) == 0){
00555 printf("ERROR: failed to set goal state\n");
00556 exit(1);
00557 }
00558
00559 printf("start planning...\n");
00560 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00561 printf("done planning\n");
00562 std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00563
00564 environment_navxythetatimebubblelat.PrintTimeStat(stdout);
00565
00566 vector<SBPL_4Dpt_t> xythetatimePath;
00567 environment_navxythetatimebubblelat.ConvertStateIDPathintoXYThetaPath(&solution_stateIDs_V, &xythetatimePath);
00568
00569
00570
00571
00572
00573
00574
00575
00576 environment_navxythetatimebubblelat.PrintTimeStat(stdout);
00577 int collisions = 0;
00578 for(unsigned int pind=0; pind<xythetatimePath.size(); pind++){
00579 SBPL_4Dpt_t pt = xythetatimePath[pind];
00580 collisions += getNumDynObsCollisions(dynObs,
00581 DISCXY2CONT(CONTXY2DISC(pt.x, 0.025), 0.025),
00582 DISCXY2CONT(CONTXY2DISC(pt.y, 0.025), 0.025),
00583 CONTTIME2DISC(pt.t,timeRes));
00584 }
00585 printf("Number of collisions with dynamic obstacles = %d\n", collisions);
00586
00587 if(bRet)
00588 printf("Solution is found\n");
00589 else
00590 printf("Solution does not exist\n");
00591
00592 printf("\n");
00593 fflush(NULL);
00594
00595
00596
00597 bool solutionFound;
00598 int numExpands;
00599 int solutionCost;
00600 double searchTime;
00601 planner.getSearchStats(&solutionFound, &numExpands, &solutionCost, &searchTime);
00602 stats[eps_idx][0] = numExpands;
00603 stats[eps_idx][1] = solutionCost;
00604 stats[eps_idx][2] = searchTime;
00605 stats[eps_idx][3] = collisions;
00606 stats[eps_idx][4] = solutionFound;
00607 eps_idx--;
00608 }
00609
00610 return bRet;
00611 }
00612
00613
00614 int plansipplat(char* env_filename, char* mprim_filename, vector<SBPL_DynamicObstacle_t> dynObs, vector< vector<double> >& stats, int test_idx){
00615
00616 int bRet = 0;
00617
00618
00619 vector<sbpl_2Dpt_t> perimeterptsV;
00620 perimeterptsV.clear();
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 int eps_idx = stats.size()-1;
00645 for(double eps=initialEps; eps>0.999; eps -= dec_eps){
00646 printf("eps = %f\n", eps);
00647
00648
00649 EnvSIPPLat environment_intervallat;
00650
00651 if(!environment_intervallat.InitializeEnv(env_filename, perimeterptsV, mprim_filename)){
00652 printf("ERROR: InitializeEnv failed\n");
00653 exit(1);
00654 }
00655 if(!environment_intervallat.setDynamicObstacles(dynObs, false)){
00656 printf("The start state is in collision!\nSolution does not exist\n\n");
00657 stats[eps_idx][4] = false;
00658 eps_idx--;
00659 continue;
00660 }
00661
00662
00663 vector<int> solution_stateIDs_V;
00664 WeightedAStar planner(&environment_intervallat, bforwardsearch);
00665
00666 planner.set_initialsolution_eps(eps);
00667
00668
00669 planner.set_search_mode(bsearchuntilfirstsolution);
00670
00671 if(planner.set_start(environment_intervallat.getStartID()) == 0){
00672 printf("ERROR: failed to set start state\n");
00673 exit(1);
00674 }
00675 if(planner.set_goal(environment_intervallat.getGoalID()) == 0){
00676 printf("ERROR: failed to set goal state\n");
00677 exit(1);
00678 }
00679
00680 printf("start planning...\n");
00681 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00682 printf("done planning\n");
00683 std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00684
00685 environment_intervallat.PrintTimeStat(stdout);
00686
00687 char buf[32];
00688 sprintf(buf, "temp/solutions/sol%.4d.txt", test_idx);
00689 FILE* fSol = fopen(buf, "w");
00690 vector<SBPL_4Dpt_t> xythetatimePath;
00691 environment_intervallat.ConvertStateIDPathintoXYThetaPath(&solution_stateIDs_V, &xythetatimePath);
00692 printf("solution size=%d\n", (int)xythetatimePath.size());
00693 for(unsigned int i = 0; i < xythetatimePath.size(); i++){
00694 fprintf(fSol, "%.3f %.3f %.3f %.3f\n", xythetatimePath.at(i).x, xythetatimePath.at(i).y, xythetatimePath.at(i).theta, xythetatimePath.at(i).t);
00695 }
00696 fclose(fSol);
00697
00698 environment_intervallat.PrintTimeStat(stdout);
00699 int collisions = 0;
00700 for(unsigned int pind=0; pind<xythetatimePath.size(); pind++){
00701 SBPL_4Dpt_t pt = xythetatimePath[pind];
00702 int temp = getNumDynObsCollisions(dynObs,
00703 DISCXY2CONT(CONTXY2DISC(pt.x, 0.025), 0.025),
00704 DISCXY2CONT(CONTXY2DISC(pt.y, 0.025), 0.025),
00705 CONTTIME2DISC(pt.t,timeRes));
00706 if(temp > 0){
00707 printf("id=%d\n",environment_intervallat.GetStateFromCoord(CONTXY2DISC(pt.x, 0.025), CONTXY2DISC(pt.x, 0.025), ContTheta2Disc(pt.theta, ENVINTERVALLAT_THETADIRS), CONTTIME2DISC(pt.t,timeRes)));
00708 }
00709 collisions += temp;
00710
00711
00712 }
00713 printf("Number of collisions with dynamic obstacles = %d\n", collisions);
00714
00715
00716 if(bRet)
00717 printf("Solution is found\n");
00718 else
00719 printf("Solution does not exist\n");
00720
00721 printf("\n");
00722 fflush(NULL);
00723
00724
00725
00726 bool solutionFound;
00727 int numExpands;
00728 int solutionCost;
00729 double searchTime;
00730 planner.getSearchStats(&solutionFound, &numExpands, &solutionCost, &searchTime);
00731 stats[eps_idx][0] = numExpands;
00732 stats[eps_idx][1] = solutionCost;
00733 stats[eps_idx][2] = searchTime;
00734 stats[eps_idx][3] = collisions;
00735 stats[eps_idx][4] = solutionFound;
00736 eps_idx--;
00737 }
00738
00739 return bRet;
00740 }
00741
00742 int planweightedinterval(char* env_filename, char* mprim_filename, vector<SBPL_DynamicObstacle_t> dynObs, vector< vector<double> >& stats, int test_idx){
00743
00744 int bRet = 0;
00745
00746
00747 vector<sbpl_2Dpt_t> perimeterptsV;
00748 perimeterptsV.clear();
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772 int eps_idx = stats.size()-1;
00773 for(double eps=initialEps; eps>0.999; eps -= dec_eps){
00774 printf("eps = %f\n", eps);
00775
00776
00777 EnvIntervalLat environment_intervallat;
00778
00779 if(!environment_intervallat.InitializeEnv(env_filename, perimeterptsV, mprim_filename)){
00780 printf("ERROR: InitializeEnv failed\n");
00781 exit(1);
00782 }
00783 if(!environment_intervallat.setDynamicObstacles(dynObs, false)){
00784 printf("The start state is in collision!\nSolution does not exist\n\n");
00785 stats[eps_idx][4] = false;
00786 eps_idx--;
00787 continue;
00788 }
00789
00790
00791 vector<int> solution_stateIDs_V;
00792 IntervalPlanner planner(&environment_intervallat);
00793
00794 planner.set_initialsolution_eps(eps);
00795
00796
00797 planner.set_search_mode(bsearchuntilfirstsolution);
00798
00799 if(planner.set_start(environment_intervallat.getStartID()) == 0){
00800 printf("ERROR: failed to set start state\n");
00801 exit(1);
00802 }
00803 if(planner.set_goal(environment_intervallat.getGoalID()) == 0){
00804 printf("ERROR: failed to set goal state\n");
00805 exit(1);
00806 }
00807
00808 printf("start planning...\n");
00809 try{
00810 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00811 }
00812 catch(SBPL_Exception* e){
00813 printf("EXCEPTION: in INTERVAL PLANNER!\n");
00814 for(;eps_idx >= 0; eps_idx--){
00815 stats[eps_idx][0] = -1;
00816 stats[eps_idx][1] = -1;
00817 stats[eps_idx][2] = -1;
00818 stats[eps_idx][3] = -1;
00819 stats[eps_idx][4] = 0;
00820 }
00821 return 0;
00822 }
00823 printf("done planning\n");
00824 std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00825
00826 environment_intervallat.PrintTimeStat(stdout);
00827
00828 char buf[32];
00829 sprintf(buf, "temp/solutions/sol%.4d.txt", test_idx);
00830 FILE* fSol = fopen(buf, "w");
00831 vector<SBPL_4Dpt_t> xythetatimePath;
00832 environment_intervallat.ConvertStateIDPathintoXYThetaPath(&solution_stateIDs_V, &xythetatimePath);
00833 printf("solution size=%d\n", (int)xythetatimePath.size());
00834 for(unsigned int i = 0; i < xythetatimePath.size(); i++){
00835 fprintf(fSol, "%.3f %.3f %.3f %.3f\n", xythetatimePath.at(i).x, xythetatimePath.at(i).y, xythetatimePath.at(i).theta, xythetatimePath.at(i).t);
00836 }
00837 fclose(fSol);
00838
00839 environment_intervallat.PrintTimeStat(stdout);
00840 int collisions = 0;
00841 for(unsigned int pind=0; pind<xythetatimePath.size(); pind++){
00842 SBPL_4Dpt_t pt = xythetatimePath[pind];
00843 int temp = getNumDynObsCollisions(dynObs,
00844 DISCXY2CONT(CONTXY2DISC(pt.x, 0.025), 0.025),
00845 DISCXY2CONT(CONTXY2DISC(pt.y, 0.025), 0.025),
00846 CONTTIME2DISC(pt.t,timeRes));
00847 if(temp > 0){
00848 printf("id=%d\n",environment_intervallat.GetStateFromCoord(CONTXY2DISC(pt.x, 0.025), CONTXY2DISC(pt.x, 0.025), ContTheta2Disc(pt.theta, ENVINTERVALLAT_THETADIRS), CONTTIME2DISC(pt.t,timeRes)));
00849 }
00850 collisions += temp;
00851
00852
00853 }
00854 printf("Number of collisions with dynamic obstacles = %d\n", collisions);
00855
00856
00857 if(bRet)
00858 printf("Solution is found\n");
00859 else
00860 printf("Solution does not exist\n");
00861
00862 printf("\n");
00863 fflush(NULL);
00864
00865
00866
00867 bool solutionFound;
00868 vector<int> numExpands;
00869 vector<int> solutionCost;
00870 vector<double> searchTime;
00871 vector<double> searchEps;
00872 planner.getSearchStats(&solutionFound, &numExpands, &solutionCost, &searchTime, &searchEps);
00873 if(numExpands.empty()){
00874 stats[eps_idx][0] = -1;
00875 stats[eps_idx][1] = -1;
00876 stats[eps_idx][2] = -1;
00877 stats[eps_idx][3] = collisions;
00878 stats[eps_idx][4] = solutionFound;
00879 }
00880 else{
00881 stats[eps_idx][0] = numExpands[0];
00882 stats[eps_idx][1] = solutionCost[0];
00883 stats[eps_idx][2] = searchTime[0];
00884 stats[eps_idx][3] = collisions;
00885 stats[eps_idx][4] = solutionFound;
00886 }
00887 eps_idx--;
00888 }
00889
00890 return bRet;
00891 }
00892
00893 int planintervallat(char* env_filename, char* mprim_filename, vector<SBPL_DynamicObstacle_t> dynObs, vector< vector<double> >& stats, int test_idx){
00894
00895 int bRet = 0;
00896
00897
00898 vector<sbpl_2Dpt_t> perimeterptsV;
00899 perimeterptsV.clear();
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924 EnvIntervalLat environment_intervallat;
00925
00926 if(!environment_intervallat.InitializeEnv(env_filename, perimeterptsV, mprim_filename)){
00927 printf("ERROR: InitializeEnv failed\n");
00928 exit(1);
00929 }
00930 if(!environment_intervallat.setDynamicObstacles(dynObs, false)){
00931 printf("The start state is in collision!\nSolution does not exist\n\n");
00932 for(unsigned int i=0; i<stats.size(); i++)
00933 stats[i][4] = false;
00934
00935 return 0;
00936 }
00937
00938
00939 vector<int> solution_stateIDs_V;
00940 IntervalPlanner planner(&environment_intervallat);
00941
00942 planner.set_initialsolution_eps(initialEps);
00943
00944
00945 planner.set_search_mode(bsearchuntilfirstsolution);
00946
00947 if(planner.set_start(environment_intervallat.getStartID()) == 0){
00948 printf("ERROR: failed to set start state\n");
00949 exit(1);
00950 }
00951 if(planner.set_goal(environment_intervallat.getGoalID()) == 0){
00952 printf("ERROR: failed to set goal state\n");
00953 exit(1);
00954 }
00955
00956 printf("start planning...\n");
00957 bRet = planner.replan(allocated_time_secs, &solution_stateIDs_V);
00958 printf("done planning\n");
00959 std::cout << "size of solution=" << solution_stateIDs_V.size() << std::endl;
00960
00961 environment_intervallat.PrintTimeStat(stdout);
00962
00963 char buf[32];
00964 sprintf(buf, "temp/solutions/sol%.4d.txt", test_idx);
00965 FILE* fSol = fopen(buf, "w");
00966 vector<SBPL_4Dpt_t> xythetatimePath;
00967 environment_intervallat.ConvertStateIDPathintoXYThetaPath(&solution_stateIDs_V, &xythetatimePath);
00968 printf("solution size=%d\n", (int)xythetatimePath.size());
00969 for(unsigned int i = 0; i < xythetatimePath.size(); i++){
00970 fprintf(fSol, "%.3f %.3f %.3f %.3f\n", xythetatimePath.at(i).x, xythetatimePath.at(i).y, xythetatimePath.at(i).theta, xythetatimePath.at(i).t);
00971 }
00972 fclose(fSol);
00973
00974 environment_intervallat.PrintTimeStat(stdout);
00975 int collisions = 0;
00976 for(unsigned int pind=0; pind<xythetatimePath.size(); pind++){
00977 SBPL_4Dpt_t pt = xythetatimePath[pind];
00978 int temp = getNumDynObsCollisions(dynObs,
00979 DISCXY2CONT(CONTXY2DISC(pt.x, 0.025), 0.025),
00980 DISCXY2CONT(CONTXY2DISC(pt.y, 0.025), 0.025),
00981 CONTTIME2DISC(pt.t,timeRes));
00982 if(temp > 0){
00983 printf("id=%d\n",environment_intervallat.GetStateFromCoord(CONTXY2DISC(pt.x, 0.025), CONTXY2DISC(pt.x, 0.025), ContTheta2Disc(pt.theta, ENVINTERVALLAT_THETADIRS), CONTTIME2DISC(pt.t,timeRes)));
00984 }
00985 collisions += temp;
00986
00987
00988 }
00989 printf("Number of collisions with dynamic obstacles = %d\n", collisions);
00990
00991
00992 if(bRet)
00993 printf("Solution is found\n");
00994 else
00995 printf("Solution does not exist\n");
00996
00997 printf("\n");
00998 fflush(NULL);
00999
01000
01001
01002 bool solutionFound;
01003 vector<int> numExpands;
01004 vector<int> solutionCost;
01005 vector<double> searchTime;
01006 vector<double> searchEps;
01007 planner.getSearchStats(&solutionFound, &numExpands, &solutionCost, &searchTime, &searchEps);
01008 int s=stats.size()-1;
01009 int i;
01010 for(i=0; i<(int)searchEps.size(); i++){
01011 stats[s-i][0] = numExpands[i];
01012 stats[s-i][1] = solutionCost[i];
01013 stats[s-i][2] = searchTime[i];
01014 stats[s-i][3] = collisions;
01015 stats[s-i][4] = solutionFound;
01016 }
01017 for(; s-i>=0; i++)
01018 stats[s-i][4] = false;
01019
01020 return bRet;
01021 }
01022
01023 void computeStats(vector< vector< vector<int> > > expands,
01024 vector< vector< vector<int> > > g,
01025 vector< vector< vector<double> > > time,
01026 vector< vector< vector<int> > > collisions,
01027 vector< vector< vector<int> > > solution_found,
01028 vector<char*> name){
01029
01030 printf("\n\n\n\nComputing statistics...\n");
01031
01032 if(solution_found.empty() || solution_found[0].empty() || solution_found[0][0].empty()){
01033 printf("No tests were run!\n");
01034 exit(1);
01035 }
01036
01037 int numPlanners = solution_found.size();
01038 int numEps = solution_found[0].size();
01039 int numTests = solution_found[0][0].size();
01040
01041 vector< vector<bool> > allFound;
01042 allFound.resize(numEps);
01043 for(int j=0; j<numEps; j++){
01044 allFound[j].resize(numTests);
01045 for(int k=0; k<numTests; k++){
01046 bool solFound = true;
01047 for(int i=0; i<numPlanners; i++){
01048 solFound &= solution_found[i][j][k];
01049 }
01050 allFound[j][k] = solFound;
01051 }
01052 }
01053
01054 printf("%d tests were run on the following planners\n", numTests);
01055 for(int m=0; m<2; m++){
01056 if(m==0)
01057 printf("\n\nStats computed for each planner individually\n");
01058 else
01059 printf("\n\nStats computed only for paths that all planners found\n");
01060
01061 for(int i=0; i<numPlanners; i++){
01062 printf("\n%s\n",name[i]);
01063
01064 for(int j=numEps-1; j>=0; j--){
01065 double mean_expands = 0;
01066 double mean_g = 0;
01067 double mean_time = 0;
01068 double mean_collisions = 0;
01069 int total_sol_found = 0;
01070 for(int k=0; k<numTests; k++){
01071 if((m==0 && solution_found[i][j][k] == 1) ||
01072 (m==1 && allFound[j][k])){
01073 mean_expands += expands[i][j][k];
01074 mean_g += g[i][j][k];
01075 mean_time += time[i][j][k];
01076 mean_collisions += (collisions[i][j][k] > 0 ? 1 : 0);
01077 total_sol_found += solution_found[i][j][k];
01078 }
01079 }
01080 mean_expands /= total_sol_found;
01081 mean_g /= total_sol_found;
01082 mean_time /= total_sol_found;
01083 mean_collisions /= total_sol_found;
01084 printf("eps=%f avg_expands=%f avg_g=%f avg_time=%f avg_collisions=%f solutionsFound=%d\n", (j*dec_eps)+1.0,
01085 mean_expands, mean_g, mean_time, mean_collisions, total_sol_found);
01086
01087 #if PRINT_SPREAD_STATS
01088 double std_dev_expands = 0;
01089 double std_dev_g = 0;
01090 double std_dev_time = 0;
01091 double std_dev_collisions = 0;
01092 for(int k=0; k<numTests; k++){
01093 if((m==0 && solution_found[i][j][k] == 1) ||
01094 (m==1 && allFound[j][k])){
01095 std_dev_expands += (expands[i][j][k]-mean_expands)*(expands[i][j][k]-mean_expands);
01096 std_dev_g += (g[i][j][k]-mean_g)*(g[i][j][k]-mean_g);
01097 std_dev_time += (time[i][j][k]-mean_time)*(time[i][j][k]-mean_time);
01098 std_dev_collisions += (collisions[i][j][k]-mean_collisions)*(collisions[i][j][k]-mean_collisions);
01099 }
01100 }
01101 std_dev_expands = sqrt(std_dev_expands/total_sol_found);
01102 std_dev_g = sqrt(std_dev_g/total_sol_found);
01103 std_dev_time = sqrt(std_dev_time/total_sol_found);
01104 std_dev_collisions = sqrt(std_dev_collisions/total_sol_found);
01105
01106 printf(" std_dev_expands=%f std_dev_g=%f std_dev_time=%f std_dev_collisions=%f\n", std_dev_expands, std_dev_g, std_dev_time, std_dev_collisions);
01107 #endif
01108 }
01109
01110 }
01111 }
01112 }
01113
01114 int main(int argc, char *argv[]){
01115 char* mprimFile = argv[1];
01116 vector<char*> name;
01117
01118
01119 name.push_back("XYThetaTimeLattice");
01120
01121
01122 name.push_back("IntervalLattice");
01123
01124 vector< vector< vector<int> > > expands;
01125 vector< vector< vector<int> > > g;
01126 vector< vector< vector<double> > > time;
01127 vector< vector< vector<int> > > collisions;
01128 vector< vector< vector<int> > > solution_found;
01129
01130 int numPlanners = name.size();
01131 int numEps = (int)((initialEps-1.0)/dec_eps+1 + 0.5);
01132 int numTests = (argc-2)/2;
01133
01134 expands.resize(numPlanners);
01135 g.resize(numPlanners);
01136 time.resize(numPlanners);
01137 collisions.resize(numPlanners);
01138 solution_found.resize(numPlanners);
01139 for(int i=0; i<numPlanners; i++){
01140 expands[i].resize(numEps);
01141 g[i].resize(numEps);
01142 time[i].resize(numEps);
01143 collisions[i].resize(numEps);
01144 solution_found[i].resize(numEps);
01145 for(int j=0; j<numEps; j++){
01146 expands[i][j].resize(numTests);
01147 g[i][j].resize(numTests);
01148 time[i][j].resize(numTests);
01149 collisions[i][j].resize(numTests);
01150 solution_found[i][j].resize(numTests);
01151 for(int k=0; k<numTests; k++){
01152 expands[i][j][k] = -1;
01153 g[i][j][k] = -1;
01154 time[i][j][k] = -1;
01155 collisions[i][j][k] = -1;
01156 solution_found[i][j][k] = -1;
01157 }
01158 }
01159 }
01160
01161 vector< vector<double> > stats;
01162 stats.resize(numEps);
01163 for(int i=0; i<numEps; i++)
01164 stats[i].resize(5);
01165
01166 for(int i=0;i<argc;i++)
01167 printf("%s\n",argv[i]);
01168
01169 for(int i=0; i<numTests; i++){
01170 char* envFile = argv[i+2];
01171 char* dynObsFile = argv[i+2+numTests];
01172 unsigned int p=0;
01173
01174 printf("files %s %s \n",envFile,dynObsFile);
01175
01176 vector<SBPL_DynamicObstacle_t> dynObs = ReadDynamicObstacles(dynObsFile);
01177
01178
01179 if(p<name.size() && strncmp("XYThetaTimeLattice",name[p],18)==0){
01180
01181 printf("\n\n\nTest %d of XYThetaTimeLattice Planner\n", i);
01182 planxythetatimelat(envFile, mprimFile, dynObs, stats, i);
01183 for(unsigned int j=0; j<stats.size(); j++){
01184 expands[p][j][i] = (int)(stats[j][0]+0.5);
01185 g[p][j][i] = (int)(stats[j][1]+0.5);
01186 time[p][j][i] = stats[j][2];
01187 collisions[p][j][i] = (int)(stats[j][3]+0.5);
01188 solution_found[p][j][i] = (int)(stats[j][4]+0.5);
01189 }
01190 p++;
01191 }
01192
01193 if(p<name.size() && strncmp("XYThetaTimeBubbleLattice",name[p],24)==0){
01194
01195 printf("\n\n\nTest %d of XYThetaTimeBubbleLattice Planner\n", i);
01196 planxythetatimebubblelat(envFile, mprimFile, dynObs, stats, i);
01197 for(unsigned int j=0; j<stats.size(); j++){
01198 expands[p][j][i] = (int)(stats[j][0]+0.5);
01199 g[p][j][i] = (int)(stats[j][1]+0.5);
01200 time[p][j][i] = stats[j][2];
01201 collisions[p][j][i] = (int)(stats[j][3]+0.5);
01202 solution_found[p][j][i] = (int)(stats[j][4]+0.5);
01203 }
01204 p++;
01205 }
01206
01207 if(p<name.size() && strncmp("SIPPLattice",name[p],11)==0){
01208
01209 printf("\n\n\nTest %d of SIPPLattice Planner\n", i);
01210 plansipplat(envFile, mprimFile, dynObs, stats, i);
01211 for(unsigned int j=0; j<stats.size(); j++){
01212 for(unsigned int k=0; k<stats[j].size(); k++){
01213 expands[p][j][i] = (int)(stats[j][0]+0.5);
01214 g[p][j][i] = (int)(stats[j][1]+0.5);
01215 time[p][j][i] = stats[j][2];
01216 collisions[p][j][i] = (int)(stats[j][3]+0.5);
01217 solution_found[p][j][i] = (int)(stats[j][4]+0.5);
01218 }
01219 }
01220 p++;
01221 }
01222
01223 if(p<name.size() && strncmp("IntervalLattice",name[p],15)==0){
01224
01225 printf("\n\n\nTest %d of IntervalLattice Planner\n", i);
01226
01227 planweightedinterval(envFile, mprimFile, dynObs, stats, i);
01228 for(unsigned int j=0; j<stats.size(); j++){
01229 expands[p][j][i] = (int)(stats[j][0]+0.5);
01230 g[p][j][i] = (int)(stats[j][1]+0.5);
01231 time[p][j][i] = stats[j][2];
01232 collisions[p][j][i] = (int)(stats[j][3]+0.5);
01233 solution_found[p][j][i] = (int)(stats[j][4]+0.5);
01234 }
01235 p++;
01236 }
01237 }
01238
01239 char buf[64];
01240 for(unsigned int i=0; i<name.size(); i++){
01241 sprintf(buf,"temp/results/data_%s.csv",name[i]);
01242 FILE* fout = fopen(buf,"w");
01243 for(int j=0; j<numTests; j++){
01244 for(int k=0; k<numEps; k++){
01245 if(solution_found[i][k][j])
01246 fprintf(fout,"%d,%f,%d",expands[i][k][j],time[i][k][j],g[i][k][j]);
01247 else
01248 fprintf(fout,"-1,-1,-1");
01249 if(k+1<numEps)
01250 fprintf(fout,",");
01251 }
01252 fprintf(fout,"\n");
01253 }
01254 fclose(fout);
01255 }
01256
01257
01258
01259 return 0;
01260 }
01261