static void buildTrajectory(sequential_soo_instance* instance) {

    unsigned int i = 1;
    state* crtState = NULL;
    double q = 0;
    double* action = soo_getAnAction(instance->instances[0]);
    double* firstAction = action;
    char isTerminal = nextStateReward(instance->initial, action, &crtState, instance->rewards) < 0 ? 1 : 0;
    instance->crtNbEvaluations++;

    while((i < instance->H) && !(instance->dropTerminal && isTerminal)) {
        state* nextState = NULL;
        action = soo_getAnAction(instance->instances[i]);
        isTerminal = nextStateReward(crtState, action, &nextState, instance->rewards + i) < 0 ? 1 : 0;
        instance->crtNbEvaluations++;
        freeState(crtState);
        crtState = nextState;
        i++;
    }
    freeState(crtState);

    for(; i > 0; i--) {
        q = instance->rewards[i-1] + (instance->gamma * q);
        soo_updateValue(instance->instances[i-1], q);
    }

    if(q > instance->crtMaxSumOfDiscountedRewards) {
        instance->crtMaxSumOfDiscountedRewards = q;
        memcpy(instance->crtOptimalAction, firstAction, sizeof(double) * NUMBER_OF_DIMENSIONS_OF_ACTION);
    }
}
unsigned int ompl::control::SpaceInformation::propagateWhileValid(const base::State *state, const Control *control,
                                                                  int steps, std::vector<base::State *> &result,
                                                                  bool alloc) const
{
    double signedStepSize = steps > 0 ? stepSize_ : -stepSize_;
    steps = abs(steps);

    if (alloc)
        result.resize(steps);
    else
    {
        if (result.empty())
            return 0;
        steps = std::min(steps, (int)result.size());
    }

    int st = 0;

    if (st < steps)
    {
        if (alloc)
            result[st] = allocState();
        statePropagator_->propagate(state, control, signedStepSize, result[st]);

        if (isValid(result[st]))
        {
            ++st;
            while (st < steps)
            {
                if (alloc)
                    result[st] = allocState();
                statePropagator_->propagate(result[st - 1], control, signedStepSize, result[st]);

                if (!isValid(result[st]))
                {
                    if (alloc)
                    {
                        freeState(result[st]);
                        result.resize(st);
                    }
                    break;
                }
                else
                    ++st;
            }
        }
        else
        {
            if (alloc)
            {
                freeState(result[st]);
                result.resize(st);
            }
        }
    }

    return st;
}
Exemple #3
0
void killGame(void)
{
	fadeOut();
	NOGBA("KILLING IT");
	freePlayer();
	freeEnergyBalls();
	freeBigButtons();
	freeCubes();
	freeDoors();
	freeElevators();
	freeEmancipation();
	freePlatforms();
	freeTimedButtons();
	freeTurrets();
	freeWallDoors();
	freeSludge();
	freeRoom(&gameRoom);
	freePortals();
	freeState(NULL);
	freeSound();
	freePause();

	resetAllPI();

	NOGBA("END mem free : %dko (%do)",getMemFree()/1024,getMemFree());
}
trajectory::InterpolatedPtr planOMPL(
    const statespace::StateSpace::State* _start,
    const statespace::StateSpace::State* _goal,
    statespace::ConstStateSpacePtr _stateSpace,
    statespace::InterpolatorPtr _interpolator,
    distance::DistanceMetricPtr _dmetric,
    constraint::SampleablePtr _sampler,
    constraint::TestablePtr _validityConstraint,
    constraint::TestablePtr _boundsConstraint,
    constraint::ProjectablePtr _boundsProjector,
    double _maxPlanTime,
    double _maxDistanceBtwValidityChecks)
{
  // Create a SpaceInformation.  This function will ensure state space matching
  auto si = getSpaceInformation(
      _stateSpace,
      _interpolator,
      std::move(_dmetric),
      std::move(_sampler),
      std::move(_validityConstraint),
      std::move(_boundsConstraint),
      std::move(_boundsProjector),
      _maxDistanceBtwValidityChecks);

  // Start and states
  auto pdef = ompl_make_shared<::ompl::base::ProblemDefinition>(si);
  auto sspace
      = ompl_static_pointer_cast<GeometricStateSpace>(si->getStateSpace());
  auto start = sspace->allocState(_start);
  auto goal = sspace->allocState(_goal);

  // ProblemDefinition clones states and keeps them internally
  pdef->setStartAndGoalStates(start, goal);

  sspace->freeState(start);
  sspace->freeState(goal);

  auto planner = ompl_make_shared<PlannerType>(si);
  return planOMPL(
      planner,
      pdef,
      std::move(_stateSpace),
      std::move(_interpolator),
      _maxPlanTime);
}
unsigned int ompl::control::SpaceInformation::propagateWhileValid(const base::State *state, const Control *control,
                                                                  int steps, base::State *result) const
{
    if (steps == 0)
    {
        if (result != state)
            copyState(result, state);
        return 0;
    }

    double signedStepSize = steps > 0 ? stepSize_ : -stepSize_;
    steps = abs(steps);

    // perform the first step of propagation
    statePropagator_->propagate(state, control, signedStepSize, result);

    // if we found a valid state after one step, we can go on
    if (isValid(result))
    {
        base::State *temp1 = result;
        base::State *temp2 = allocState();
        base::State *toDelete = temp2;
        unsigned int r = steps;

        // for the remaining number of steps
        for (int i = 1; i < steps; ++i)
        {
            statePropagator_->propagate(temp1, control, signedStepSize, temp2);
            if (isValid(temp2))
                std::swap(temp1, temp2);
            else
            {
                // the last valid state is temp1;
                r = i;
                break;
            }
        }

        // if we finished the for-loop without finding an invalid state, the last valid state is temp1
        // make sure result contains that information
        if (result != temp1)
            copyState(result, temp1);

        // free the temporary memory
        freeState(toDelete);

        return r;
    }
    // if the first propagation step produced an invalid step, return 0 steps
    // the last valid state is the starting one (assumed to be valid)
    else
    {
        if (result != state)
            copyState(result, state);
        return 0;
    }
}
int main(int argc, const char* argv[]){
	{std::string srcFile, printFile;
	parseOpt(argc-1, &argv[1], srcFile, printFile);
	void *state = newState();
	parseFile(state,srcFile.c_str(),printFile.c_str());
	freeState(state);
	}
	#ifdef WIN32
	_CrtDumpMemoryLeaks();
	#endif
	return 0;
}
void sequential_soo_uninitInstance(sequential_soo_instance** instance) {

    unsigned int i = 0;
    for(;i < (*instance)->H; i++)
        soo_uninit((*instance)->instances+i);
    free((*instance)->instances);
    freeState((*instance)->initial);
    free((*instance)->rewards);
    free((*instance));
    *instance = NULL;

}
Exemple #8
0
double ompl::base::SpaceInformation::averageValidMotionLength(unsigned int attempts) const
{
    // take the square root here because we in fact have a nested for loop
    // where each loop executes #attempts steps (the sample() function of the UniformValidStateSampler if a for loop
    // too)
    attempts = std::max((unsigned int)floor(sqrt((double)attempts) + 0.5), 2u);

    StateSamplerPtr ss = allocStateSampler();
    auto uvss(std::make_shared<UniformValidStateSampler>(this));
    uvss->setNrAttempts(attempts);

    State *s1 = allocState();
    State *s2 = allocState();

    std::pair<State *, double> lastValid;
    lastValid.first = nullptr;

    double d = 0.0;
    unsigned int count = 0;
    for (unsigned int i = 0; i < attempts; ++i)
        if (uvss->sample(s1))
        {
            ++count;
            ss->sampleUniform(s2);
            if (checkMotion(s1, s2, lastValid))
                d += distance(s1, s2);
            else
                d += distance(s1, s2) * lastValid.second;
        }

    freeState(s2);
    freeState(s1);

    if (count > 0)
        return d / (double)count;
    else
        return 0.0;
}
Exemple #9
0
int libscalpel_finalize(scalpelState ** state)
{
    std::string funcname("libscalpel_finalize");

    if (state == NULL)
        throw std::runtime_error(funcname + ": state argument must not be NULL.");

    if (*state == NULL)
        throw std::runtime_error(funcname + ": state has not been allocated.");

    closeAuditFile((*state)->auditFile);
    destroy_threading_model(*state);
    destroyStore();
    freeState(*state);

    return SCALPEL_OK;
}
Exemple #10
0
bool ompl::base::SpaceInformation::searchValidNearby(const ValidStateSamplerPtr &sampler, State *state, const State *near, double distance) const
{
    if (state != near)
        copyState(state, near);

    // fix bounds, if needed
    if (!satisfiesBounds(state))
        enforceBounds(state);

    bool result = isValid(state);

    if (!result)
    {
        // try to find a valid state nearby
        State *temp = cloneState(state);
        result = sampler->sampleNear(state, temp, distance);
        freeState(temp);
    }

    return result;
}
Exemple #11
0
void TestRunnerDlg::OnSelchangeComboTest()
{
    CComboBox   *testsSelection = (CComboBox *)GetDlgItem (IDC_COMBO_TEST);

    int currentSelection = testsSelection->GetCurSel ();

    if (currentSelection >= 0 && currentSelection < _tests.size ())
    {
        _selectedTest = (_tests.begin () + currentSelection)->pTest;
        beIdle ();
		CWinApp* pApp = AfxGetApp();
		pApp->WriteProfileString("Tests", "lastTest", _selectedTest->toString().c_str());
    }
    else
    {
        _selectedTest = 0;
        beRunDisabled ();

    }

    freeState ();
    reset ();

}
Exemple #12
0
double ompl::base::SpaceInformation::probabilityOfValidState(unsigned int attempts) const
{
    if (attempts == 0)
        return 0.0;

    unsigned int valid = 0;
    unsigned int invalid = 0;

    StateSamplerPtr ss = allocStateSampler();
    State *s = allocState();

    for (unsigned int i = 0; i < attempts; ++i)
    {
        ss->sampleUniform(s);
        if (isValid(s))
            ++valid;
        else
            ++invalid;
    }

    freeState(s);

    return (double)valid / (double)(valid + invalid);
}
Exemple #13
0
void TestRunnerDlg::OnRun()
{
	if (_selectedTest == 0)
		return;

    freeState       ();
    reset           ();

    beRunning       ();

    int numberOfTests = _selectedTest->countTestCases ();

    _testsProgress->start (numberOfTests);

    _result            = new GUITestResult ((TestRunnerDlg *)this);
    _activeTest        = new ActiveTest (_selectedTest);

    _testStartTime     = timeGetTime ();

    _activeTest->run (_result);

    _testEndTime       = timeGetTime ();

}
int main(int argc, char* argv[]) {

#ifdef BALL
    double discountFactor = 0.9;
#else
#ifdef CART_POLE
    double discountFactor = 0.95;
#else
#ifdef DOUBLE_CART_POLE
    double discountFactor = 0.95;
#else
#ifdef MOUNTAIN_CAR
    double discountFactor = 0.99;
#else
#ifdef ACROBOT
    double discountFactor = 0.95;
#else
#ifdef BOAT
    double discountFactor = 0.95;
#else
#ifdef SWIMMER
    double discountFactor = 0.95;
#endif
#endif
#endif
#endif
#endif
#endif
#endif

    FILE* initFileFd = NULL;
    state** initialStates = NULL;
    FILE* results = NULL;
    char str[1024];
    unsigned int i = 0;
    unsigned int* ns = NULL;
    unsigned int nbN = 0;
    unsigned int* hs = NULL;
    unsigned int nbH = 0;
    unsigned int n = 0;
    unsigned int nbSteps = 0;
    unsigned int nbIterations = 1;
    unsigned int timestamp = time(NULL);
    int readFscanf = -1;

    random_search_instance* random_search = NULL;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the inital state");
    struct arg_str* r = arg_str1("n", NULL, "<s>", "List of maximum numbers of evaluations");
    struct arg_str* d = arg_str1("h", NULL, "<s>", "List of depth");
    struct arg_int* s = arg_int1("s", NULL, "<n>", "Number of steps");
    struct arg_int* it = arg_int1("i", NULL, "<n>", "Number of iteration");
    struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs");
    struct arg_end* end = arg_end(7);

    int nerrors = 0;
    void* argtable[7];

    argtable[0] = initFile;
    argtable[1] = r;
    argtable[2] = d;
    argtable[3] = s;
    argtable[4] = it;
    argtable[5] = where;
    argtable[6] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 7);
        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, 7);
        return EXIT_FAILURE;
    }

    initGenerativeModelParameters();
    initGenerativeModel();

    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &n);
    initialStates = (state**)malloc(sizeof(state*) * n);
    
    for(; i < n; i++) {
        readFscanf = fscanf(initFileFd, "%s\n", str);
        initialStates[i] = makeState(str);
    }
    fclose(initFileFd);

    nbSteps = s->ival[0];
    nbIterations = it->ival[0];
    ns = parseUnsignedIntList((char*)r->sval[0], &nbN);
    hs = parseUnsignedIntList((char*)d->sval[0], &nbH);

    random_search = random_search_initInstance(NULL, discountFactor);
    h_max = h_max_crt_depth;

    sprintf(str, "%s/%u_results_random_search_%s.csv", where->filename[0], timestamp,(char*)r->sval[0]);
    results = fopen(str, "w");

    for(i = 0; i < nbIterations; i++) {
        unsigned int j = 0;

        for(;j < nbH; j++) {
            unsigned int k = 0;

            crtDepth = hs[j];

            for(; k < nbN; k++) {
                unsigned int l = 0;
                double sumRewards = 0.0;

                for(; l < n; l++) {
                    unsigned int m = 0;
                    state* crt = copyState(initialStates[l]);

                    for(; m < nbSteps; m++) {
                        char isTerminal = 0;
                        double reward = 0.0;
                        state* nextState = NULL;
                        double* optimalAction = NULL;

                        random_search_resetInstance(random_search, crt);
                        optimalAction = random_search_planning(random_search, ns[k]);
                        isTerminal = nextStateReward(crt, optimalAction, &nextState, &reward) < 0 ? 1 : 0;
                        freeState(crt);
                        free(optimalAction);
                        crt = nextState;
                        sumRewards += reward;
                        if(isTerminal)
                            break;
                    }
                    random_search_resetInstance(random_search, crt);
                    freeState(crt);

                    printf(">>>>>>>>>>>>>> %uth initial state processed with h=%u and n=%u of iteration %u\n", l + 1, hs[j], ns[k], i+1);
                    fflush(NULL);
                }

                fprintf(results, "%u,%u,%.15f\n", hs[j],ns[k], sumRewards / (double)n);
                printf(">>>>>>>>>>>>>> n = %u  done\n\n", ns[k]);
                fflush(NULL);
            }
            printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> h = %u done \n\n", hs[j]);
            fflush(NULL);
        }

        fprintf(results,"\n");
        printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ITERATION %u DONE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n\n", i+1);
        fflush(NULL);

    }

    fclose(results);

    arg_freetable(argtable, 7);

    for(i = 0; i < n; i++)
        freeState(initialStates[i]);

    free(initialStates);

    random_search_uninitInstance(&random_search);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}
Exemple #15
0
TestRunnerDlg::~TestRunnerDlg ()
{
    freeState ();
    delete _testsProgress;
}
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[]) {

#ifdef BALL
    double discountFactor = 0.9;
#else
#ifdef CART_POLE
    double discountFactor = 0.95;
#else
#ifdef DOUBLE_CART_POLE
    double discountFactor = 0.95;
#else
#ifdef MOUNTAIN_CAR
    double discountFactor = 0.99;
#else
#ifdef ACROBOT
    double discountFactor = 0.95;
#else
#ifdef BOAT
    double discountFactor = 0.95;
#else
#ifdef CART_POLE_BINARY
    double discountFactor = 0.95;
#else
#ifdef SWIMMER
    double discountFactor = 0.95;
#endif
#endif
#endif
#endif
#endif
#endif
#endif
#endif

    FILE* initFileFd = NULL;
    state** initialStates = NULL;
    FILE* results = NULL;
    char str[1024];
    unsigned int i = 0;
    unsigned int h = 0;
    unsigned int* ns = NULL;
    unsigned int nbN = 0;
    unsigned int n = 0;
    unsigned int nbSteps = 0;
    unsigned int timestamp = time(NULL);
    int readFscanf = -1;

    optimistic_instance* optimistic = NULL;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the inital state");
    struct arg_str* r = arg_str1("n", NULL, "<s>", "List of maximum numbers of evaluations");
    struct arg_int* s = arg_int1("s", NULL, "<n>", "Number of steps");
    struct arg_int* k = arg_int1("k", NULL, "<n>", "Branching factor of the problem");
    struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs");
    struct arg_end* end = arg_end(6);

    int nerrors = 0;
    void* argtable[6];

    argtable[0] = initFile;
    argtable[1] = r;
    argtable[2] = s;
    argtable[3] = k;
    argtable[4] = where;
    argtable[5] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 6);
        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, 6);
        return EXIT_FAILURE;
    }

    initGenerativeModelParameters();
    K = k->ival[0];
    initGenerativeModel();

    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &n);
    initialStates = (state**)malloc(sizeof(state*) * n);
    
    for(; i < n; i++) {
        readFscanf = fscanf(initFileFd, "%s\n", str);
        initialStates[i] = makeState(str);
    }
    fclose(initFileFd);

    nbSteps = s->ival[0];
    ns = parseUnsignedIntList((char*)r->sval[0], &nbN);

    optimistic = optimistic_initInstance(NULL, discountFactor);

    sprintf(str, "%s/%u_results_%u_%u.csv", where->filename[0], timestamp, K, nbSteps);
    results = fopen(str, "w");

    for(h = 0; h < nbN; h++) {
        double sumRewards = 0.0;

        for(i = 0; i < n; i++) {
            unsigned int j = 0;
            state* crt = copyState(initialStates[i]);

            optimistic_resetInstance(optimistic, crt);
            for(; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                optimistic_keepSubtree(optimistic);
                action* optimalAction = optimistic_planning(optimistic, ns[h]);
                isTerminal = nextStateReward(crt, optimalAction, &nextState, &reward) < 0 ? 1 : 0;
                freeState(crt);
                crt = nextState;
                sumRewards += reward;
                if(isTerminal)
                    break;
            }
            optimistic_resetInstance(optimistic, crt);
            freeState(crt);

            printf(">>>>>>>>>>>>>> %uth initial state processed\n", i + 1);
            fflush(NULL);
        }

        fprintf(results, "%u,%.15f\n", ns[h], sumRewards / (double)n);
        printf(">>>>>>>>>>>>>> n = %u  done\n\n", ns[h]);
        fflush(NULL);
    }

    fclose(results);

    arg_freetable(argtable, 6);

    for(i = 0; i < n; i++)
        freeState(initialStates[i]);

    free(initialStates);

    optimistic_uninitInstance(&optimistic);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}
int main(int argc, char* argv[]) {

    double discountFactor = 0.9;
    unsigned int i = 0;
    unsigned int n = 0;
    FILE* initFileFd = NULL;
    FILE* outputFileFd = NULL;
    unsigned int nbIterations = 0;
    int readFscanf = -1;

    optimistic_instance* optimistic = NULL;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the inital state");
    struct arg_int* k = arg_int1("k", NULL, "<n>", "The branching factor of the problem");
    struct arg_int* it = arg_int1("n", NULL, "<n>", "The number of iterations");
    struct arg_file* outputFile = arg_file1("o", NULL, "<file>", "The output file");
    struct arg_end* end = arg_end(5);

    void* argtable[5];

    int nerrors = 0;

    argtable[0] = initFile;
    argtable[1] = it;
    argtable[2] = outputFile;
    argtable[3] = k;
    argtable[4] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 5);
        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, 5);
        return EXIT_FAILURE;
    }

    nbIterations = it->ival[0];

    initGenerativeModelParameters();
    K = k->ival[0];
    initGenerativeModel();

    outputFileFd = fopen(outputFile->filename[0], "w");

    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &n);

    arg_freetable(argtable, 5);

    optimistic = optimistic_initInstance(NULL, discountFactor);

    for(; i < n; i++) {
        char str[1024];
        unsigned int j = 0;

        readFscanf = fscanf(initFileFd, "%s\n", str);
        state* initial = makeState(str);
        double crtOptimalValue = 0.0;
        unsigned int crtOptimalAction = 0;

        for(; j < K; j++) {
            state* nextState = NULL;
            double reward = 0.0;
            char isTerminal = 0;

            isTerminal = nextStateReward(initial, actions[j], &nextState, &reward);
            optimistic_resetInstance(optimistic, nextState);
            freeState(nextState);
            optimistic_planning(optimistic, nbIterations);

            if(optimistic->crtOptimalValue > crtOptimalValue) {
                crtOptimalValue = optimistic->crtOptimalValue;
                crtOptimalAction = j;
            }

            fprintf(outputFileFd, "%.15f,", optimistic->crtOptimalValue);
            printf("%uth action done\n", j+1);
            fflush(outputFileFd);
        }
        fprintf(outputFileFd, "%u\n", crtOptimalAction);
        freeState(initial);
        printf("%uth initial state processed\n", i+1);
        fflush(outputFileFd);
    }

    fclose(outputFileFd);
    fclose(initFileFd);

    optimistic_uninitInstance(&optimistic);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}
int main(int argc, char* argv[]) {

    double discountFactor = 0.9;

    FILE* initFileFd = NULL;
    FILE** combinedFd = NULL;
    FILE* optimalFd = NULL;

    optimistic_instance* optimistic = NULL;
    random_search_instance* random_search = NULL;
    uct_instance* uct = NULL;
    uniform_instance* uniform = NULL;

    unsigned int maxDepth = 0;
    unsigned int i = 0;
    unsigned int n = 0;
    state** initialStates = NULL;
    unsigned int timestamp = time(NULL);
    double* optimalValues = NULL;
    int readFscanf = -1;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the inital state");
    struct arg_int* d = arg_int1("d", NULL, "<n>", "Maximum depth of an uniform tree which the number of call per step");
    struct arg_int* k = arg_int1("k", NULL, "<n>", "Branching factor of the problem");
    struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs");
    struct arg_file* optimal = arg_file1(NULL, "optimal", "<file>", "File containing the optimal values");
    struct arg_end* end = arg_end(6);

    void* argtable[6];
    int nerrors = 0;

    argtable[0] = initFile;
    argtable[1] = where;
    argtable[2] = d;
    argtable[3] = k;
    argtable[4] = optimal;
    argtable[5] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 6);
        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, 6);
        return EXIT_FAILURE;
    }

    initGenerativeModelParameters();
    K = k->ival[0];
    initGenerativeModel();

    optimalFd = fopen(optimal->filename[0], "r");
    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &n);
    initialStates = (state**)malloc(sizeof(state*) * n);
    
    for(i = 0; i < n; i++) {
        char str[1024];
        readFscanf = fscanf(initFileFd, "%s\n", str);
        initialStates[i] = makeState(str);
    }

    maxDepth = d->ival[0];

    combinedFd = (FILE**)malloc(sizeof(FILE*) * maxDepth);

    for(i = 1; i <= maxDepth; i++) {
        char str[1024];
        sprintf(str, "%s/%u_combined_%u_%u.csv", where->filename[0], timestamp, K, i);
        combinedFd[i - 1] = fopen(str, "w");
        fprintf(combinedFd[i - 1], "n,optimistic,random search,uct,uniform\n");
    }

    arg_freetable(argtable, 6);

    optimalValues = (double*)malloc(sizeof(double) * K);

    optimistic = optimistic_initInstance(initialStates[0], discountFactor);
    random_search = random_search_initInstance(initialStates[0], discountFactor);
    uct = uct_initInstance(initialStates[0], discountFactor);
    uniform = uniform_initInstance(initialStates[0], discountFactor);

    for(i = 0; i < n; i++) {
        unsigned int j = 1;
        unsigned int maxNbIterations = K;
        unsigned int optimalAction = 0;
        char str[1024];

        readFscanf = fscanf(optimalFd, "%s\n", str);
        optimalValues[0] = strtod(strtok(str, ","), NULL);
        printf("%.15f,",optimalValues[0]);

        for(; j < K; j++) {
            optimalValues[j] = strtod(strtok(NULL, ","), NULL);
            printf("%.15f,",optimalValues[j]);
        }

        optimalAction = atol(strtok(NULL, ","));
        printf("%u\n",optimalAction);

        for(j = 1; j <= maxDepth; j++) {
            unsigned int crtOptimalAction = getActionId(optimistic_planning(optimistic, maxNbIterations));
            fprintf(combinedFd[j - 1], "%u,", maxNbIterations);
            fprintf(combinedFd[j - 1], "%.15f,", crtOptimalAction == optimalAction ? 0.0 : optimalValues[optimalAction] - optimalValues[crtOptimalAction]);
            maxNbIterations += pow(K, j+1);
        }
        if(i < (n - 1))
            optimistic_resetInstance(optimistic, initialStates[i+1]);

        printf("optimistic: %uth initial state processed\n", i+1);

        fflush(NULL);


        maxNbIterations = K;
        for(j = 1; j <= maxDepth; j++) {
            unsigned int crtOptimalAction = getActionId(random_search_planning(random_search, maxNbIterations));
            fprintf(combinedFd[j - 1], "%.15f,", crtOptimalAction == optimalAction ? 0.0 : optimalValues[optimalAction] - optimalValues[crtOptimalAction]);
            maxNbIterations += pow(K, j+1);
        }
        if(i < (n - 1))
            random_search_resetInstance(random_search, initialStates[i + 1]);

        printf("random_search: %uth initial state processed\n", i+1);

        fflush(NULL);


        maxNbIterations = K;
        for(j = 1; j <= maxDepth; j++) {
            unsigned int crtOptimalAction = getActionId(uct_planning(uct, maxNbIterations));
            fprintf(combinedFd[j - 1], "%.15f,", crtOptimalAction == optimalAction ? 0.0 : optimalValues[optimalAction] - optimalValues[crtOptimalAction]);
            maxNbIterations += pow(K, j+1);
        }
        if(i < (n - 1))
            uct_resetInstance(uct, initialStates[i + 1]);

        printf("uct: %uth initial state processed\n", i+1);

        fflush(NULL);


        maxNbIterations = K;
        for(j = 1; j <= maxDepth; j++) {
            unsigned int crtOptimalAction = getActionId(uniform_planning(uniform, maxNbIterations));
            fprintf(combinedFd[j - 1], "%.15f\n", crtOptimalAction == optimalAction ? 0.0 : optimalValues[optimalAction] - optimalValues[crtOptimalAction]);
            maxNbIterations += pow(K, j+1);
        }
        if(i < (n - 1))
            uniform_resetInstance(uniform, initialStates[i + 1]);

        printf("uniform: %uth initial state processed\n", i+1);

        printf("%uth initial state processed\n", i+1);

        fflush(NULL);

    }

    for(i = 0; i < maxDepth; i++) {
        fclose(combinedFd[i]);
    }

    for(i = 0; i < n; i++)
        freeState(initialStates[i]);

    free(initialStates);

    free(combinedFd);

    optimistic_uninitInstance(&optimistic);
    random_search_uninitInstance(&random_search);
    uct_uninitInstance(&uct);
    uniform_uninitInstance(&uniform);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}
Exemple #20
0
void killEditor(void)
{
	fadeOut();
	freeRoomEditor();
	freeState(NULL);
}
trajectory::InterpolatedPtr planOMPL(
    const statespace::StateSpace::State* _start,
    constraint::TestablePtr _goalTestable,
    constraint::SampleablePtr _goalSampler,
    statespace::ConstStateSpacePtr _stateSpace,
    statespace::InterpolatorPtr _interpolator,
    distance::DistanceMetricPtr _dmetric,
    constraint::SampleablePtr _sampler,
    constraint::TestablePtr _validityConstraint,
    constraint::TestablePtr _boundsConstraint,
    constraint::ProjectablePtr _boundsProjector,
    double _maxPlanTime,
    double _maxDistanceBtwValidityChecks)
{
  if (_goalTestable == nullptr)
  {
    throw std::invalid_argument("Testable goal is nullptr.");
  }

  if (_goalSampler == nullptr)
  {
    throw std::invalid_argument("Sampleable goal is nullptr.");
  }

  if (_goalTestable->getStateSpace() != _stateSpace)
  {
    throw std::invalid_argument("Testable goal does not match StateSpace");
  }

  if (_goalSampler->getStateSpace() != _stateSpace)
  {
    throw std::invalid_argument("Sampleable goal does not match StateSpace");
  }

  auto si = getSpaceInformation(
      _stateSpace,
      _interpolator,
      std::move(_dmetric),
      std::move(_sampler),
      std::move(_validityConstraint),
      std::move(_boundsConstraint),
      std::move(_boundsProjector),
      _maxDistanceBtwValidityChecks);

  // Set the start and goal
  auto pdef = ompl_make_shared<::ompl::base::ProblemDefinition>(si);
  auto sspace
      = ompl_static_pointer_cast<GeometricStateSpace>(si->getStateSpace());
  auto start = sspace->allocState(_start);
  pdef->addStartState(start); // copies
  sspace->freeState(start);

  auto goalRegion = ompl_make_shared<GoalRegion>(
      si, std::move(_goalTestable), _goalSampler->createSampleGenerator());
  pdef->setGoal(goalRegion);

  auto planner = ompl_make_shared<PlannerType>(si);
  return planOMPL(
      planner,
      pdef,
      std::move(_stateSpace),
      std::move(_interpolator),
      _maxPlanTime);
}
int main(int argc, char* argv[]) {

#ifdef BALL
    double discountFactor = 0.9;
#else
#ifdef CART_POLE
    double discountFactor = 0.95;
#else
#ifdef DOUBLE_CART_POLE
    double discountFactor = 0.95;
#else
#ifdef MOUNTAIN_CAR
    double discountFactor = 0.95;
#else
#ifdef ACROBOT
    double discountFactor = 0.95;
#else
#ifdef BOAT
    double discountFactor = 0.95;
#else
#ifdef CART_POLE_BINARY
    double discountFactor = 0.95;
#else
#ifdef SWIMMER
    double discountFactor = 0.95;
#endif
#endif
#endif
#endif
#endif
#endif
#endif
#endif

    FILE* initFileFd = NULL;
    state** initialStates = NULL;
    unsigned int maxDepth = 0;
    FILE* combinedFd = NULL;
    FILE* results = NULL;
    char str[1024];
    unsigned int i = 0;
    unsigned int minDepth = 1;
    unsigned int crtDepth = 0;
    unsigned int n = 0;
    unsigned int maxNbIterations = 0;
    unsigned int nbSteps = 0;
    unsigned int timestamp = time(NULL);
    int readFscanf = -1;

    optimistic_instance* optimistic = NULL;
    random_search_instance* random_search = NULL;
    uct_instance* uct = NULL;
    uniform_instance* uniform = NULL;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the inital state");
    struct arg_int* d = arg_int1("d", NULL, "<n>", "Maximum depth of an uniform tree which the number of call per step");
    struct arg_int* d2 = arg_int0(NULL, "min", "<n>", "Minimun depth to start from (min > 0)");
    struct arg_int* s = arg_int1("s", NULL, "<n>", "Number of steps");
    struct arg_int* k = arg_int1("k", NULL, "<n>", "Branching factor of the problem");
    struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs");
    struct arg_end* end = arg_end(7);

    int nerrors = 0;
    void* argtable[7];

    argtable[0] = initFile;
    argtable[1] = d2;
    argtable[2] = d;
    argtable[3] = s;
    argtable[4] = k;
    argtable[5] = where;
    argtable[6] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 7);
        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, 7);
        return EXIT_FAILURE;
    }

    initGenerativeModelParameters();
    K = k->ival[0];
    initGenerativeModel();

    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &n);
    initialStates = (state**)malloc(sizeof(state*) * n);
    
    for(; i < n; i++) {
        readFscanf = fscanf(initFileFd, "%s\n", str);
        initialStates[i] = makeState(str);
    }
    fclose(initFileFd);

    if(d2->count)
        minDepth = d2->ival[0];
    maxDepth = d->ival[0];
    maxNbIterations = K;
    nbSteps = s->ival[0];

    optimistic = optimistic_initInstance(NULL, discountFactor);
    random_search = random_search_initInstance(NULL, discountFactor);
    uct = uct_initInstance(NULL, discountFactor);
    uniform = uniform_initInstance(NULL, discountFactor);

    sprintf(str, "%s/%u_results_%u_%u.csv", where->filename[0], timestamp, K, nbSteps);
    results = fopen(str, "w");

    for(crtDepth = 1; crtDepth < minDepth; crtDepth++)
        maxNbIterations += pow(K, crtDepth+1);

    for(crtDepth = minDepth; crtDepth <= maxDepth; crtDepth++) {
        double averages[4] = {0.0, 0.0, 0.0, 0.0};
        sprintf(str, "%s/%u_combined_%u_%u(%u)_%u.csv", where->filename[0], timestamp, K, crtDepth, maxNbIterations, nbSteps);
        combinedFd = fopen(str, "w");
        fprintf(combinedFd, "nbIterations,optimistic,optimistic(discounted),optimistic depth,random search,random search(discounted),random search depth,uct,uct(discounted),uct depth,uniform,uniform(discounted),uniform depth\n");

        for(i = 0; i < n; i++) {
            unsigned int j = 0;
            double sumRewards = 0.0;
            double discountedSumRewards = 0.0;
            unsigned int sumDepths = 0;
            state* crt = copyState(initialStates[i]);

            optimistic_resetInstance(optimistic, crt);
            for(; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                optimistic_keepSubtree(optimistic);
                action* optimalAction = optimistic_planning(optimistic, maxNbIterations);
                isTerminal = nextStateReward(crt, optimalAction, &nextState, &reward);
                freeState(crt);
                crt = nextState;
                sumRewards += reward;
                sumDepths += optimistic_getMaxDepth(optimistic);
                discountedSumRewards += optimistic->gammaPowers[j] * reward;
                if(isTerminal < 0)
                    break;
            }
            optimistic_resetInstance(optimistic, crt);
            freeState(crt);
            fprintf(combinedFd, "%u,%.15f,%.15f,%.15f,",maxNbIterations, sumRewards, discountedSumRewards, sumDepths / (double)((j == nbSteps) ? nbSteps : (j + 1)));

            averages[0] += sumRewards;

            printf("Optimistic   : %uth initial state processed\n", i + 1);

            sumRewards = 0.0;
            discountedSumRewards = 0.0;
            sumDepths = 0;
            crt = copyState(initialStates[i]);
            for(j = 0; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                random_search_resetInstance(random_search, crt);
                action* optimalAction = random_search_planning(random_search, maxNbIterations);
                isTerminal = nextStateReward(crt, optimalAction, &nextState, &reward);
                freeState(crt);
                crt = nextState;
                sumRewards += reward;
                sumDepths += random_search_getMaxDepth(random_search);
                discountedSumRewards += random_search->gammaPowers[j] * reward;
                if(isTerminal < 0)
                    break;
            }
            random_search_resetInstance(random_search, crt);
            freeState(crt);
            fprintf(combinedFd, "%.15f,%.15f,%.15f,", sumRewards, discountedSumRewards, sumDepths / (double)((j == nbSteps) ? nbSteps : (j + 1)));

            averages[1] += sumRewards;

            printf("Random search: %uth initial state processed\n", i + 1);

            sumRewards = 0.0;
            discountedSumRewards = 0.0;
            sumDepths = 0;
            crt = copyState(initialStates[i]);
            uct_resetInstance(uct, crt);
            for(j = 0; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                uct_keepSubtree(uct);
                action* optimalAction = uct_planning(uct, maxNbIterations);
                isTerminal = nextStateReward(crt, optimalAction, &nextState, &reward);
                freeState(crt);
                crt = nextState;
                sumRewards += reward;
                sumDepths += uct_getMaxDepth(uct);
                discountedSumRewards += uct->gammaPowers[j] * reward;
                if(isTerminal < 0)
                    break;
            }
            uct_resetInstance(uct, crt);
            freeState(crt);
            fprintf(combinedFd, "%.15f,%.15f,%.15f,", sumRewards, discountedSumRewards, sumDepths / (double)((j == nbSteps) ? nbSteps : (j + 1)));

            averages[2] += sumRewards;

            printf("Uct          : %uth initial state processed\n", i + 1);

            sumRewards = 0.0;
            discountedSumRewards = 0.0;
            crt = copyState(initialStates[i]);
            uniform_resetInstance(uniform, crt);
            for(j = 0; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                uniform_keepSubtree(uniform);
                action* optimalAction = uniform_planning(uniform, maxNbIterations);
                isTerminal = nextStateReward(crt, optimalAction, &nextState, &reward);
                freeState(crt);
                crt = nextState;
                sumRewards += reward;
                discountedSumRewards += uniform->gammaPowers[j] * reward;
                if(isTerminal < 0)
                    break;
            }
            uniform_resetInstance(uniform, crt);
            freeState(crt);
            fprintf(combinedFd, "%.15f,%.15f,%u\n", sumRewards, discountedSumRewards, crtDepth -1);

            fflush(combinedFd);

            averages[3] += sumRewards;

            printf("Uniform      : %uth initial state processed\n", i + 1);
            printf(">>>>>>>>>>>>>> %uth initial state processed\n", i + 1);

        }

        fprintf(results, "%u,%.15f,%.15f,%.15f,%.15f\n", maxNbIterations, averages[0] / (double)n, averages[1] / (double)n, averages[2] / (double)n, averages[3] / (double)n);
        fflush(results);

        printf(">>>>>>>>>>>>>> %u depth done\n\n", crtDepth);

        fclose(combinedFd);
        maxNbIterations += pow(K, crtDepth+1);

    }

    fclose(results);

    arg_freetable(argtable, 7);

    for(i = 0; i < n; i++)
        freeState(initialStates[i]);

    free(initialStates);

    optimistic_uninitInstance(&optimistic);
    random_search_uninitInstance(&random_search);
    uct_uninitInstance(&uct);
    uniform_uninitInstance(&uniform);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}
int main(int argc, char* argv[]) {

    double discountFactor = 0.9;

    FILE* initFileFd = NULL;
    double* setPoints = NULL;
    FILE* results = NULL;
    char str[1024];
    unsigned int i = 0;
    unsigned int nbSetPoints = 0;
    unsigned int* ns = NULL;
    unsigned int nbN = 0;
    unsigned int* hs = NULL;
    unsigned int nbH = 0;
    unsigned int nbSteps = 0;
    unsigned int timestamp = time(NULL);
    int readFscanf = -1;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the set points");
    struct arg_int* s = arg_int1("s", NULL, "<n>", "Number of steps");
    struct arg_str* r = arg_str1("n", NULL, "<s>", "List of ressources");
    struct arg_str* z = arg_str1("h", NULL, "<s>", "List of length for the sequences");
    struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs");
    struct arg_end* end = arg_end(6);

    int nerrors = 0;
    void* argtable[6];

    argtable[0] = initFile;
    argtable[1] = r;
    argtable[2] = s;
    argtable[3] = z;
    argtable[4] = where;
    argtable[5] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 6);
        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, 6);
        return EXIT_FAILURE;
    }

    initGenerativeModelParameters();
    initGenerativeModel();

    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &nbSetPoints);
    setPoints = (double*)malloc(sizeof(double) * nbSetPoints);
    
    for(; i < nbSetPoints; i++) {
        readFscanf = fscanf(initFileFd, "%s\n", str);
        setPoints[i] = strtod(str, NULL);
    }
    fclose(initFileFd);

    nbSteps = s->ival[0];
    hs = parseUnsignedIntList((char*)z->sval[0], &nbH);
    ns = parseUnsignedIntList((char*)r->sval[0], &nbN);

    sprintf(str, "%s/%u_results_%s_%s.csv", where->filename[0], timestamp, z->sval[0], r->sval[0]);
    results = fopen(str, "w");

    for(i = 0; i < nbN; i++) {                                          /* Loop on the computational ressources */
        printf("Starting with %u computational ressources\n", ns[i]);
        fprintf(results, "%u", ns[i]);
        fflush(NULL);
        unsigned int j = 0;
        for(; j < nbH; j++) {                                           /* Loop on the length of the sequences */
            unsigned int k = 0;
            state* crt1 = initState();
            state* crt2 = copyState(crt1);
            double averages[2] = {0.0,0.0};
            for(; k < nbSetPoints; k++) {                           /* Loop on the initial states */
                unsigned int l = 0;
                parameters[10] = setPoints[k];

                for(; l < nbSteps; l++) {                               /* Loop on the step */
                    char isTerminal = 0;
                    double reward = 0.0;
                    state* nextState = NULL;
                    sequential_direct_instance* sequential_direct = sequential_direct_initInstance(crt1, discountFactor, hs[j],1);

                    double* optimalAction = sequential_direct_planning(sequential_direct, ns[i]);
                    isTerminal = nextStateReward(crt1, optimalAction, &nextState, &reward) < 0 ? 1 : 0;
                    freeState(crt1);
                    crt1 = nextState;
                    averages[0] += reward;
                    sequential_direct_uninitInstance(&sequential_direct);
                    if(isTerminal)
                        break;
                }
                printf("direct: %u set point done for h=%u\n", k, hs[j]);
                fflush(NULL);

                for(l = 0; l < nbSteps; l++) {                               /* Loop on the step */
                    char isTerminal = 0;
                    double reward = 0.0;
                    state* nextState = NULL;
                    sequential_soo_instance* sequential_soo = sequential_soo_initInstance(crt2, discountFactor, hs[j],1);

                    double* optimalAction = sequential_soo_planning(sequential_soo, ns[i]);
                    isTerminal = nextStateReward(crt2, optimalAction, &nextState, &reward);
                    freeState(crt2);
                    crt2 = nextState;
                    averages[1] += reward;
                    sequential_soo_uninitInstance(&sequential_soo);
                    if(isTerminal)
                        break;
                }
                printf("soo: %u set point done for h=%u\n", k, hs[j]);
                fflush(NULL);
            }
            freeState(crt1);
            freeState(crt2);
            averages[0] = averages[0] /(double)nbSetPoints;
            averages[1] = averages[1] /(double)nbSetPoints;
            printf("Computation with h=%u and n=%u done\n", hs[j], ns[i]);
            fprintf(results, ",%.15f,%.15f", averages[0],averages[1]);
            fflush(NULL);
        }
        printf("Computation with %u computational ressources done\n\n", ns[i]);
        fprintf(results, "\n");
        fflush(NULL);
    }

    fclose(results);

    arg_freetable(argtable, 6);

    free(setPoints);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}
int main(int argc, char* argv[]) {

    double discountFactor = 0.9;

    FILE* initFileFd = NULL;
    double* setPoints = NULL;
    unsigned int nbSetPoints = 0;
    unsigned int maxDepth = 0;
    FILE* results = NULL;
    char str[1024];
    unsigned int i = 0;
    unsigned int minDepth = 1;
    unsigned int crtDepth = 0;
    unsigned int maxNbIterations = 0;
    unsigned int nbSteps = 0;
    unsigned int timestamp = time(NULL);
    int readFscanf = -1;

    optimistic_instance* optimistic = NULL;
    random_search_instance* random_search = NULL;
    uct_instance* uct = NULL;
    uniform_instance* uniform = NULL;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the set points");
    struct arg_int* d2 = arg_int0(NULL, "min", "<n>", "Minimum depth to start from (min>0)");
    struct arg_int* d = arg_int1("d", NULL, "<n>", "Maximum depth of an uniform tree which the number of call per step");
    struct arg_int* s = arg_int1("s", NULL, "<n>", "Number of steps");
    struct arg_int* k = arg_int1("k", NULL, "<n>", "Branching factor of the problem");
    struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs");
    struct arg_end* end = arg_end(7);

    int nerrors = 0;
    void* argtable[7];

    argtable[0] = initFile;
    argtable[1] = d2;
    argtable[2] = d;
    argtable[3] = s;
    argtable[4] = k;
    argtable[5] = where;
    argtable[6] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 7);
        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, 7);
        return EXIT_FAILURE;
    }

    initGenerativeModelParameters();
    K = k->ival[0];
    initGenerativeModel();

    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &nbSetPoints);
    setPoints = (double*)malloc(sizeof(double) * nbSetPoints);
    
    for(; i < nbSetPoints; i++) {
        readFscanf = fscanf(initFileFd, "%s\n", str);
        setPoints[i] = strtod(str, NULL);
    }
    fclose(initFileFd);

    if(d2->count)
        minDepth = d2->ival[0];
    maxDepth = d->ival[0];
    maxNbIterations = K;
    nbSteps = s->ival[0];

    optimistic = optimistic_initInstance(NULL, discountFactor);
    random_search = random_search_initInstance(NULL, discountFactor);
    uct = uct_initInstance(NULL, discountFactor);
    uniform = uniform_initInstance(NULL, discountFactor);

    sprintf(str, "%s/%u_results_%u_%u.csv", where->filename[0], timestamp, K, nbSteps);
    results = fopen(str, "w");

    for(crtDepth = 1; crtDepth < minDepth; crtDepth++)
        maxNbIterations += pow(K, crtDepth+1);

    for(crtDepth = minDepth; crtDepth <= maxDepth; crtDepth++) {
        double averages[4] = {0.0, 0.0, 0.0, 0.0};
        state* crt1 = initState();
        state* crt2 = copyState(crt1);
        state* crt3 = copyState(crt1);
        state* crt4 = copyState(crt1);

        optimistic_resetInstance(optimistic, crt1);
        uct_resetInstance(uct, crt3);
        uniform_resetInstance(uniform, crt4);
        for(i = 0; i < nbSetPoints; i++) {
            unsigned int j = 0;

            parameters[10] = setPoints[i];

            for(; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                optimistic_keepSubtree(optimistic);
                action* optimalAction = optimistic_planning(optimistic, maxNbIterations);
                isTerminal = nextStateReward(crt1, optimalAction, &nextState, &reward);
                freeState(crt1);
                crt1 = nextState;
                averages[0] += reward;
                if(isTerminal < 0)
                    break;
            }
            optimistic_resetInstance(optimistic, crt1);

            printf("Optimistic   : %uth set point processed\n", i + 1);

            for(j = 0; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                random_search_resetInstance(random_search, crt2);
                action* optimalAction = random_search_planning(random_search, maxNbIterations);
                isTerminal = nextStateReward(crt2, optimalAction, &nextState, &reward);
                freeState(crt2);
                crt2 = nextState;
                averages[1] += reward;
                if(isTerminal < 0)
                    break;
            }
            random_search_resetInstance(random_search, crt1);

            printf("Random search: %uth set point processed\n", i + 1);

            for(j = 0; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                uct_keepSubtree(uct);
                action* optimalAction = uct_planning(uct, maxNbIterations);
                isTerminal = nextStateReward(crt3, optimalAction, &nextState, &reward);
                freeState(crt3);
                crt3 = nextState;
                averages[2] += reward;
                if(isTerminal < 0)
                    break;
            }
            uct_resetInstance(uct, crt3);

            printf("Uct          : %uth set point processed\n", i + 1);

            for(j = 0; j < nbSteps; j++) {
                char isTerminal = 0;
                double reward = 0.0;
                state* nextState = NULL;

                uniform_keepSubtree(uniform);
                action* optimalAction = uniform_planning(uniform, maxNbIterations);
                isTerminal = nextStateReward(crt4, optimalAction, &nextState, &reward);
                freeState(crt4);
                crt4 = nextState;
                averages[3] += reward;
                if(isTerminal < 0)
                    break;
            }
            uniform_resetInstance(uniform, crt4);

            printf("Uniform      : %uth set point processed\n", i + 1);
            printf(">>>>>>>>>>>>>> %uth set point processed\n", i + 1);

        }

        fprintf(results, "%u,%.15f,%.15f,%.15f,%.15f\n", maxNbIterations, averages[0] / (double)nbSetPoints, averages[1] / (double)nbSetPoints, averages[2] / (double)nbSetPoints, averages[3] / (double)nbSetPoints);
        fflush(results);
        freeState(crt1);
        freeState(crt2);
        freeState(crt3);
        freeState(crt4);

        printf(">>>>>>>>>>>>>> %u depth done\n\n", crtDepth);

        maxNbIterations += pow(K, crtDepth+1);

    }

    fclose(results);

    arg_freetable(argtable, 7);

    free(setPoints);

    optimistic_uninitInstance(&optimistic);
    random_search_uninitInstance(&random_search);
    uct_uninitInstance(&uct);
    uniform_uninitInstance(&uniform);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}
Exemple #25
0
void killMenu(void)
{
	fadeOut();
	freeMenuScene();
	freeState(NULL);
}
Exemple #26
0
// the exposed libscalpel API
// NOTE: This function is deprecated and will be removed. Use the
// libscalpel_* functions instead.
// TODO make the driver in scalpel_exec.c use this (minor refactoring needed)
// TODO add support for the remaining options avail from cmd-line
// returns SCALPEL_OK on no error, can throw runtime_error exception on errors
int scalpel_carveSingleInput(ScalpelInputReader * const reader, const char * const confFilePath,
    const char * const outDir, const unsigned char generateFooterDb,
    const unsigned char handleEmbedded, const unsigned char organizeSubdirs,
    const unsigned char previewMode,
    const unsigned char carveWithMissingFooters,
    const unsigned char noSearchOverlap) throw (std::runtime_error) {

    if (!reader || ! confFilePath || ! outDir) {
        //invalid args
        throw std::runtime_error("Invalid empty arguments");
    }

    if (!reader->dataSource || !reader->id) {
        throw std::runtime_error("Invalid empty input reader arguments");
    }

    //check fns
    if (!reader->open || !reader->read || !reader->seeko || !reader->tello
        || !reader->close || !reader->getError || !reader->getSize) {
            throw std::runtime_error("Reader callbacks not setup");
    }

    struct scalpelState state;

    std::string processorName ("scalpel_carveSingleInput()");
    char * args[5];
    args[0] = const_cast<char*> ( processorName.c_str());
    args[1] = reader->id;
    args[2] = const_cast<char*> (confFilePath);
    args[3] = const_cast<char*> (outDir);
    args[4] = 0;


    initializeState(args, &state);

    //setup input
    state.inReader = reader;

    //setup options
    const size_t outDirLen = strlen(outDir);
    strncpy(state.outputdirectory, outDir, outDirLen);
    state.outputdirectory[outDirLen] = 0;
    const size_t confFilePathLen = strlen(confFilePath);
    strncpy(state.conffile, confFilePath, confFilePathLen);
    state.conffile[confFilePathLen] = 0;
    state.generateHeaderFooterDatabase = generateFooterDb;
    state.handleEmbedded = handleEmbedded;
    state.organizeSubdirectories = organizeSubdirs;
    state.previewMode = previewMode;
    state.carveWithMissingFooters = carveWithMissingFooters;
    state.noSearchOverlap = noSearchOverlap;

    convertFileNames(&state);

    // read configuration file
    int err;
    if ((err = readSearchSpecFile(&state))) {
        // problem with config file
        handleError(&state, err); //can throw
        freeState(&state);
        std::stringstream ss;
        ss << "Error reading spec file, error code: " << err;
        throw std::runtime_error(ss.str());
    }

    // prepare audit file and make sure output directory is empty.
    if ((err = openAuditFile(&state))) {
        handleError(&state, err); //can throw
        freeState(&state);
        std::stringstream ss;
        ss << "Error opening audit file, error code: " << err;
        throw std::runtime_error(ss.str());
    }

    // Initialize the backing store of buffer to read-in, process image data.
    init_store();

    // Initialize threading model for cpu or gpu search.
    init_threading_model(&state);

    if ((err = digImageFile(&state))) {
        handleError(&state, err); //can throw
        closeAuditFile(state.auditFile);
        destroyStore();
        freeState(&state);
        std::stringstream ss;
        ss << "Error digging file, error code: " << err;
        throw std::runtime_error(ss.str());
    }

    if ((err = carveImageFile(&state))) {
        handleError(&state, err); //can throw
        closeAuditFile(state.auditFile);
        destroy_threading_model(&state);
        destroyStore();
        freeState(&state);
        std::stringstream ss;
        ss << "Error carving file, error code: " << err;
        throw std::runtime_error(ss.str());
    }

    closeAuditFile(state.auditFile);
    destroy_threading_model(&state);
    destroyStore();
    freeState(&state);

    return SCALPEL_OK;
}
int main(int argc, char* argv[]) {

#ifdef BALL
    double discountFactor = 0.9;
#else
#ifdef CART_POLE
    double discountFactor = 0.95;
#else
#ifdef DOUBLE_CART_POLE
    double discountFactor = 0.95;
#else
#ifdef MOUNTAIN_CAR
    double discountFactor = 0.99;
#else
#ifdef ACROBOT
    double discountFactor = 0.95;
#else
#ifdef BOAT
    double discountFactor = 0.95;
#else
#ifdef SWIMMER
    double discountFactor = 0.95;
#endif
#endif
#endif
#endif
#endif
#endif
#endif

    FILE* initFileFd = NULL;
    state** initialStates = NULL;
    FILE* results = NULL;
    char str[1024];
    unsigned int i = 0;
    unsigned int nbInitialStates = 0;
    unsigned int* ns = NULL;
    unsigned int nbN = 0;
    double* Ls = NULL;
    unsigned int nbL = 0;
    unsigned int nbSteps = 0;
    unsigned int timestamp = time(NULL);
    int readFscanf = -1;

    lipschitzian_instance* lipschitzian = NULL;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the inital state");
    struct arg_int* s = arg_int1("s", NULL, "<n>", "Number of steps");
    struct arg_str* r = arg_str1("n", NULL, "<s>", "List of ressources");
    struct arg_str* z = arg_str1("L", NULL, "<s>", "List of Lipschitz coefficients to try");
    struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs");
    struct arg_end* end = arg_end(6);

    int nerrors = 0;
    void* argtable[6];

    argtable[0] = initFile;
    argtable[1] = r;
    argtable[2] = s;
    argtable[3] = z;
    argtable[4] = where;
    argtable[5] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 6);
        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, 6);
        return EXIT_FAILURE;
    }

    initGenerativeModelParameters();
    initGenerativeModel();

    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &nbInitialStates);
    initialStates = (state**)malloc(sizeof(state*) * nbInitialStates);
    
    for(; i < nbInitialStates; i++) {
        readFscanf = fscanf(initFileFd, "%s\n", str);
        initialStates[i] = makeState(str);
    }
    fclose(initFileFd);

    nbSteps = s->ival[0];
    Ls = parseDoubleList((char*)z->sval[0], &nbL);
    ns = parseUnsignedIntList((char*)r->sval[0], &nbN);

    sprintf(str, "%s/%u_results_%s_%s.csv", where->filename[0], timestamp, z->sval[0], r->sval[0]);
    results = fopen(str, "w");

    lipschitzian = lipschitzian_initInstance(NULL, discountFactor, 0.0);
    
    for(i = 0; i < nbN; i++) {                                               /* Loop on the computational ressources */
        fprintf(results, "%u", ns[i]);
        printf("Starting with %u computational ressources\n", ns[i]);
        fflush(NULL);
        unsigned int j = 0;
        for(; j < nbL; j++) {                                           /* Loop on the Lispchitz constant */
            unsigned int k = 0;
            double average = 0.0;
            lipschitzian->L = Ls[j];
            for(; k < nbInitialStates; k++) {                           /* Loop on the initial states */
                unsigned int l = 0;
                double sumRewards = 0.0;
                state* crt = copyState(initialStates[k]);

                lipschitzian_resetInstance(lipschitzian, crt);
                for(; l < nbSteps; l++) {                               /* Loop on the step */
                    char isTerminal = 0;
                    double reward = 0.0;
                    state* nextState = NULL;

                    double* optimalAction = lipschitzian_planning(lipschitzian, ns[i]);
                    isTerminal = nextStateReward(crt, optimalAction, &nextState, &reward) < 0 ? 1 : 0;
                    free(optimalAction);
                    freeState(crt);
                    crt = nextState;
                    sumRewards += reward;
                    lipschitzian_resetInstance(lipschitzian,crt);
                    if(isTerminal)
                        break;
                }
                average += sumRewards;
                freeState(crt);
                printf("Computation of the %u initial state done with L=%f\n", k, Ls[j]);
                fflush(NULL);
            }
            average = average /(double)nbInitialStates;
            fprintf(results, ",%.15f", average);
            printf("Computation with L=%f and n=%u done\n", Ls[j], ns[i]);
            fflush(NULL);
        }
        fprintf(results,"\n");
        printf("Computation with %u computational ressources done\n\n", ns[i]);
        fflush(NULL);
    }

    fclose(results);

    arg_freetable(argtable, 6);

    for(i = 0; i < nbInitialStates; i++)
        freeState(initialStates[i]);

    free(initialStates);

    lipschitzian_uninitInstance(&lipschitzian);

    free(ns);
    free(Ls);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}
int main(int argc, char* argv[]) {

    double discountFactor = 0.9;

    FILE* initFileFd = NULL;
    double* setPoints = NULL;
    FILE* results = NULL;
    char str[1024];
    unsigned int i = 0;
    unsigned int nbSetPoints = 0;
    unsigned int* ns = NULL;
    unsigned int nbN = 0;
    unsigned int* hs = NULL;
    unsigned int nbH = 0;
    unsigned int nbSteps = 0;
    unsigned int nbIterations = 0;
    unsigned int timestamp = time(NULL);
    int readFscanf = -1;
    random_search_instance* random_search = NULL;

    struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the set points");
    struct arg_int* s = arg_int1("s", NULL, "<n>", "Number of steps");
    struct arg_int* it = arg_int1("i", NULL, "<n>", "Number of iterations");
    struct arg_str* r = arg_str1("n", NULL, "<s>", "List of ressources");
    struct arg_str* d = arg_str1("h", NULL, "<s>", "List of depth");
    struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs");
    struct arg_end* end = arg_end(7);

    int nerrors = 0;
    void* argtable[7];

    argtable[0] = initFile;
    argtable[1] = r;
    argtable[2] = d;
    argtable[3] = s;
    argtable[4] = it;
    argtable[5] = where;
    argtable[6] = end;

    if(arg_nullcheck(argtable) != 0) {
        printf("error: insufficient memory\n");
        arg_freetable(argtable, 7);
        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, 7);
        return EXIT_FAILURE;
    }

    initGenerativeModelParameters();
    initGenerativeModel();

    initFileFd = fopen(initFile->filename[0], "r");
    readFscanf = fscanf(initFileFd, "%u\n", &nbSetPoints);
    setPoints = (double*)malloc(sizeof(double) * nbSetPoints);
    
    for(; i < nbSetPoints; i++) {
        readFscanf = fscanf(initFileFd, "%s\n", str);
        setPoints[i] = strtod(str, NULL);
    }
    fclose(initFileFd);

    nbSteps = s->ival[0];
    nbIterations = it->ival[0];
    ns = parseUnsignedIntList((char*)r->sval[0], &nbN);
    hs = parseUnsignedIntList((char*)d->sval[0], &nbH);

    sprintf(str, "%s/%u_results_random_search_%s.csv", where->filename[0], timestamp, r->sval[0]);
    results = fopen(str, "w");

    random_search = random_search_initInstance(NULL, discountFactor);
    h_max = h_max_crt_depth;

    for(i = 0; i < nbIterations; i++) {
        unsigned int j = 0;

        for(;j < nbH; j++) {
            unsigned int k = 0;

            crtDepth = hs[j];

            for(; k < nbN; k++) {                                          /* Loop on the computational ressources */
                state* crt = initState();
                double average = 0.0;
                unsigned int l = 0;

                printf("Starting with %u computational ressources\n", ns[k]);
                fprintf(results, "%u,%u", hs[j], ns[k]);
                fflush(NULL);

                for(; l < nbSetPoints; l++) {                           /* Loop on the initial states */
                    unsigned int m = 0;
                    parameters[10] = setPoints[l];

                    for(; m < nbSteps; m++) {                               /* Loop on the step */
                        char isTerminal = 0;
                        double reward = 0.0;
                        state* nextState = NULL;
                        random_search_resetInstance(random_search, crt);

                        double* optimalAction = random_search_planning(random_search, ns[k]);
                        isTerminal = nextStateReward(crt, optimalAction, &nextState, &reward) < 0 ? 1 : 0;
                        freeState(crt);
                        crt = nextState;
                        average += reward;
                        if(isTerminal)
                            break;
                    }

                    random_search_resetInstance(random_search, crt);
                    printf("random search: %u set point done\n", l);
                    fflush(NULL);
                }

                freeState(crt);
                average = average /(double)nbSetPoints;
                printf("Computation with %u computational ressources done\n\n", ns[k]);
                fprintf(results, ",%.15f\n", average);
                fflush(NULL);
            }

            printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>Depth %u done\n", hs[j]);
            fflush(NULL);
        }

        printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ITERATION %u DONE<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",i+1);
        fprintf(results,"\n");
        fflush(NULL);
    }
 
    fclose(results);

    arg_freetable(argtable, 7);

    free(setPoints);

    freeGenerativeModel();
    freeGenerativeModelParameters();

    return EXIT_SUCCESS;

}