Ejemplo n.º 1
0
main()
{
  cout << utility(-1, 3, 0, 4) << endl;
  cout << utility(-5, 6, 8, 4) << endl;
  cout << utility(3, 7, 2, 4) << endl;

}
Ejemplo n.º 2
0
//--------------------------------------------------------------
void testApp::update(){
int loop = 0;
    while (loop < 1000){
        int randomRow = rand() % neighborhood.size();
        int randomCol = rand() % neighborhood[randomRow].size();
        //cout << "Row: " << randomRow << endl;
        //cout << "Col: " << randomCol << endl;
        int bestSwitchRow = 0;
        int bestSwitchCol = 0;
        for(int i=-1; i<=1; i++){
            for(int j=-1; j<=1; j++){
                //cout << "i: " << i << " j: " << j << endl;
                if(utility(randomRow, randomCol, bestSwitchRow, bestSwitchCol) < utility(randomRow, randomCol, i, j)){
                    //cout << "In if statement" << endl;
                    bestSwitchRow = i;
                    bestSwitchCol = j;
                }
                //cout << "Through if statement" << endl;
            }
        }
        if(bestSwitchRow == 0 && bestSwitchCol == 0){
            int randomRowDirection = (rand() % 3)-1;
            int randomColDirection = (rand() % 3)-1;
            switchType(randomRow, randomCol, randomRowDirection, randomColDirection);
        }else{
            switchType(randomRow, randomCol, bestSwitchRow, bestSwitchCol);
        }
        //cout << "Switched!" << endl;
        loop++;
    }
}
int TicTacToeState::max_value(unsigned int depth)
{
    if (done())
        return utility(depth);

    depth++; // Increase depth by one
    int v = -10; // Smaller than possible utility return
    for (unsigned int i = 0; i < actions.size(); i++)
    {
        player = p1_symbol; // p1 gets the max value

        TicTacToeState next_state = result(actions[i]); 
        int temp = next_state.min_value(depth);
        // For debuging purpose
        //if (depth == 2)
        //    print_debug_info(next_state, depth, temp);
        // Get the max from mins
        if (v < temp)
        {
            v = temp;
            index = i;
        }
    }
    return v;
}
Ejemplo n.º 4
0
void Solution::rotate(vector<vector<int> > &A) {
    int n=A.size();
	for(int i=0;i<n/2;i++)
		utility(A,i,i,n-2*i); //call iteratively for all outer boundaries
    // Do not write main() function.
    // Do not read input, instead use the arguments to the function.
    // Do not print the output, instead return values as specified
    // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
}
Ejemplo n.º 5
0
double efg_solve::Game::MaxRegret(const std::array<std::vector<double>, 2> &strategy_profile, double &regret1, double &regret2) const {
  int max_seq = std::max(num_sequences(Player::P1), num_sequences(Player::P2));
  std::vector<double> utility((unsigned long) max_seq, 0);
  double game_value = GameValue(strategy_profile, &utility);

  UtilityVector(strategy_profile[player_id(Player::P2)], &utility, Player::P1);
  regret1 = BestResponseValue(Player::P1, &utility) - game_value;

  UtilityVector(strategy_profile[player_id(Player::P1)], &utility, Player::P2);
  regret2 = BestResponseValue(Player::P2, &utility) + game_value;

  return std::max(regret1, regret2);
}
// QUtility
void QUtility::learn (QState last_state, QAction action,
                      QState current_state, QAction credit)
{
    QCredit new_util, max_util;
    QAction a;

/*  assert(action >= 0 && action < total_actions);

    for (a = 0; a < total_actions; a++) {
        QCredit util = utility(current_state, a);
        if (max_util < util || a == 0)
            max_util = util;
    }*/
    a = best_action(current_state);
    max_util = utility(current_state, a);
    new_util = credit + discount * max_util;
    adjust(last_state, action, new_util);
}
Ejemplo n.º 7
0
CustomerOrder::CustomerOrder(const std::string& record) {

    name    = "";
    product = "";
    order   = nullptr;
    nOrders = 0;


    bool more = true;
    size_t next_pos = 0;
    Utilities utility(field_width);

    //read tokens
    name = utility.nextToken(record, next_pos, more);
    if(more)
        product =  utility.nextToken(record, next_pos, more);

    //vector to store products that customer wants to order
    std::vector<std::string> orders_vec;
    std::string temp_record;
    while(more) {
        temp_record = "";
        temp_record = utility.nextToken(record, next_pos, more);
        if(!temp_record.empty()) {
            orders_vec.push_back(temp_record);
            nOrders++;
        }
    }

    order = new ItemOrder[nOrders];

    for(size_t i = 0; i < nOrders; i++)
        order[i] = ItemOrder(orders_vec[i]);


    //if (utility.getFieldWidth() < field_width)
        field_width = utility.getFieldWidth();

}
Ejemplo n.º 8
0
/* Gateway Function */
void mexFunction( int nlhs, mxArray *plhs[],    /* Input Vars */
		  int nrhs, const mxArray*prhs[] )      /* Output Vars */
{ 
    double *score;      /* Output Vars */
/*     double *b, *cTok;	/* Input Vars */
    size_t m,n; 

    /* Check for proper number of arguments */
    if (nrhs != 2) { 
	    mexErrMsgIdAndTxt( "MATLAB:getAllValid:invalidNumInputs",
                "Two input arguments required."); 
    } else if (nlhs > 2) {
	    mexErrMsgIdAndTxt( "MATLAB:getAllValid:maxlhs",
                "Too many output arguments."); 
    } 

    /* Check the dimensions of board_in. */ 
    m = mxGetM(B_IN);	/* Should be L */
    n = mxGetN(B_IN);	/* Should be L */
    if (!mxIsDouble(B_IN) || mxIsComplex(B_IN) || 
	(m != L) || (n != L)) { 
	    mexErrMsgIdAndTxt( "MATLAB:getAllValid:invalid board",
                "getAllValid requires that B be a L x L matrix."); 
    } 
    
    /* Do the actual computations in a subroutine and assign */
    SCORE_OUT = mxCreateDoubleScalar(utility(prhs));

    /* Assign pointers to the output parameters */ 
    score = mxGetPr(SCORE_OUT);
    
    /* Cleanup */
/* mxDestroyArray(temp); */
    
    return;
    
}
Ejemplo n.º 9
0
double efg_solve::Game::GameValue(const std::array<std::vector<double>, 2> &strategy_profile) const {
  int max_seq = std::max(num_sequences(Player::P1), num_sequences(Player::P2));
  std::vector<double> utility((unsigned long) max_seq, 0);
  return GameValue(strategy_profile, &utility);
}
Ejemplo n.º 10
0
  void Generator::start_generation() const
  {
    String utility(Utility::make());

    String filename(this->config-
  }
Ejemplo n.º 11
0
Pitcher::Pitcher(std::string s,int w,int h,char t,float i,int e) : BaseballPlayer(s,w,h) {
	throws=t;
	inningsPitched=i;
	earnedRuns=e;
	ERA= utility();
}
Ejemplo n.º 12
0
	std::cout<<"Weight:  "<<weight<<" pounds"<<std::endl;
	std::cout<<"Throws:  "<<throws<<std::endl;
	std::cout<<"EarnedRuns:  "<<earnedRuns<<std::endl;
	std::cout<<"Innings Pitched:  "<<std::fixed<<std::setprecision(3)<<inningsPitched<<std::endl;
	std::cout<<"ERA:  "<<std::fixed<<std::setprecision(2)<<ERA<<std::endl<<std::endl;
	
	/** writing to outFile**/
	outFile<<name<<" "<<name<<" "<<height<<" "<<weight<<" "<<throws<<" "<<earnedRuns<<" "<<inningsPitched<<"\n";
}



void Pitcher::load_player(std::ifstream& input){
	
	input>>name>>height>>weight>>throws>>earnedRuns>>inningsPitched;
	ERA=utility();
	
	/******** another method*************
	=====================================
	
	std::string temp;
	std::getline (input,temp);
		
	int posNil=0;
	int nextNil=0;
	for (int i=0;i<temp.size();i++){
		char check=temp.at(i);
		if (isspace(check)){
			posNil=i;
			break;
		}
Ejemplo n.º 13
0
/*
	Same idea as make_minimax_alphabeta_cpu_move but without the alpha beta checking
*/
int make_minimax_cpu_move(Alex_Ayerdi * b, int playerval, vector<int> &cell) {
	
	if (b->full_board()) return utility(b);

	int minplay = INFINITY;
	int maxplay = -INFINITY;

	int resultMin = INFINITY;
	int resultMax = -INFINITY;

	bool madeMove = false;

	if (b->get_tries() == 5) return utility(b); 

	if (playerval == MAX)
	{
		//go among all the children
		for (int i = 1; i < 9; i++)
		{
			for (int j = 1; j < 9; j++)
			{
				//choose the empty squares
				if (b->get_square(i, j) == 0)
				{
					//play a test square
					Alex_Ayerdi tempAlex_Ayerdi;
					tempAlex_Ayerdi.copyBoard(b);

					if (b->play_square(i, j, playerval) == false)
					{
						continue; //if we can't play any moves, return the utility
					}
					else
					{
						madeMove = true;
					}

					b->increase_tries();

					//get the utilty from making that test move
					resultMax = make_minimax_cpu_move(b, MIN, cell);

					if (resultMax >= maxplay) // save the maximum outcome
					{
						maxplay = resultMax;

						
						if (b->get_tries() == 1)
						{
							cell.clear();
							cell.push_back(i);
							cell.push_back(j);
						}

					}

					//reset the move so we can try another
					
					if ((i == 1 && j == 1) ||
						(i == 8 && j == 8) ||
						(i == 1 && j == 8) ||
						(i == 8 && j == 1)) 
					{
						b->copyBoard(&tempAlex_Ayerdi);
						b->decrease_tries();

						return MAX;
					}

					b->copyBoard(&tempAlex_Ayerdi);
					b->decrease_tries();
				}
			}
		}

		if (madeMove == false) 
			return utility(b);
		
		return maxplay;  //return the best move we found
	}
	else
	{
		//same deal as the max player, but with min idea
		for (int i = 1; i < 9; i++)
		{
			for (int j = 1; j < 9; j++)
			{
				if (b->get_square(i, j) == 0)
				{
					Alex_Ayerdi tempAlex_Ayerdi;
					tempAlex_Ayerdi.copyBoard(b);

					if (b->play_square(i, j, playerval) == false)
					{
						continue;
					}
					else
					{
						madeMove = true;
					}
					b->increase_tries();

					resultMin = make_minimax_cpu_move(b, MAX, cell);

					if (resultMin <= minplay)  // save the minimum outcome
					{
						minplay = resultMin;

						//not all moves are valid, only save the best one from the first ply
						if (b->get_tries() == 1)
						{
							cell.clear();
							cell.push_back(i);
							cell.push_back(j);
						}

					}

					
					if ((i == 1 && j == 1) ||
						(i == 8 && j == 8) ||
						(i == 1 && j == 8) ||
						(i == 8 && j == 1)) 
					{
						b->copyBoard(&tempAlex_Ayerdi);
						b->decrease_tries();

						return MIN;
					}
					

					b->copyBoard(&tempAlex_Ayerdi);
					b->decrease_tries();
				}
			}
		}

		if (madeMove == false)
			return utility(b);

		return minplay;
	}
}
Ejemplo n.º 14
0
// this function handles players moving around the board
void move(struct player * players, struct location * board, const int n, int * pvalue, char * plocation)
{
    struct player * p = &players[n];
    int ret;
#ifdef DEBUG
    fprintf(output[globalrank], "Player %d moved from %d\n", n, p->location);
#endif
    // check to see if on the "go to jail" cell
    if (p->location == 30)
    {
        // go to jail
        p->location = 10;
        board[p->location].visited++;
        p->money -= 50;
        return;
    }
    else
    {
        // advance the player
        p->location += roll() + roll();
    }
    // check to see if player passed go
    if (p->location > 39)
    {
#ifdef DEBUG
        fprintf(output[globalrank], "Player %d passed Go\n", n);
#endif
        p->location %= 40;
        p->money += 200;
    }
#ifdef DEBUG
    fprintf(output[globalrank], " to %d\n", p->location);
#endif
    board[p->location].visited++;
    switch (p->location)
    {
        case 1:
        case 3:
        case 6:
        case 8:
        case 9:
        case 11:
        case 13:
        case 14:
        case 16:
        case 18:
        case 19:
        case 21:
        case 23:
        case 24:
        case 26:
        case 27:
        case 29:
        case 31:
        case 32:
        case 34:
        case 37:
        case 39:
            // properties
            property(players, board, n, pvalue, plocation);
            break;
        case 2:
        case 17:
        case 33:
            comm_chest(players, n);
            break;
        case 7:
        case 22:
        case 36:
            // 1 is property
            // 2 is utility pay 10x
            // 3 is railroad pay 2x
            // 4 is handle movement
            // 5 is railroad pay norm
            // chance
            ret = chance(players, n);
            switch (ret)
            {
                case 1:
                    property(players, board, n, pvalue, plocation);
                    break;
                case 2:
                    utility(players, board, 10, n, pvalue, plocation);
                    break;
                case 3:
                    railroad(players, board, 2, n, pvalue, plocation);
                    break;
                case 4:
                    move(players, board, n, pvalue, plocation);
                    break;
                case 5:
                    railroad(players, board, 1, n, pvalue, plocation);
                    break;
            }
            break;
        case 5:
        case 15:
        case 25:
        case 35:
            railroad(players, board, 1, n, pvalue, plocation);
            // railroad
            break;
        case 12:
        case 28:
            utility(players, board, 1, n, pvalue, plocation);
            // utility
            break;
        case 30:
            p->location = 10;
            p->money -= 50;
            // go to jail
            break;
        case 10:
            // jail
            break;
        case 4:
            p->money -= 200;
            // income tax
            break;
        case 38:
            p->money -= 75;
            // luxury tax
            break;
        case 0:
        case 20:
            // go and free parking, do nothing
            return;
        default:
            fprintf(stderr, "ERROR: where are you??? player %d at %d\n", p->order, p->location);
            break;
    }
}