void Peer::leech() { double next_choked_update = simgrid::s4u::Engine::get_clock() + UPDATE_CHOKED_INTERVAL; XBT_DEBUG("Start downloading."); /* Send a "handshake" message to all the peers it got (since it couldn't have gotten more than 50 peers) */ sendHandshakeToAllPeers(); XBT_DEBUG("Starting main leech loop listening on mailbox: %s", mailbox_->get_cname()); void* data = nullptr; while (simgrid::s4u::Engine::get_clock() < deadline && countPieces(bitfield_) < FILE_PIECES) { if (comm_received == nullptr) { comm_received = mailbox_->get_async(&data); } if (comm_received->test()) { message = static_cast<Message*>(data); handleMessage(); delete message; comm_received = nullptr; } else { // We don't execute the choke algorithm if we don't already have a piece if (simgrid::s4u::Engine::get_clock() >= next_choked_update && countPieces(bitfield_) > 0) { updateChokedPeers(); next_choked_update += UPDATE_CHOKED_INTERVAL; } else { simgrid::s4u::this_actor::sleep_for(SLEEP_DURATION); } } } if (hasFinished()) XBT_DEBUG("%d becomes a seeder", id); }
vector<double> GTPWrapper::getLongrange(bool first) { vector<double> ret; int *tpos = new int[2]; int d = (int)floor(eyesize/2)+1; //sone above.. tpos = relativeFrontPos(first,tpos,d); int * t = countPieces(sqlook(tpos,first)); ret.push_back(t[0]); ret.push_back(t[1]); delete[] t; //sone left.. tpos = relativeLeftPos(first,tpos,d); t = countPieces(sqlook(tpos,first)); ret.push_back(t[0]); ret.push_back(t[1]); delete[] t; //sone right.. tpos = relativeRightPos(first,tpos,d); t = countPieces(sqlook(tpos,first)); ret.push_back(t[0]); ret.push_back(t[1]); delete[] t; //sone behind.. tpos = relativeBackPos(first,tpos,d); t = countPieces(sqlook(tpos,first)); ret.push_back(t[0]); ret.push_back(t[1]); delete[] t; delete[] tpos; return ret; }
/*Determine whether Board contains valid number of Pieces of each type*/ int boardValid(char board[BOARD_SIZE][BOARD_SIZE]){ int whites[NUMBER_OF_PIECES] = { 0 }; int blacks[NUMBER_OF_PIECES] = { 0 }; countPieces(board, WHITE, whites); countPieces(board, BLACK, blacks); if (whites[KING_ID] != 1 || blacks[KING_ID] != 1) return 0; if (whites[QUEEN_ID] > 1 || blacks[QUEEN_ID] > 1) return 0; if (whites[BISHOP_ID] > 2 || blacks[BISHOP_ID] > 2) return 0; if (whites[ROOK_ID] > 2 || blacks[ROOK_ID] > 2) return 0; if (whites[KNIGHT_ID] > 2 || blacks[KNIGHT_ID] > 2) return 0; if (whites[PAWN_ID] > 8 || blacks[PAWN_ID] > 8) return 0; return 1; }
void PrintPosition (POSITION position, STRING playersName, BOOLEAN usersTurn){ char* board; int turn, x, y, reds, blues; board = unhash(position, &turn); countPieces(board, &reds, &blues); printf("\t%s's Turn (%s):\n ",playersName,(turn==PLAYER_ONE ? "BLACK" : "WHITE")); printf("%s\n", GetPrediction(position, playersName, usersTurn)); for (y = length; y >= 0; y--) { // for all the rows if (y == 0) { printf(" "); for (x = 0; x < width; x = x + 1) { printf("%c ", 'a' + x); } } else { printf(" %2d ", y); //print row number for (x = 1; x <= width; x++) { if (x+1 <= width) { printf("%c - ",board[toIndex(x,y)]); } else { printf("%c",board[toIndex(x,y)]); } } printf("\n"); if (y > 1) { printf(" "); for (x = 0; x < width; x++) { printf("| "); } printf("\n"); } } } printf("\n\n"); if(board != NULL) SafeFree(board); }
TIER BoardToTier(char* board) { int blacks, whites; countPieces(board, &blacks, &whites); int totalPieces = blacks+whites; return boardsize-totalPieces; }
/** @brief Return the piece to be downloaded * There are two cases (as described in "Bittorrent Architecture Protocol", Ryan Toole : * If a piece is partially downloaded, this piece will be selected prioritarily * If the peer has strictly less than 4 pieces, he chooses a piece at random. * If the peer has more than pieces, he downloads the pieces that are the less replicated (rarest policy). * If all pieces have been downloaded or requested, we select a random requested piece (endgame mode). * @param remote_peer: information about the connection * @return the piece to download if possible. -1 otherwise */ int Peer::selectPieceToDownload(Connection* remote_peer) { int piece = partiallyDownloadedPiece(remote_peer); // strict priority policy if (piece != -1) return piece; // end game mode if (countPieces(current_pieces) >= (FILE_PIECES - countPieces(bitfield_)) && isInterestedBy(remote_peer)) { #if ENABLE_END_GAME_MODE == 0 return -1; #endif int nb_interesting_pieces = 0; // compute the number of interesting pieces for (unsigned int i = 0; i < FILE_PIECES; i++) if (hasNotPiece(i) && remote_peer->hasPiece(i)) nb_interesting_pieces++; xbt_assert(nb_interesting_pieces != 0); // get a random interesting piece int random_piece_index = RngStream_RandInt(stream, 0, nb_interesting_pieces - 1); int current_index = 0; for (unsigned int i = 0; i < FILE_PIECES; i++) { if (hasNotPiece(i) && remote_peer->hasPiece(i)) { if (random_piece_index == current_index) { piece = i; break; } current_index++; } } xbt_assert(piece != -1); return piece; } // Random first policy if (countPieces(bitfield_) < 4 && isInterestedByFree(remote_peer)) { int nb_interesting_pieces = 0; // compute the number of interesting pieces for (unsigned int i = 0; i < FILE_PIECES; i++) if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) nb_interesting_pieces++; xbt_assert(nb_interesting_pieces != 0); // get a random interesting piece int random_piece_index = RngStream_RandInt(stream, 0, nb_interesting_pieces - 1); int current_index = 0; for (unsigned int i = 0; i < FILE_PIECES; i++) { if (hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) { if (random_piece_index == current_index) { piece = i; break; } current_index++; } } xbt_assert(piece != -1); return piece; } else { // Rarest first policy short min = SHRT_MAX; int nb_min_pieces = 0; int current_index = 0; // compute the smallest number of copies of available pieces for (unsigned int i = 0; i < FILE_PIECES; i++) { if (pieces_count[i] < min && hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) min = pieces_count[i]; } xbt_assert(min != SHRT_MAX || not isInterestedByFree(remote_peer)); // compute the number of rarest pieces for (unsigned int i = 0; i < FILE_PIECES; i++) if (pieces_count[i] == min && hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) nb_min_pieces++; xbt_assert(nb_min_pieces != 0 || not isInterestedByFree(remote_peer)); // get a random rarest piece int random_rarest_index = RngStream_RandInt(stream, 0, nb_min_pieces - 1); for (unsigned int i = 0; i < FILE_PIECES; i++) if (pieces_count[i] == min && hasNotPiece(i) && remote_peer->hasPiece(i) && isNotDownloadingPiece(i)) { if (random_rarest_index == current_index) { piece = i; break; } current_index++; } xbt_assert(piece != -1 || not isInterestedByFree(remote_peer)); return piece; } }
pair<double,double> CoCheckersExperiment::playGame( shared_ptr<NEAT::GeneticIndividual> ind1, shared_ptr<NEAT::GeneticIndividual> ind2 ) { //You get 1 point just for entering the game, wahooo! pair<double,double> rewards(1.0,1.0); #if DEBUG_GAME_ANNOUNCER cout << "Playing game\n"; #endif populateSubstrate(ind1,0); populateSubstrate(ind2,1); uchar b[8][8]; //cout << "Playing games with HyperNEAT as black\n"; //for (handCodedType=0;handCodedType<5;handCodedType++) for (testCases=0;testCases<2;testCases++) { if (testCases==0) { individualBlack = ind1; individualWhite = ind2; } else //testCases==1 { individualBlack = ind2; individualWhite = ind1; } resetBoard(b); int retval=-1; int rounds=0; for (rounds=0;rounds<CHECKERS_MAX_ROUNDS&&retval==-1;rounds++) { //cout << "Round: " << rounds << endl; moveToMake = CheckersMove(); if (testCases==0) { currentSubstrateIndex=0; } else //testCases==1 { currentSubstrateIndex=1; } //cout << "Black is thinking...\n"; evaluatemax(b,CheckersNEATDatatype(INT_MAX/2),0,2); #if CHECKERS_EXPERIMENT_DEBUG cout << "BLACK MAKING MOVE\n"; printBoard(b); #endif if (moveToMake.from.x==255) { //black loses cout << "BLACK LOSES!\n"; retval = WHITE; } else { makeMove(moveToMake,b); retval = getWinner(b,WHITE); } #if CHECKERS_EXPERIMENT_LOG_EVALUATIONS memcpy(gameLog[rounds*2],b,sizeof(uchar)*8*8); #endif #if COCHECKERS_EXPERIMENT_DEBUG printBoard(b); CREATE_PAUSE(""); #endif if (retval==-1) { //printBoard(b); moveToMake = CheckersMove(); { //progress_timer t; if (testCases==0) { currentSubstrateIndex=1; } else //testCases==1 { currentSubstrateIndex=0; } //cout << "White is thinking...\n"; evaluatemin(b,CheckersNEATDatatype(INT_MAX/2),0,3); //cout << "SimpleCheckers time: "; } #if COCHECKERS_EXPERIMENT_DEBUG cout << "WHITE MAKING MOVE\n"; printBoard(b); #endif if (moveToMake.from.x==255) { //white loses cout << "WHITE LOSES BECAUSE THERE'S NO MOVES LEFT!\n"; retval = BLACK; #if COCHECKERS_EXPERIMENT_DEBUG printBoard(b); CREATE_PAUSE(""); #endif } else { makeMove(moveToMake,b); retval = getWinner(b,BLACK); } #if COCHECKERS_EXPERIMENT_DEBUG printBoard(b); CREATE_PAUSE(""); #endif } #if CHECKERS_EXPERIMENT_LOG_EVALUATIONS memcpy(gameLog[rounds*2+1],b,sizeof(uchar)*8*8); #endif } if (retval==BLACK) { #if DEBUG_GAME_ANNOUNCER cout << "BLACK WON!\n"; #endif if (ind1==individualBlack) { rewards.first += 800; rewards.first += (CHECKERS_MAX_ROUNDS-rounds); } else { rewards.second += 800; rewards.second += (CHECKERS_MAX_ROUNDS-rounds); } } else if (retval==-1) //draw { #if DEBUG_GAME_ANNOUNCER cout << "WE TIED!\n"; #endif //rewards.first += 200; //rewards.second += 200; } else //White wins { #if DEBUG_GAME_ANNOUNCER cout << "WHITE WON\n"; #endif if (ind1==individualWhite) { rewards.first += 800; rewards.first += (CHECKERS_MAX_ROUNDS-rounds); } else { rewards.second += 800; rewards.second += (CHECKERS_MAX_ROUNDS-rounds); } } int whiteMen,blackMen,whiteKings,blackKings; //countPieces(gi.board,whiteMen,blackMen,whiteKings,blackKings); countPieces(b,whiteMen,blackMen,whiteKings,blackKings); if (ind1==individualWhite) { rewards.first += (2 * (whiteMen) ); rewards.first += (3 * (whiteKings) ); rewards.second += (2 * (blackMen) ); rewards.second += (3 * (blackKings) ); } else { rewards.first += (2 * (blackMen) ); rewards.first += (3 * (blackKings) ); rewards.second += (2 * (whiteMen) ); rewards.second += (3 * (whiteKings) ); } } #if DEBUG_GAME_ANNOUNCER cout << "Fitness earned: " << rewards.first << " & " << rewards.second << endl; CREATE_PAUSE(""); #endif return rewards; }