int main(int argc, char* argv[]) { double discountFactor; unsigned int maxNbEvaluations; char isTerminal = 0; char keepingTree = 0; int nbTimestep = -1; unsigned int branchingFactor = 0; #ifdef USE_SDL char isDisplayed = 1; char isFullscreen = 1; char verbose = 0; char resolution[255] = "640x480"; #else char verbose = 1; #endif uniform_instance* instance = NULL; state* crtState = NULL; state* nextState = NULL; double reward = 0.0; action* optimalAction = NULL; struct arg_dbl* g = arg_dbl1("g", "discountFactor", "<d>", "The discount factor for the problem"); struct arg_int* n = arg_int1("n", "nbEvaluations", "<n>", "The number of evaluations"); struct arg_int* s = arg_int0("s", "nbtimestep", "<n>", "The number of timestep"); struct arg_int* b = arg_int0("b", "branchingFactor", "<n>", "The branching factor of the problem"); struct arg_lit* k = arg_lit0("k", NULL, "Keep the subtree"); struct arg_str* i = arg_str0(NULL, "state", "<s>", "The initial state to use"); #ifdef USE_SDL struct arg_lit* d = arg_lit0("d", NULL, "Display the viewer"); struct arg_lit* f = arg_lit0("f", NULL, "Fullscreen"); struct arg_lit* v = arg_lit0("v", NULL, "Verbose"); struct arg_str* r = arg_str0(NULL, "resolution", "<s>", "The resolution of the display window"); void* argtable[11]; int nbArgs = 10; #else void* argtable[7]; int nbArgs = 6; #endif struct arg_end* end = arg_end(nbArgs+1); int nerrors = 0; s->ival[0] = -1; b->ival[0] = 0; argtable[0] = g; argtable[1] = n; argtable[2] = s; argtable[3] = k; argtable[4] = b; argtable[5] = i; #ifdef USE_SDL argtable[6] = d; argtable[7] = f; argtable[8] = v; argtable[9] = r; #endif argtable[nbArgs] = end; if(arg_nullcheck(argtable) != 0) { printf("error: insufficient memory\n"); arg_freetable(argtable, nbArgs+1); return EXIT_FAILURE; } nerrors = arg_parse(argc, argv, argtable); if(nerrors > 0) { printf("%s:", argv[0]); arg_print_syntax(stdout, argtable, "\n"); arg_print_errors(stdout, end, argv[0]); arg_freetable(argtable, nbArgs+1); return EXIT_FAILURE; } discountFactor = g->dval[0]; maxNbEvaluations = n->ival[0]; branchingFactor = b->ival[0]; initGenerativeModelParameters(); if(branchingFactor) K = branchingFactor; initGenerativeModel(); if(i->count) crtState = makeState(i->sval[0]); else crtState = initState(); #if USE_SDL isDisplayed = d->count; isFullscreen = f->count; verbose = v->count; if(r->count) strcpy(resolution, r->sval[0]); #endif nbTimestep = s->ival[0]; keepingTree = k->count; arg_freetable(argtable, nbArgs+1); instance = uniform_initInstance(crtState, discountFactor); #ifdef USE_SDL if(isDisplayed) { if(initViewer(resolution, uniform_drawingProcedure, isFullscreen) == -1) return EXIT_FAILURE; viewer(crtState, NULL, 0.0, instance); } #endif do { if(keepingTree) uniform_keepSubtree(instance); else uniform_resetInstance(instance, crtState); optimalAction = uniform_planning(instance, maxNbEvaluations); isTerminal = nextStateReward(crtState, optimalAction, &nextState, &reward); freeState(crtState); crtState = nextState; if(verbose) { printState(crtState); printAction(optimalAction); printf("reward: %f depth: %u\n", reward, uniform_getMaxDepth(instance)); } #ifdef USE_SDL } while(!isTerminal && (nbTimestep < 0 || --nbTimestep) && !viewer(crtState, optimalAction, reward, instance)); #else } while(!isTerminal && (nbTimestep < 0 || --nbTimestep));
int main(int argc, char **argv) { struct arg_dbl *a = arg_dbl1(NULL,NULL,"a","a is <double>"); struct arg_dbl *b = arg_dbl0(NULL,NULL,"b","b is <double>"); struct arg_dbl *c = arg_dbl0(NULL,NULL,"c","c is <double>"); struct arg_dbl *d = arg_dbln("dD","delta","<double>",0,3,"d can occur 0..3 times"); struct arg_dbl *e = arg_dbl0(NULL,"eps,eqn","<double>","eps is optional"); struct arg_lit *help = arg_lit0(NULL,"help","print this help and exit"); struct arg_end *end = arg_end(20); void* argtable[] = {a,b,c,d,e,help,end}; int nerrors; int exitcode=0; int i; double sum=0; /* printf("a=%p\n",a); printf("b=%p\n",b); printf("c=%p\n",c); printf("d=%p\n",d); printf("e=%p\n",e); printf("help=%p\n",help); printf("end=%p\n",end); printf("argtable=%p\n",argtable); */ /* print the command line */ for (i=0; i<argc; i++) printf("%s ",argv[i]); printf("\n"); /* verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable) != 0) { /* NULL entries were detected, some allocations must have failed */ printf("%s: insufficient memory\n",argv[0]); exitcode=1; goto exit; } /* Parse the command line as defined by argtable[] */ nerrors = arg_parse(argc,argv,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("Usage: %s ", argv[0]); arg_print_syntax(stdout,argtable,"\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); exitcode=0; goto exit; } /* If the parser returned any errors then display them and exit */ if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout,end,argv[0]); exitcode=1; goto exit; } /* parsing complete, verify all args sum to zero */ for (i=0; i<a->count; i++) { printf("a[%d]=%f\n",i,a->dval[i]); sum += a->dval[i]; } for (i=0; i<b->count; i++) { printf("b[%d]=%f\n",i,b->dval[i]); sum += b->dval[i]; } for (i=0; i<c->count; i++) { printf("c[%d]=%f\n",i,c->dval[i]); sum += c->dval[i]; } for (i=0; i<d->count; i++) { printf("d[%d]=%f\n",i,d->dval[i]); sum += d->dval[i]; } for (i=0; i<e->count; i++) { printf("e[%d]=%f\n",i,e->dval[i]); sum += e->dval[i]; } printf("sum=%f\n",sum); if (sum<-1.0e-6 || sum>1.0e-6) { printf("%s: error - sum=%f is non-zero\n",argv[0],sum); exitcode=1; goto exit; } exit: /* deallocate each non-null entry in argtable[] */ arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); printf("%s: exitcode=%d\n\n",argv[0],exitcode); /* close stdin and stdout to stop memcheck whining about their memory not being freed */ fclose(stdin); fclose(stdout); return exitcode; }
struct Args parse_arguments(int argc, char *argv[]) { struct Args args; args.mass = 0.0; args.diam = 0.0; args.cd = 0.0; args.wind = 0.0; args.run_simulation = 0; // Simulation options struct arg_dbl *mass = arg_dbl1("m", "mass", "<float>", "Mass of rocket without motor (g)"); struct arg_dbl *diam = arg_dbl1("d", "diam", "<float>", "Diameter of rocket (mm)"); struct arg_dbl *cd = arg_dbl1("c", "cd", "<float>", "Drag coefficient of rocket"); struct arg_dbl *wind = arg_dbl1("w", "wind", "<float>", "Wind speed (km/h)"); struct arg_str *motor = arg_str1("M", "motor", "<string>", "Motor (use '-l' or '--list_motors' to see list"); // Help options struct arg_lit *help = arg_lit0("h", "help", "Printing help"); struct arg_lit *list_motors = arg_lit0("l", "list_motors", "List motors"); struct arg_end *end = arg_end(20); // Prepare argtable void *argtable[] = {mass, diam, cd, wind, motor, help, list_motors, end}; const char *progname = "lawn_dart"; int nerrors; // Verify the argtable[] entries were allocated sucessfully if (arg_nullcheck(argtable) != 0) { // NULL entries were detected, some allocations must have failed printf("%s: insufficient memory\n",progname); } // Parse the command line as defined by argtable[] nerrors = arg_parse(argc, argv, argtable); // Check for --help if (help->count > 0 || argc == 1) { printf("Lawn Dart: A Model Rocket Flight Simulator\n\n"); printf("%s", progname); arg_print_syntax(stdout, argtable,"\n"); puts(""); arg_print_glossary(stdout, argtable," %-25s %s\n"); puts(""); print_help(); } else if (list_motors->count > 0) { print_motor_list(); } else { // If all required options have been specified if (!nerrors) { // Get options from params args.mass = mass->dval[0] / 1000.0; args.diam = diam->dval[0] / 1000.0; args.cd = cd->dval[0]; args.wind = wind->dval[0] / 3.6; // Assert proper values int all_params_ok = 1; if (args.mass < 0.001 || args.mass > 1000.0) all_params_ok = 0; if (args.diam < 0.001 || args.diam > 1.0) all_params_ok = 0; if (args.cd < 0.0 || args.cd > 1.0) all_params_ok = 0; if (args.wind < 0.0 || args.wind > 100.0) all_params_ok = 0; // Confirm to run simulation if (all_params_ok) { args.run_simulation = 1; } } else { puts("--- Missing required parameters ---\n"); printf("%s", progname); arg_print_syntax(stdout, argtable,"\n"); puts(""); arg_print_glossary(stdout, argtable," %-25s %s\n"); puts(""); print_help(); } } return args; }