Пример #1
0
int controls_evaluate(DateTime currentTime, DateTime elapsedTime, double tStep)
//
//  Input:   currentTime = current simulation date/time
//           elapsedTime = decimal days since start of simulation
//           tStep = simulation time step (days)                               //(5.0.013 - LR)
//  Output:  returns number of new actions taken
//  Purpose: evaluates all control rules at current time of the simulation.
//
{
    int    r;                          // control rule index
    int    result;                     // TRUE if rule premises satisfied
    struct TPremise* p;                // pointer to rule premise clause
    struct TAction*  a;                // pointer to rule action clause
    DateTime theDate = floor(currentTime);
    DateTime theTime = currentTime - floor(currentTime);

    // --- evaluate each rule
    if ( RuleCount == 0 ) return 0;
    clearActionList();
    for (r=0; r<RuleCount; r++)
    {
        // --- evaluate rule's premises
        result = TRUE;
        p = Rules[r].firstPremise;
        while (p)
        {
            if ( p->type == r_OR )
            {
                if ( result == FALSE )
                    result = evaluatePremise(p, theDate, theTime,
                                 elapsedTime, tStep);
            }
            else
            {
                if ( result == FALSE ) break;
                result = evaluatePremise(p, theDate, theTime, 
                             elapsedTime, tStep);
            }
            p = p->next;
        }    

        // --- if premises true, add THEN clauses to action list
        //     else add ELSE clauses to action list
        if ( result == TRUE ) a = Rules[r].thenActions;
        else                  a = Rules[r].elseActions;
        while (a)
        {
            updateActionValue(a, currentTime, tStep);                          //(5.0.012 - LR)
            updateActionList(a);
            a = a->next;
        }
    }

    // --- execute actions on action list
    if ( ActionList ) return executeActionList(currentTime);
    else return 0;
}
Пример #2
0
Action  RolloutAgent::getAction(Tetris *board)
{
	vector<Action > bestActions;
	float bestValue = -99999999;

	//For all valid rotations
	for (int r = 0; r < NUM_ROTATIONS; r++) {
		Rotation rot = (Rotation) r;

		//Make sure that we only check valid columns
		int maxColumn = board->highestValidColWithRot(rot) + 1;

		//For all valid columns for each rotation
		for (int col = 0; col < maxColumn; col++) {
			//Create the action for this move (will be cleaned up by playing it)
			
			float actVal = 0;
			
			//TODO: Look at all possible next pieces instead of random sampling

			for (int w = 0; w < W; w++) {
				Action a = Action(rot, col);

				//Copy the board -- this makes it have random next pieces
				Tetris *trajectorySim = board->gameCopy();

				// trajectorySim->playAction(a, false);
				actVal += heurAgent->valueOfActionOnBoard(a, trajectorySim);

				for (int k = 0; k < K; k++) {
					// trajectorySim->setPiece(1);
					// trajectorySim->printBoard();
					Action heuristicAct = heurAgent->getAction(trajectorySim);
					// trajectorySim->playAction(heuristicAct, false);
					actVal += pow(GAMMA, k + 1) * heurAgent->valueOfActionOnBoard(heuristicAct, trajectorySim);
				}

				// actVal += heurAgent->valueBetweenBoards(board, trajectorySim);
				// trajectorySim->printBoard();

				delete trajectorySim;
			}
			
			//Average over the W K-length trajectories
			actVal /= (float)W;
			// actionSim->printBoard();
			// cout << "PLAY WITH ROW: " << r << " AND COL: " << col << endl;
			// cout << "SCORE: " << actVal << endl << endl;
			// exit(1);
			if (actVal > bestValue) {
				bestValue = actVal;
				foundNewBestAction(bestActions, rot, col);
			} else if (actVal == bestValue) {
				foundTiedAction(bestActions, rot, col);
			}
		}
	}

	//Choose the best action
	Action a = pickRandomAction(bestActions);

	//Clean up the actions
	clearActionList(bestActions);

	//Play the action
	return a;
}
Пример #3
0
void UserControlsEngine::endGame()
{
    hasEnd = true;
    clearActionList();
    killTimer(idTimer);
}