virtual bool test (const Array<double,1> state,
					 const Array<double,1> action)
	{
		double h = 0.0001;
		bool all_pass = true;
		
		// state derivative REWARD
		//
		Array<double,1> _hs(4);
		Array<double,1> res_st(4);
		
		for (int i=0; i<4; i++)
			{
				_hs = 0;
				_hs(i) = h;
				Array<double,1> s1(4);
				s1 = state + _hs;
				Array<double,1> s2(4);
				s2 = state - _hs;
				res_st(i) = (get_reward(s1)-get_reward(s2)) / (2*h);
			}
		
		Array<double,1> dState (4);
		dState = dd_R_state(state);
		if ( sum(abs(res_st-dState)) > 0.001) {
			all_pass = false;
			std::cout << "FoodSource::test() state derivative of REWARD failed!\n";
			 std::cout << "derivative: " << dState << std::endl;
			 std::cout << "stencil: " << res_st << std::endl;
		}

		// state derivative TIME
		//
		Array<double,2> res_st2(4,4);
		res_st2 = 0;
		for (int i=0; i<4; i++)
			{
				_hs = 0;
				_hs(i) = h;
				Array<double,1> s1(4);
				s1 = state + _hs;
				Array<double,1> s2(4);
				s2 = state - _hs;
				res_st2(TIME,i) = (get_time(s1)-get_time(s2)) / (2*h);
			}
		
		Array<double,2> dState2 (4,4);
		dState2 =  dd_T_state(state);
		if ( sum(abs(res_st2-dState2)) > 0.001) {
		  all_pass = false;
		  std::cout << "FoodSource::test() state derivative of TIME failed!\n";
		  std::cout << "derivative: " << dState2 << std::endl;
		  std::cout << "stencil: " << res_st2 << std::endl;
		}
		
		return all_pass;
	};
Esempio n. 2
0
struct BotThinkFunc* botRun(struct BotState* state){
	static int runNextPulse = 0;
	static const int runPulseTimeout = 25;

	if( runNextPulse < qbTickCount() ) {
		qbLog("heartbeat");
		//memset(&state->gs, 0, sizeof(state->gs));
		qbGetGameState(&state->gs);
		if(!begun)
		{
			learner.set_num_features(22);
			learner.set_eta(0.1);
			learner.init(0.1, 0.3, 0.0);
			qbLog("Before tick: %d", qbTickCount());
			getPlayerInfo(&state->gs, state->name);
			getItemIndices(state->gs);
			qbMovementOrder(zero_a, zero_v, 0);
			qbLog("After tick: %d", qbTickCount());
			begun = true;
		}
		qbLog("Before time: %d", qbTickCount());
		//parseItems(state->gs);
		getClosestEntities(state->gs);
		qbLog("getting next action");
		int action = learner.learning_step(&state->gs, closest_entities, get_reward(state->gs));
		if(state->gs.player.health < 0)
		{
			qbMovementOrder(zero_a, forward_v, 1);
			getPlayerInfo(&state->gs, state->name);
			getItemIndices(state->gs);
			state->gs.player.angle = 0.0;
			num_deaths++;
		}
		else
		{
			perform_action(&state->gs, action);
		}
		//qbMovementOrder(zero_a, forward_v, 1);
		//LogPlayerInfo(state->gs);
		qbLog("After time: %d", qbTickCount());
		//populateItemList(state->gs);
		//parseEntities(state->gs);
		//runTowardEntity(1, state->gs);
		runNextPulse = qbTickCount() + runPulseTimeout;
	}
/*
#ifdef WIN32
	Sleep(QB_MIN_FRAMETIME);
#else
	usleep(QB_MIN_FRAMETIME * 1000);
#endif*/
	return NULL;
}