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;
    }
}
/*
Based on code from :

Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans  http://continuousphysics.com/Bullet/
*/
void ompl::base::SO3StateSpace::interpolate(const State *from, const State *to, const double t, State *state) const
{
    assert(fabs(norm(static_cast<const StateType*>(from)) - 1.0) < MAX_QUATERNION_NORM_ERROR);
    assert(fabs(norm(static_cast<const StateType*>(to)) - 1.0) < MAX_QUATERNION_NORM_ERROR);

    double theta = arcLength(from, to);
    if (theta > std::numeric_limits<double>::epsilon())
    {
        double d = 1.0 / sin(theta);
        double s0 = sin((1.0 - t) * theta);
        double s1 = sin(t * theta);

        const StateType *qs1 = static_cast<const StateType*>(from);
        const StateType *qs2 = static_cast<const StateType*>(to);
        StateType       *qr  = static_cast<StateType*>(state);
        double dq = qs1->x * qs2->x + qs1->y * qs2->y + qs1->z * qs2->z + qs1->w * qs2->w;
        if (dq < 0)  // Take care of long angle case see http://en.wikipedia.org/wiki/Slerp
            s1 = -s1;

        qr->x = (qs1->x * s0 + qs2->x * s1) * d;
        qr->y = (qs1->y * s0 + qs2->y * s1) * d;
        qr->z = (qs1->z * s0 + qs2->z * s1) * d;
        qr->w = (qs1->w * s0 + qs2->w * s1) * d;
    }
    else
    {
        if (state != from)
            copyState(state, from);
    }
}
Exemple #3
0
//==============================================================================
SE2::ScopedState SE2::cloneState(const StateSpace::State* stateIn) const
{
  auto newState = createState();
  copyState(stateIn, newState);

  return newState;
}
Exemple #4
0
Board::Board( const Board & other )
	:	QAbstractListModel( Q_NULLPTR )
	,	m_figures( std::move( initFigures() ) )
	,	m_whiteKing( static_cast< King* > ( m_figures.at( 20 ).data() ) )
	,	m_blackKing( static_cast< King* > ( m_figures.at( 4 ).data() ) )
{
	copyState( other );
}
    /**
     * Proces the slow and fast aggregator and copy between them if needed. 
     *The aggregators are implicitly copied  
     * The copy is triggered when the slow filter process a stream that was dropped by the fast filter. 
     */
    void step()
    {
	
	// call the slow aggregator streams in the relevant order
	while( slow_aggr->step() );
	
	//this logic is for determining if a copy is needed, so only triggered if there is no pending copy 
	if(!copy)
	{
	    //Check which streams were dropped by the fast filter, but weren't dropped by the slow filter.
	    for( int i = 0; i < stream_size; i++)
	    {
		const aggregator::StreamStatus &status_fast( fast_aggr->getBufferStatus(i) );
		const aggregator::StreamStatus &status_slow( slow_aggr->getBufferStatus(i) );
		
		int total_stream_dropped_fast =  status_fast.samples_dropped_buffer_full + status_fast.samples_dropped_late_arriving;
		int total_stream_dropped_slow =  status_slow.samples_dropped_buffer_full + status_slow.samples_dropped_late_arriving;
		int current_dif = total_stream_dropped_fast - total_stream_dropped_slow; 
		//verify if a stream was dropped by the fast filter, but wasen't dropped by the slow filter
		if( current_dif > 0 )
		{
		    //verify if that stream of that type was procces by the slow filter 
		    if( prev_num_processed_streams_slow[i] < status_slow.samples_processed)
		    {
			//if the stream process was of a type dropped by the fast filter there need to be a copy
			copy = true; 
			break; 
		    }
		}

		prev_num_processed_streams_slow[i] = status_slow.samples_processed;

	    }
	    
	}
	 
	//std::cout << " MUST COPY " << copy << std::endl; 
	//verify if there is a need to procces the fast aggregator 
	if ( slow_aggr->getLatency().toSeconds() >  fast_aggr->getTimeOut().toSeconds() )
	{
	    if( copy ) 
	    {
		copy = false; 
		fast_aggr->copyState( *slow_aggr );
		copyState(); 
		
		//since there was a copy this get the new ammount of stream dropped 
		for( int i = 0; i < stream_size; i++)
		{
			const aggregator::StreamStatus &status_slow( slow_aggr->getBufferStatus(i) );
			prev_num_processed_streams_slow[i] = status_slow.samples_processed;
		}
	    }
	while( fast_aggr->step() );

	}

    }
int findDrawLocation(){
    int cycle =currentState.round - twoOfPowerState.round;
    State state1;
    State state2;
    copyState(&state1, &initState);
    copyState(&state2, &initState);

    int cycleCount = cycle;
    while(cycleCount-- /** debugging , cycle--**/ ){
        dispatchOneCard( &state2);
        darwCardsFromPack( &state2);
        state2.round++;
        getWhichTurn( &state2);
    }

    int i=15;  /** int i=14;  debugging, because round is start by 1**/

    for ( ;i<=currentState.round;i++  ){

        dispatchOneCard( &state1);
        darwCardsFromPack( &state1);

        dispatchOneCard( &state2);
        darwCardsFromPack( &state2);

        if (isDraw(&state1 , &state2) ==1 ){
            return state1.round + cycle; /**debugging  return state1.round; **/
        }

        state1.round++;
        state2.round++;
        getWhichTurn( &state1);
        getWhichTurn( &state2);
    }


    return -1;
}
Exemple #7
0
Board &
Board::operator = ( const Board & other )
{
	if( this != &other )
	{
		m_figures = std::move( initFigures() );
		m_whiteKing = static_cast< King* > ( m_figures.at( 20 ).data() );
		m_blackKing = static_cast< King* > ( m_figures.at( 4 ).data() );

		copyState( other );
	}

	return *this;
}
Exemple #8
0
bool ompl::base::SpaceInformation::searchValidNearby(State *state, const State *near, double distance, unsigned int attempts) const
{
    if (satisfiesBounds(near) && isValid(near))
    {
        if (state != near)
            copyState(state, near);
        return true;
    }
    else
    {
        // try to find a valid state nearby
        UniformValidStateSampler *uvss = new UniformValidStateSampler(this);
        uvss->setNrAttempts(attempts);
        return searchValidNearby(ValidStateSamplerPtr(uvss), state, near, distance);
    }
}
void ompl::control::SpaceInformation::propagate(const base::State *state, const Control *control, int steps,
                                                base::State *result) const
{
    if (steps == 0)
    {
        if (result != state)
            copyState(result, state);
    }
    else
    {
        double signedStepSize = steps > 0 ? stepSize_ : -stepSize_;
        steps = abs(steps);

        statePropagator_->propagate(state, control, signedStepSize, result);
        for (int i = 1; i < steps; ++i)
            statePropagator_->propagate(result, control, signedStepSize, result);
    }
}
sequential_soo_instance* sequential_soo_initInstance(state* initial, double gamma, unsigned int H, char dropTerminal) {

    unsigned int i = 0;
    sequential_soo_instance* newInstance = (sequential_soo_instance*)malloc(sizeof(sequential_soo_instance));
    newInstance->H = H;
    newInstance->initial = copyState(initial);
    newInstance->instances = (soo**)malloc(sizeof(soo*) * H);
    for(;i < H; i++)
        newInstance->instances[i] = soo_init(hMax);
    newInstance->gamma = gamma;
    for(i = 0; i < NUMBER_OF_DIMENSIONS_OF_ACTION; i++)
        newInstance->crtOptimalAction[i] = 0.5;
    newInstance->crtMaxSumOfDiscountedRewards = 0.0;
    newInstance->rewards = (double*)malloc(sizeof(double) * H);
    newInstance->crtNbEvaluations = 0;
    newInstance->dropTerminal = dropTerminal;
    return newInstance;

}
void copyStates( ESMoL::Stateflow inputStateflow, ESMoL::Stateflow outputStateflow ) {

	getConnectorRefList().clear();
	getTransConnectorMap().clear();

	StateVector stateVector = inputStateflow.State_kind_children();
	for( StateVector::iterator stvItr = stateVector.begin() ; stvItr != stateVector.end() ; ++stvItr ) {
		ESMoL::State inputState = *stvItr;
		ESMoL::State outputState = ESMoL::State::Create( outputStateflow );
		copyState( inputState, outputState );
	}
	for( ConnectorRefList::iterator crlItr = getConnectorRefList().begin() ; crlItr != getConnectorRefList().end() ; ++crlItr ) {
		ESMoL::ConnectorRef inputConnectorRef = *crlItr;
		ESMoL::TransConnector inputTransConnector = inputConnectorRef.ref();
		if ( inputTransConnector == Udm::null ) {
			std::cerr << "Warning:  ConnectorRef is a null reference!" << std::endl;
			continue;
		}

		TransConnectorMap::iterator tcmItr = getTransConnectorMap().find( inputConnectorRef );
		if ( tcmItr == getTransConnectorMap().end() ) {
			std::cerr << "Warning:  ConnectorRef has no copy in TransConnectorMap" << std::endl;
			continue;
		}
		ESMoL::TransConnector outputTransConnector = tcmItr->second;
		if ( outputTransConnector.type() != ESMoL::ConnectorRef::meta ) {
			std::cerr << "Warning:  TransConnector should be a ConnectorRef" << std::endl;
			continue;
		}
		ESMoL::ConnectorRef outputConnectorRef = ESMoL::ConnectorRef::Cast( outputTransConnector );

		tcmItr = getTransConnectorMap().find( inputTransConnector );
		if ( tcmItr == getTransConnectorMap().end() ) {
			std::cerr << "Warning:  TransConnector has no copy in TransConnectorMap" << std::endl;
			continue;
		}
		outputTransConnector = tcmItr->second;

		outputConnectorRef.ref() = outputTransConnector;
	}
}
Exemple #12
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;
}
	AlphaReal FeaturewiseLearner::run( vector<int>& colIndices ) 
	{ 
		AlphaReal bestEnergy = -numeric_limits<AlphaReal>::max();
		FeaturewiseLearner* bestClassifier = NULL;
		
		// for each fearture given in colIndices run the learner
		for( vector<int>::iterator it = colIndices.begin(); it != colIndices.end(); ++it )
		{
			AlphaReal tmpEnergy = this->run(*it);
			if ( tmpEnergy > bestEnergy )
			{
				// keep the best classifier
				if (bestClassifier) delete bestClassifier;
				bestClassifier = reinterpret_cast<FeaturewiseLearner*>(copyState());
				bestEnergy = tmpEnergy;				
			}
		}
				
		// copy the best classifier 
		subCopyState(bestClassifier);
		delete bestClassifier;	
		
		return bestEnergy; 
	};	
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[]) {

#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;

}
Exemple #16
0
unsigned int ompl::base::SpaceInformation::getMotionStates(const State *s1, const State *s2,
                                                           std::vector<State *> &states, unsigned int count,
                                                           bool endpoints, bool alloc) const
{
    // add 1 to the number of states we want to add between s1 & s2. This gives us the number of segments to split the
    // motion into
    count++;

    if (count < 2)
    {
        unsigned int added = 0;

        // if they want endpoints, then at most endpoints are included
        if (endpoints)
        {
            if (alloc)
            {
                states.resize(2);
                states[0] = allocState();
                states[1] = allocState();
            }
            if (states.size() > 0)
            {
                copyState(states[0], s1);
                added++;
            }

            if (states.size() > 1)
            {
                copyState(states[1], s2);
                added++;
            }
        }
        else if (alloc)
            states.resize(0);
        return added;
    }

    if (alloc)
    {
        states.resize(count + (endpoints ? 1 : -1));
        if (endpoints)
            states[0] = allocState();
    }

    unsigned int added = 0;

    if (endpoints && states.size() > 0)
    {
        copyState(states[0], s1);
        added++;
    }

    /* find the states in between */
    for (unsigned int j = 1; j < count && added < states.size(); ++j)
    {
        if (alloc)
            states[added] = allocState();
        stateSpace_->interpolate(s1, s2, (double)j / (double)count, states[added]);
        added++;
    }

    if (added < states.size() && endpoints)
    {
        if (alloc)
            states[added] = allocState();
        copyState(states[added], s2);
        added++;
    }

    return added;
}
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 #18
0
// Test the discardCard() function
//
// discardCard():
// Removes card from player's hand and either puts it in the played pile
// (if not trashed) or not (if trashed).
//
// handPos       - enumerated card
// currentPlayer - the index of the current player
// state         - holds the game state
// trashFlag     - determines whether the card is trashed (>= 1) or not (< 1)
//
// Always returns 0.
//
int testDiscardCard(int handPos, int currentPlayer, struct gameState *state, int trashFlag)
{
    struct gameState *origState;  // copy of game state

    // Make a copy of the original game state
    //
    origState = copyState(state);

    // Run discardCard()
    //
    discardCard(handPos, currentPlayer, state, trashFlag);

    // If the card was trashed, the card should not be added to the played
    // card pile and played card count should not increment. Otherwise,
    // add the card to the played card pile and increment the count.
    //
    if(trashFlag >= 1)
    {
        if(state->playedCardCount == origState->playedCardCount)
        {
            printf("discardCard: PASS trashed card did not increment playedCardCount\n");
        }
        else
        {
            printf("discardCard: FAIL trashed card incremented playedCardCount\n");
        }
    }
    else
    {
        // If the card was NOT trashed, the card SHOULD be added to the played
        // card pile and played card count SHOULD increment
        //
        if(state->playedCardCount == origState->playedCardCount + 1)
        {
            printf("discardCard: PASS non-trashed card incremented playedCardCount\n");
        }
        else
        {
            printf("discardCard: FAIL non-trashed card did not incremenet playedCardCount\n");
        }

        if(state->playedCards[state->playedCardCount-1] == origState->hand[currentPlayer][handPos])
        {
            printf("discardCard: PASS non-trashed card added to played card pile\n");
        }
        else
        {
            printf("discardCard: FAIL non-trashed card not added to played card pile\n");
        }
    }

    // Remove the card from the player's hand
    //
    if(handPos == origState->handCount[currentPlayer] - 1)
    {
        if(state->handCount[currentPlayer] == origState->handCount[currentPlayer] - 1)
        {
            printf("discardCard: PASS card removal decremented current player handcount\n");
        }
        else
        {
            printf("discardCard: FAIL card removal did not decrement current player handcount\n");
        }
    }
    else if(origState->handCount[currentPlayer] == 1)
    {
        if(state->handCount[currentPlayer] == origState->handCount[currentPlayer] - 1)
        {
            printf("discardCard: PASS card removal decremented current player handcount\n");
        }
        else
        {
            printf("discardCard: FAIL card removal did not decrement current player handcount\n");
        }
    }
    else
    {
        // Check if discarded card was replaced with last card in hand
        //
        if(state->hand[currentPlayer][handPos] == origState->hand[currentPlayer][(origState->handCount[currentPlayer] - 1)])
        {
            printf("discardCard: PASS discarded card was replaced with last card in hand\n");
        }
        else
        {
            printf("discardCard: FAIL discarded card was not replaced with last card in hand\n");
        }

        // Check if last card was set to -1
        //
        if(state->hand[currentPlayer][origState->handCount[currentPlayer] - 1] == -1)
        {
            printf("discardCard: PASS last card set to -1\n");
        }
        else
        {
            printf("discardCard: FAIL last card not set to -1\n");
        }

        // Check if the number of cards in hand was reduced
        //
        if(state->handCount[currentPlayer] == origState->handCount[currentPlayer] - 1)
        {
            printf("discardCard: PASS card removal decremented current player handcount\n");
        }
        else
        {
            printf("discardCard: FAIL card removal did not decrement current player handcount\n");
        }
    }
    
    // Report what, if anything, changed in the game state
    //
    whatChanged(origState, state);
    printf("\n");

    return 0;
}
int main(){

    #if !defined(ONLINE_JUDGE)
        freopen("246.in","r",stdin);
        ///freopen("error_input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    currentState = getNewState();
    initState = getNewState();
    twoOfPowerState = getNewState();

    int i=0;


    int topCard=0;
    while ( scanf("%d",&topCard) != EOF && topCard ){

        clearState(&currentState);
        clearState(&initState);
        clearState(&twoOfPowerState);

        currentState.handCard[51] = topCard;
        for(i=0;i<51;i++){
            scanf("%d",&currentState.handCard[50-i]);
        }
        currentState.handCardLength =52;

        currentState.round = 1; /** debugging for round start with 1 **/

        for (i=1;i<=14;i++){

           /** turn = getWhichTurn( &currentState);  debug for all cardsLength is zero **/



           int handCard = currentState.handCard[ (currentState.handCardLength -1) ];
           int cardLength = currentState.cardsLength[currentState.whichTurn];

           currentState.cards[currentState.whichTurn][ cardLength] =handCard;

           currentState.handCardLength--;
           currentState.cardsLength[currentState.whichTurn]++;


           if ( is2OfPower(i) >0 ){
                copyState( &twoOfPowerState , &currentState);
           }

           outputForDebug(&currentState);

           /** debugging , put on the last part after dispatching card.**/
           currentState.whichTurn= (currentState.whichTurn +1 ) % 7;
           currentState.round ++;  /** debugging put it than copyState **/


        }

        copyState(&initState, &currentState);  /** use initSate when try to find loop's location. **/


        while (1){

              /** for debugging
              if ( currentState.round == 82  ){
                 int bbb=1;
              }
              **/


               dispatchOneCard( &currentState);
               darwCardsFromPack( &currentState);


               if ( currentState.handCardLength == 52){
                   printf("Win : %d\n",  currentState.round);
                   break;
               }else if ( currentState.handCardLength ==0 ){
                   printf("Loss: %d\n",  currentState.round);
                   break;
               }else if (isDraw(&currentState, &twoOfPowerState)>0 ){
                   int roundLocation = findDrawLocation();
                   printf("Draw: %d\n", roundLocation);
                   break;
               }

               /** debugging, should be put behind isDraw()!!
                    before copying  currentState to towOfPowerState,
                    it should be done the isDraw action.
                **/
               if ( is2OfPower(currentState.round  ) >0  ){  /**debugging is2OfPower(i) **/
                    copyState( &twoOfPowerState , &currentState);
               }

               currentState.round++;
               getWhichTurn( &currentState );

        }


    }


    return 0;
}
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 #22
0
// Test the treasure map card
//
// NOTE: YOU ADDED A BUG: changed the code that trashes both treasure
// cards to no longer trash them (trashFlag changed from 1 to -1).
//
// From the Dominion Card Game Wiki (dominioncg.wikia.com):
//
// Trash this and another copy of treasure map from your hand. If you do
// trash two treasure maps, gain 4 gold cards, putting them on top of your
// deck.
//
// If you play this and you do not have another treasure map card in your
// hand, you gain nothing. Two cards are required to get gold.
//
// If there aren't enough gold cards left, you gain what you can.
//
int testTreasureMapCard(struct gameState *state, int handPos, int currentPlayer)
{
    struct gameState *origState;  // copy of game state
    int origNumTreasureMap = 0;
    int newNumTreasureMap = 0;
    int newTopGolds = 0;
    int origTopGolds = 0;
    int idx;

    // Make a copy of the original game state
    //
    origState = copyState(state);

    treasureMapCard(state, handPos);

    for(idx = 0; idx < origState->handCount[currentPlayer]; idx++)
    {
        if(origState->hand[currentPlayer][idx] == treasure_map)
            origNumTreasureMap++;
    }
    for(idx = 0; idx < state->handCount[currentPlayer]; idx++)
    {
        if(state->hand[currentPlayer][idx] == treasure_map)
            newNumTreasureMap++;
    }

    // See how many cards on top of the original deck were golds (if any)
    //
    for(idx = origState->deckCount[currentPlayer]-1; idx > 0 && idx > *(origState->deck[currentPlayer])-5; idx--)
    {
        if(origState->deck[currentPlayer][idx] == gold)
            origTopGolds++;
    }

    // See how many cards on top of the new deck are golds (if any)
    //
    for(idx = state->deckCount[currentPlayer]-1; idx > 0 && idx > *(state->deck[currentPlayer])-5; idx--)
    {
        if(state->deck[currentPlayer][idx] == gold)
            newTopGolds++;
    }

    // See if golds were added correctly
    //
    if(origNumTreasureMap >= 2)
    {
        // We started with two treasure map cards. See how many were
        // discarded.
        //
        if(origNumTreasureMap - newNumTreasureMap == 2)
        {
            // We discarded two treasure map cards. Check golds.
            //
            if(newTopGolds == 4 && newTopGolds - origTopGolds == 4)
            {
                printf("treasureMapCard: PASS two TMs discarded, four golds added to top of deck\n");
            }
            else 
            {
                printf("treasureMapCard: FAIL two TMs discarded, four golds not added to top of deck\n");
            }
        }
        // We did not discard two treasure map cards. Check golds.
        //
        if(newTopGolds == 4 && newTopGolds - origTopGolds == 4)
        {
            printf("treasureMapCard: FAIL did not discard two TMs, four golds added to top of deck\n");
        }
        else 
        {
            printf("treasureMapCard: PASS did not discard two TMs, golds not added to top of deck\n");
        }

    }
    else 
    {
        // We started with one  treasure map card. Discarding it does
        // nothing but discard a card. Check to make sure no golds were
        // added to the top of the deck.
        //
        if(newTopGolds == 4 && newTopGolds - origTopGolds == 4)
        {
            printf("treasureMapCard: FAIL did not discard two TMs, four golds added to top of deck\n");
        }
        else 
        {
            printf("treasureMapCard: PASS did not discard two TMs, golds not added to top of deck\n");
        }
    }

    // Report what, if anything, changed in the game state
    //
    whatChanged(origState, state);
    printf("\n");

    return 0;
}
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;

}
void copyState( ESMoL::State inputState, ESMoL::State outputState ) {

	UdmEngine::copyState( inputState, outputState );

	TransStartVector transStartVector = inputState.TransStart_kind_children();
	for( TransStartVector::iterator tsvItr = transStartVector.begin() ; tsvItr != transStartVector.end() ; ++tsvItr ) {
		ESMoL::TransStart inputTransStart = *tsvItr;
		ESMoL::TransStart outputTransStart = ESMoL::TransStart::Create( outputState );
		getTransConnectorMap().insert(  std::make_pair( inputTransStart, outputTransStart )  );
	}

	DataVector dataVector = inputState.Data_kind_children();
	for( DataVector::iterator dtvItr = dataVector.begin() ; dtvItr != dataVector.end() ; ++dtvItr ) {
		copyData(  *dtvItr, ESMoL::Data::Create( outputState )  );
	}

	EventVector eventVector = inputState.Event_kind_children();
	for( EventVector::iterator envItr = eventVector.begin() ; envItr != eventVector.end() ; ++envItr ) {
		copyEvent(  *envItr, ESMoL::Event::Create( outputState )  );
	}

	JunctionVector junctionVector = inputState.Junction_kind_children();
	for( JunctionVector::iterator jnvItr = junctionVector.begin() ; jnvItr != junctionVector.end() ; ++jnvItr ) {
		ESMoL::Junction inputJunction = *jnvItr;
		ESMoL::Junction outputJunction = ESMoL::Junction::Create( outputState );
		getTransConnectorMap().insert(  std::make_pair( inputJunction, outputJunction )  );
	}

	StateVector stateVector = inputState.State_kind_children();
	for( StateVector::iterator stvItr = stateVector.begin() ; stvItr != stateVector.end() ; ++stvItr ) {
		ESMoL::State inputSubState = *stvItr;
		ESMoL::State outputSubState = ESMoL::State::Create( outputState );
		getTransConnectorMap().insert(  std::make_pair( inputSubState, outputSubState )  );
		copyState( inputSubState, outputSubState );
	}

	ConnectorRefVector connectorRefVector = inputState.ConnectorRef_kind_children();
	for( ConnectorRefVector::iterator jnvItr = connectorRefVector.begin() ; jnvItr != connectorRefVector.end() ; ++jnvItr ) {
		ESMoL::ConnectorRef inputConnectorRef = *jnvItr;
		ESMoL::ConnectorRef outputConnectorRef = ESMoL::ConnectorRef::Create( outputState );
		getTransConnectorMap().insert(  std::make_pair( inputConnectorRef, outputConnectorRef )  );
		getConnectorRefList().push_back( inputConnectorRef );
	}

	TransitionVector transitionVector = inputState.Transition_kind_children();
	for( TransitionVector::iterator trvItr = transitionVector.begin() ; trvItr != transitionVector.end() ; ++trvItr ) {
		ESMoL::Transition inputTransition = *trvItr;

		ESMoL::TransConnector inputSrcTransConnector = inputTransition.srcTransition_end();
		TransConnectorMap::iterator tcmItr = getTransConnectorMap().find( inputSrcTransConnector );
		if ( tcmItr == getTransConnectorMap().end() ) {
			std::cerr << "WARNING:  transconnector does not map to copy" << std::endl;
			continue;
		}
		ESMoL::TransConnector outputSrcTransConnector = tcmItr->second;

		ESMoL::TransConnector inputDstTransConnector = inputTransition.dstTransition_end();
		tcmItr = getTransConnectorMap().find( inputDstTransConnector );
		if ( tcmItr == getTransConnectorMap().end() ) {
			std::cerr << "WARNING:  transconnector does not map to copy" << std::endl;
			continue;
		}
		ESMoL::TransConnector outputDstTransConnector = tcmItr->second;

		ESMoL::Transition outputTransition = ESMoL::Transition::Create( outputState );
		UdmEngine::copyTransition( inputTransition, outputTransition );

		outputTransition.srcTransition_end() = outputSrcTransConnector;
		outputTransition.dstTransition_end() = outputDstTransConnector;
	}
}