X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=bayeswf.cpp;h=eee6882e4e18f59dcc9c2b8f37f950e22b3d6e13;hb=2d2a3a01f1d6828f37de15bd52a7bae6d64da117;hp=7ae502d2492a801c8847e0b8eb07234d9f5e9bdd;hpb=e7674d31d1c284487aa4021f66f068c0e9035ccc;p=wloh diff --git a/bayeswf.cpp b/bayeswf.cpp index 7ae502d..eee6882 100644 --- a/bayeswf.cpp +++ b/bayeswf.cpp @@ -33,11 +33,11 @@ float prior_sigma; // Data waiting for insertion into the database. struct RatingDBTuple { - string player; + int player; float mu, mu_stddev; }; struct CovarianceDBTuple { - string player1, player2; + int player1, player2; float covariance; }; vector rating_db_tuples; @@ -65,7 +65,7 @@ struct match { map > matches_for_player; vector all_matches; -void dump_scores(const vector &players, const float *mu, const float *mu_stddev, int num_players) +void dump_scores(const vector &players, const float *mu, const float *mu_stddev, int num_players) { #if USE_DB for (int i = 0; i < num_players; ++i) { @@ -74,7 +74,7 @@ void dump_scores(const vector &players, const float *mu, const float *mu } #else for (int i = 0; i < num_players; ++i) { - printf("%f %f %s\n", mu[i], mu_stddev[i], players[i].c_str()); + printf("%f %f %d\n", mu[i], mu_stddev[i], players[i]); } #endif } @@ -243,7 +243,7 @@ void construct_hessian(const float *mu, int num_players) // Compute uncertainty (stddev) of mu estimates, which is sqrt((H^-1)_ii), // where H is the Hessian (see construct_hessian()). -void compute_mu_uncertainty(const float *mu, const vector &players) +void compute_mu_uncertainty(const float *mu, const vector &players) { // FIXME: Use pseudoinverse if applicable. Matrix ih = hessian.inverse(); @@ -264,9 +264,9 @@ void compute_mu_uncertainty(const float *mu, const vector &players) #else for (unsigned i = 0; i < players.size(); ++i) { for (unsigned j = 0; j < players.size(); ++j) { - printf("covariance %s %s %f\n", - players[i].c_str(), - players[j].c_str(), + printf("covariance %d %d %f\n", + players[i], + players[j], ih(i, j)); } } @@ -275,8 +275,6 @@ void compute_mu_uncertainty(const float *mu, const vector &players) void process_file(const char *filename) { - printf("%s...\n", filename); - global_sigma = 70.0f; prior_sigma = 70.0f; matches_for_player.clear(); @@ -305,8 +303,8 @@ void process_file(const char *filename) exit(1); } - vector players; - map player_map; + vector players; + map player_map; for (int i = 0; i < num_players; ++i) { char buf[256]; @@ -315,17 +313,17 @@ void process_file(const char *filename) exit(1); } - players.push_back(buf); - player_map[buf] = i; + players.push_back(atoi(buf)); + player_map[atoi(buf)] = i; } int num_matches = 0; for ( ;; ) { - char pl1[256], pl2[256]; + int pl1, pl2; int score1, score2; float weight; - if (fscanf(fp, "%s %s %d %d %f", pl1, pl2, &score1, &score2, &weight) != 5) { + if (fscanf(fp, "%d %d %d %d %f", &pl1, &pl2, &score1, &score2, &weight) != 5) { //fprintf(stderr, "Read %d matches.\n", num_matches); break; } @@ -333,11 +331,11 @@ void process_file(const char *filename) ++num_matches; if (player_map.count(pl1) == 0) { - fprintf(stderr, "Unknown player '%s'\n", pl1); + fprintf(stderr, "Unknown player '%d'\n", pl1); exit(1); } if (player_map.count(pl2) == 0) { - fprintf(stderr, "Unknown player '%s'\n", pl2); + fprintf(stderr, "Unknown player '%d'\n", pl2); exit(1); } @@ -408,7 +406,7 @@ void process_file(const char *filename) int main(int argc, char **argv) { #if USE_DB - pqxx::connection conn("dbname=wloh_dev host=127.0.0.1 user=wloh password=oto4iCh5"); + pqxx::connection conn("dbname=wloh host=127.0.0.1 user=wloh password=oto4iCh5"); #endif for (int i = 1; i < argc; ++i) { @@ -424,17 +422,35 @@ int main(int argc, char **argv) pqxx::work txn(conn); txn.exec("SET client_min_messages TO WARNING"); + // Dump aux_params. + { + txn.exec("TRUNCATE aux_params"); + pqxx::tablewriter writer(txn, "aux_params"); + for (map, float>::const_iterator it = aux_params.begin(); it != aux_params.end(); ++it) { + char str[128]; + snprintf(str, 128, "%f", it->second); + + vector tuple; + tuple.push_back(it->first.first); // locale + tuple.push_back(it->first.second); // parameter name + tuple.push_back(str); + writer.push_back(tuple); + } + writer.complete(); + } + // Dump ratings. { txn.exec("TRUNCATE ratings"); pqxx::tablewriter writer(txn, "ratings"); for (unsigned i = 0; i < rating_db_tuples.size(); ++i) { - char mu_str[128], mu_stddev_str[128]; + char player_str[128], mu_str[128], mu_stddev_str[128]; + snprintf(player_str, 128, "%d", rating_db_tuples[i].player); snprintf(mu_str, 128, "%f", rating_db_tuples[i].mu); snprintf(mu_stddev_str, 128, "%f", rating_db_tuples[i].mu_stddev); vector tuple; - tuple.push_back(rating_db_tuples[i].player); + tuple.push_back(player_str); tuple.push_back(mu_str); tuple.push_back(mu_stddev_str); writer.push_back(tuple); @@ -447,12 +463,14 @@ int main(int argc, char **argv) txn.exec("CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )"); pqxx::tablewriter writer(txn, "new_covariance"); for (unsigned i = 0; i < covariance_db_tuples.size(); ++i) { - char cov_str[128]; + char player1_str[128], player2_str[128], cov_str[128]; + snprintf(player1_str, 128, "%d", covariance_db_tuples[i].player1); + snprintf(player2_str, 128, "%d", covariance_db_tuples[i].player2); snprintf(cov_str, 128, "%f", covariance_db_tuples[i].covariance); vector tuple; - tuple.push_back(covariance_db_tuples[i].player1); - tuple.push_back(covariance_db_tuples[i].player2); + tuple.push_back(player1_str); + tuple.push_back(player2_str); tuple.push_back(cov_str); writer.push_back(tuple); } @@ -463,27 +481,12 @@ int main(int argc, char **argv) txn.exec("ALTER TABLE new_covariance ADD PRIMARY KEY ( player1, player2 );"); txn.exec("DROP TABLE IF EXISTS covariance"); txn.exec("ALTER TABLE new_covariance RENAME TO covariance"); - - // Dump aux_params. - { - txn.exec("TRUNCATE aux_params"); - pqxx::tablewriter writer(txn, "aux_params"); - for (map, float>::const_iterator it = aux_params.begin(); it != aux_params.end(); ++it) { - char str[128]; - snprintf(str, 128, "%f", it->second); - - vector tuple; - tuple.push_back(it->first.first); // locale - tuple.push_back(it->first.second); // parameter name - tuple.push_back(str); - writer.push_back(tuple); - } - writer.complete(); - } #else //fprintf(stderr, "Optimal sigma: %f (two-player: %f)\n", sigma[0], sigma[0] * sqrt(2.0f)); for (map, float>::const_iterator it = aux_params.begin(); it != aux_params.end(); ++it) { - printf("%s: aux_param %s %f\n", it->first->first, it->first->second, it->second); + printf("%s: aux_param %s %f\n", it->first.first.c_str(), it->first.second.c_str(), it->second); } #endif + + txn.commit(); }