void sendNow(ActionItem *item) { mutex.wait(); actions.clear(); _send(item); mutex.post(); }
/// Sort actions by their expected likelihood /// WRITEME more documentation /// \todo Make bucket size a parameter /// \todo Don't hardcode children span frequencies void Parser::sort_actions(Actions& actions) const { /// Put all TOP inferences last (first?) Actions top_actions; vector<Actions> sort; /// \todo Make bucket size a parameter unsigned bucketsize = 1000; sort.resize(bucketsize); for (Actions::const_iterator a = actions.begin(); a != actions.end(); a++) { if (a->label() == Label_TOP()) { top_actions.push_back(*a); continue; } double sizefreq = 0; /// \todo Don't hardcode children span frequencies /// We exclude TOP from these frequencies, since it skews the number of unaries switch(a->children().size()) { case 1: sizefreq = 0.231926; break; case 2: sizefreq = 0.565159; break; case 3: sizefreq = 0.147423; break; case 4: sizefreq = 0.0422515; break; case 5: sizefreq = 0.00985868; break; case 6: sizefreq = 0.00269639; break; case 7: sizefreq = 0.000553723; break; case 8: sizefreq = 3.61124e-05; break; case 9: sizefreq = 6.01873e-05; break; case 10: sizefreq = 1.20375e-05; break; case 11: sizefreq = 2.40749e-05; break; default: sizefreq = 0; break; } sizefreq /= 0.565159; double labelfreq = label_frequency(a->label()) / max_label_frequency(); double freq = sizefreq * labelfreq; unsigned bucket = (unsigned)((bucketsize-1) * freq); assert(bucket >= 0 && bucket < bucketsize); sort.at(bucket).push_back(*a); } unsigned actionsize = actions.size(); actions.clear(); //for(vector<Actions>::const_reverse_iterator as = sort.rbegin(); for(vector<Actions>::reverse_iterator as = sort.rbegin(); as != sort.rend(); as++) { actions.insert(actions.end(), as->begin(), as->end()); } // Insert TOP inferences at the end of the list actions.insert(actions.end(), top_actions.begin(), top_actions.end()); assert(actions.size() == actionsize); }
void EditorContext::DoActions(Actions& actions) { for (int i=0; i<actions.size(); ++i) { actions[i](); } actions.clear(); }
int main(int argc, char ** argv) { if (argc != 3) { printf("Usage: %s HOSTNAME PORT\n", argv[0]); return 1; } string address = argv[1]; string port_str = argv[2]; int port = std::stoi(port_str); string name = "nearest"; try { Session session; printf("Connecting to %s:%d...\n", address.c_str(), port); session.connect(address, port); printf("Logging in as a player named '%s'...\n", name.c_str()); session.login_player(name); printf("Waiting for WELCOME...\n"); session.wait_for_welcome(); printf("Waiting for GAME_STARTS...\n"); session.wait_for_game_starts(); int mon_id = session.player_id(); std::vector<NeutralCell> cellules_neutres; TurnPlayerCell temp_cell; std::map<double, NeutralCell*> distances; // trie les cellules neutres par leur distance à ma cellule double temp_distance; NeutralCell temp_destination; Turn tour; Position destination; Actions actions; while(session.is_logged()) { actions.clear(); printf("Waiting for next turn...\n"); session.wait_for_next_turn(); /// recevoir le tour cellules_neutres.clear(); distances.clear(); cellules_neutres = session.neutral_cells(); /// récupérer la liste de mes cellules for (unsigned int i=0; i<tour.pcells.size(); ++i) { if (tour.pcells[i].player_id == mon_id) { temp_cell = tour.pcells[i]; /// trier les cellules neutres par distance croissante for (unsigned int j=0; j<cellules_neutres.size(); ++j) { if (cellules_neutres[j].remaining_turns_before_apparition == 0) { // on ne prend en compte que les cellules vivantes temp_distance = distance(temp_cell, cellules_neutres[j]); distances[temp_distance] = &cellules_neutres[j]; } } /// récupérer les coord de la cellule neutre la plus proche temp_destination = *(distances.begin())->second; destination.x = temp_destination.position.x; destination.y = temp_destination.position.y; /// envoyer l'action printf("Adding move_action (pcell_id=%d, dest=(%g,%g))\n", temp_cell.pcell_id, destination.x, destination.y); actions.add_move_action(temp_cell.pcell_id, destination.x, destination.y); } } printf("Sending actions...\n"); session.send_actions(actions); } } catch (ainet16::AINetException & exception) { cout << exception.what() << endl; return 1; } return 0; }