bool GetVoteDecision(maploader::s_phasor_mapcycle_entry& out) { if (!mapvote_in_progress) return false; std::vector<int> votes(current_vote_options.size(), 0); for (auto itr = player_votes.begin(); itr != player_votes.end(); ++itr) if (itr->vote != -1) votes[itr->vote]++; // Make a list of all options that were voted for the most std::vector<int> max_voted; for (size_t x = 0; x < votes.size(); x++) { if (!max_voted.size()) max_voted.push_back(x); else if (votes[max_voted[0]] == votes[x]) max_voted.push_back(x); else if (votes[max_voted[0]] < votes[x]) { max_voted.clear(); max_voted.push_back(x); } } int vote_index = max_voted[rand() % max_voted.size()]; out = current_vote_options[vote_index].game_info; mapvote_in_progress = false; return true; }
TEST (votes, contested) { rai::genesis genesis; auto block1 (std::make_shared<rai::state_block> (rai::test_genesis_key.pub, genesis.hash (), rai::test_genesis_key.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0)); auto block2 (std::make_shared<rai::state_block> (rai::test_genesis_key.pub, genesis.hash (), rai::test_genesis_key.pub, rai::genesis_amount - 2 * rai::Gxrb_ratio, rai::test_genesis_key.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0)); ASSERT_FALSE (*block1 == *block2); rai::votes votes (block1); ASSERT_TRUE (votes.uncontested ()); votes.rep_votes[rai::test_genesis_key.pub] = block2; ASSERT_FALSE (votes.uncontested ()); }
void SpatiallySparseCNN::processDatasetRepeatTest(SpatialDataset &dataset, int batchSize, int nReps, string predictionsFilename,string header) { vector<vector<int> > votes(dataset.pictures.size()); vector<vector<float> > probs(dataset.pictures.size()); for (int i=0;i<dataset.pictures.size();++i) { votes[i].resize(dataset.nClasses); probs[i].resize(dataset.nClasses); } ofstream f,g; for (int rep=1;rep<=nReps;++rep) { BatchProducer bp(*this, dataset,inputSpatialSize,batchSize,4); while(SpatiallySparseBatch* batch=bp.nextBatch()) { processBatch(*batch,0,f,g); for (int i=0;i<batch->interfaces[0].batchSize;++i) { int ii=batch->sampleNumbers[i]; votes[ii][batch->predictions[i][0]]++; for (int j=0;j<dataset.nClasses;++j) probs[ii][j]+=batch->probabilities[i][j]; } delete batch; } float errors=dataset.pictures.size(),nll=0; for (int i=0;i<dataset.pictures.size();++i) { vector<int> predictions=vectorTopIndices(probs[i],nTop); for (int j=0;j<nTop;j++) if (predictions[j]==dataset.pictures[i]->label) errors--; nll-=log(max(probs[i][dataset.pictures[i]->label]/rep,1.0e-15)); } if (!predictionsFilename.empty()) { cout << predictionsFilename << endl; f.open(predictionsFilename.c_str()); f<<header; for (int i=0;i<dataset.pictures.size();++i) { f << dataset.pictures[i]->identify(); for (int j=0;j<dataset.nClasses;++j) f <<"," << probs[i][j]/rep; f <<endl; } f.close(); } cout << dataset.name << " rep " << rep <<"/"<<nReps << " Mistakes: " << 100*errors/dataset.pictures.size() << "% NLL " << nll/dataset.pictures.size() <<endl; // // if (dataset.type==UNLABELLEDBATCH and rep==nReps) { // // ofstream f;f.open("unlabelledData.predictions"); // // ofstream g;g.open("unlabelledData.probabilities"); // // } } }
ACTION( SystemAccount, voteproducer ) { account_name voter; account_name proxy; std::vector<account_name> producers; EOSLIB_SERIALIZE( voteproducer, (voter)(proxy)(producers) ) }; /** * @pre vp.producers must be sorted from lowest to highest * @pre if proxy is set then no producers can be voted for * @pre every listed producer or proxy must have been previously registered * @pre vp.voter must authorize this action * @pre voter must have previously staked some EOS for voting */ static void on( const voteproducer& vp ) { eosio_assert( std::is_sorted( vp.producers.begin(), vp.producers.end() ), "producer votes must be sorted" ); eosio_assert( vp.producers.size() <= 30, "attempt to vote for too many producers" ); if( vp.proxy != 0 ) eosio_assert( vp.producers.size() == 0, "cannot vote for producers and proxy at same time" ); require_auth( vp.voter ); account_votes_index_type avotes( SystemAccount, SystemAccount ); const auto& existing = avotes.get( vp.voter ); std::map<account_name, pair<uint128_t, uint128_t> > producer_vote_changes; uint128_t old_weight = existing.staked.quantity; /// old time uint128_t new_weight = old_weight; /// TODO: update for current weight for( const auto& p : existing.producers ) producer_vote_changes[p].first = old_weight; for( const auto& p : vp.producers ) producer_vote_changes[p].second = new_weight; producer_votes_index_type votes( SystemAccount, SystemAccount ); for( const auto& delta : producer_vote_changes ) { if( delta.second.first != delta.second.second ) { const auto& provote = votes.get( delta.first ); votes.update( provote, 0, [&]( auto& pv ){ pv.total_votes -= delta.second.first; pv.total_votes += delta.second.second; }); } } avotes.update( existing, 0, [&]( auto& av ) { av.proxy = vp.proxy; av.last_update = now(); av.producers = vp.producers; }); }
/** * This method will create a producr_config and producer_votes object for 'producer' * * @pre producer is not already registered * @pre producer to register is an account * @pre authority of producer to register * */ static void on( const regproducer& reg ) { auto producer = reg.producer; require_auth( producer ); producer_votes_index_type votes( SystemAccount, SystemAccount ); const auto* existing = votes.find( producer ); eosio_assert( !existing, "producer already registered" ); votes.emplace( producer, [&]( auto& pv ){ pv.owner = producer; pv.total_votes = 0; }); producer_config_index_type proconfig( SystemAccount, SystemAccount ); proconfig.emplace( producer, [&]( auto& pc ) { pc.owner = producer; pc.packed_key = reg.producer_key; }); }
int main(){ int t; scanf("%d\n", &t); while(t--){ int n, k; scanf("%d %d\n", &n, &k); std::vector<int> votes(n + 1, 0); std::vector<bool> qualified(n + 1, 1); for(int p = 1; p <= n; p++){ int x; scanf("%d", &x); ++votes[x]; qualified[p] = (x != p); } int count(0); for(int p = 1; p <= n; p++){if(qualified[p] && votes[p] >= k){++count;}} printf("%d\n", count); } return 0; }
ACTION( SystemAccount, stakevote ) { account_name voter; system_token_type amount; EOSLIB_SERIALIZE( stakevote, (voter)(amount) ) }; static void on( const stakevote& sv ) { print( "on stake vote\n" ); eosio_assert( sv.amount.quantity > 0, "must stake some tokens" ); require_auth( sv.voter ); account_votes_index_type avotes( SystemAccount, SystemAccount ); const auto* acv = avotes.find( sv.voter ); if( !acv ) { acv = &avotes.emplace( sv.voter, [&]( auto& av ) { av.owner = sv.voter; av.last_update = now(); av.proxy = 0; }); } uint128_t old_weight = acv->staked.quantity; uint128_t new_weight = old_weight + sv.amount.quantity; producer_votes_index_type votes( SystemAccount, SystemAccount ); for( auto p : acv->producers ) { votes.update( votes.get( p ), 0, [&]( auto& v ) { v.total_votes -= old_weight; v.total_votes += new_weight; }); } avotes.update( *acv, 0, [&]( auto av ) { av.last_update = now(); av.staked += sv.amount; }); currency::inline_transfer( sv.voter, SystemAccount, sv.amount, "stake for voting" ); }
void Combinator::operator() (Data * dt, int ** classification, Classifier ** classifiers, int numClassifiers) { int i; int chosen; std::vector<int> votes(dt->getNLabels()); for (i = 0; i < dt->getNSamples(); i++) { for (int & v : votes) v = 0; for (int j = 0; j < numClassifiers; j++) ++votes[classification[j][i] - 1]; chosen = 0; for (int j = 1; j < dt->getNLabels(); j++) if (votes[j] > votes[chosen]) chosen = j; dt->setClassificationLabel(i, chosen + 1); } }
void WeightedVote::operator()(Data * dt, int ** classification, Classifier ** classifiers, int numClassifiers) { int i; int chosen; std::vector<float> votes(dt->getNLabels()); for (i = 0; i < dt->getNSamples(); i++) { for (auto & v : votes) v = 0.0f; for (int j = 0; j < numClassifiers; j++) votes[classification[j][i] - 1] += weight[j]; chosen = 0; for (int j = 1; j < dt->getNLabels(); j++) if (votes[j] > votes[chosen]) chosen = j; dt->setClassificationLabel(i, chosen + 1); } }
// update w weights void GraphHandler::updateWeights(vector<vector<unsigned int>> &clustering, vector<unsigned int> &medians){ if (clustering.size() != medians.size()){ cout << "error\n"; } vector<unsigned int> votes(w.size(), 0); unsigned int votes_sum = 0; for (unsigned int i=0; i<w.size(); i++){ for (unsigned int j=0; j<clustering.size(); j++){ for (unsigned int k=0; k<clustering[j].size(); k++){ votes[i] += vote(i, clustering[j][k], medians[j]); } } votes_sum += votes[i]; } w_sum = 0; for (unsigned int i=0; i<w.size(); i++){ w[i] = 0.5 * (w[i] + (double (votes[i] * w.size())) / double (votes_sum)); w_sum += w[i]; } };
void Consensus::findConsensus(const vector<Point2f> & points, const vector<int> & classes, const float scale, const float rotation, Point2f & center, vector<Point2f> & points_inlier, vector<int> & classes_inlier) { FILE_LOG(logDEBUG) << "Consensus::findConsensus() call"; //If no points are available, reteurn nan if (points.size() == 0) { center.x = numeric_limits<float>::quiet_NaN(); center.y = numeric_limits<float>::quiet_NaN(); FILE_LOG(logDEBUG) << "Consensus::findConsensus() return"; return; } //Compute votes vector<Point2f> votes(points.size()); for (size_t i = 0; i < points.size(); i++) { votes[i] = points[i] - scale * rotate(points_normalized[classes[i]], rotation); } t_index N = points.size(); float * D = new float[N*(N-1)/2]; //This is a lot of memory, so we put it on the heap cluster_result Z(N-1); //Compute pairwise distances between votes int index = 0; for (size_t i = 0; i < points.size(); i++) { for (size_t j = i+1; j < points.size(); j++) { //TODO: This index calculation is correct, but is it a good thing? //int index = i * (points.size() - 1) - (i*i + i) / 2 + j - 1; D[index] = norm(votes[i] - votes[j]); index++; } } FILE_LOG(logDEBUG) << "Consensus::MST_linkage_core() call"; MST_linkage_core(N,D,Z); FILE_LOG(logDEBUG) << "Consensus::MST_linkage_core() return"; union_find nodes(N); //Sort linkage by distance ascending std::stable_sort(Z[0], Z[N-1]); //S are cluster sizes int* S = (int*)alloca((2*N-1)*sizeof(int)); //TODO: Why does this loop go to 2*N-1? Shouldn't it be simply N? Everything > N gets overwritten later for(int i = 0; i < 2*N-1; i++) { S[i] = 1; } t_index parent = 0; //After the loop ends, parent contains the index of the last cluster for (node const * NN=Z[0]; NN!=Z[N-1]; ++NN) { // Get two data points whose clusters are merged in step i. // Find the cluster identifiers for these points. t_index node1 = nodes.Find(NN->node1); t_index node2 = nodes.Find(NN->node2); // Merge the nodes in the union-find data structure by making them // children of a new node // if the distance is appropriate if (NN->dist < thr_cutoff) { parent = nodes.Union(node1, node2); S[parent] = S[node1] + S[node2]; } } //Get cluster labels int* T = (int*)alloca(N*sizeof(int)); for (t_index i = 0; i < N; i++) { T[i] = nodes.Find(i); } //Find largest cluster int S_max = distance(S, max_element(S, S + 2*N-1)); //Find inliers, compute center of votes points_inlier.reserve(S[S_max]); classes_inlier.reserve(S[S_max]); center.x = center.y = 0; for (size_t i = 0; i < points.size(); i++) { //If point is in consensus cluster if (T[i] == S_max) { points_inlier.push_back(points[i]); classes_inlier.push_back(classes[i]); center.x += votes[i].x; center.y += votes[i].y; } } center.x /= points_inlier.size(); center.y /= points_inlier.size(); delete[] D; FILE_LOG(logDEBUG) << "Consensus::findConsensus() return"; }
std::vector<poseT> RefinePoses(const pcl::PointCloud<myPointXYZ>::Ptr scene, const std::vector<ModelT> &mesh_set, const std::vector<poseT> &all_poses) { int pose_num = all_poses.size(); std::vector<ModelT> est_models(pose_num); pcl::PointCloud<myPointXYZ>::Ptr down_scene(new pcl::PointCloud<myPointXYZ>()); pcl::VoxelGrid<myPointXYZ> sor; sor.setInputCloud(scene); sor.setLeafSize(0.005, 0.005, 0.005); sor.filter(*down_scene); #pragma omp parallel for schedule(dynamic, 1) for(int i = 0 ; i < pose_num ; i++ ){ for( int j = 0 ; j < mesh_set.size() ; j++ ){ if( mesh_set[j].model_label == all_poses[i].model_name ) { est_models[i].model_label = all_poses[i].model_name; est_models[i].model_cloud = pcl::PointCloud<myPointXYZ>::Ptr (new pcl::PointCloud<myPointXYZ>()); pcl::transformPointCloud(*mesh_set[j].model_cloud, *est_models[i].model_cloud, all_poses[i].shift, all_poses[i].rotation); break; } } } std::vector< pcl::search::KdTree<myPointXYZ>::Ptr > tree_set(est_models.size()); #pragma omp parallel for schedule(dynamic, 1) for( int i = 0 ; i < pose_num ; i++ ) { tree_set[i] = pcl::search::KdTree<myPointXYZ>::Ptr (new pcl::search::KdTree<myPointXYZ>()); tree_set[i]->setInputCloud(est_models[i].model_cloud); } std::vector<int> votes(pose_num, 0); std::vector< std::vector<int> > adj_graph(pose_num); for( int i = 0 ; i < pose_num ; i++ ) adj_graph[i].resize(pose_num, 0); float sqrT = 0.01*0.01; int down_num = down_scene->size(); std::vector< std::vector<int> > bin_vec(down_num); #pragma omp parallel for for(int i = 0 ; i < pose_num ; i++ ) { int count = 0; for( pcl::PointCloud<myPointXYZ>::const_iterator it = down_scene->begin() ; it < down_scene->end() ; it++, count++ ) { std::vector<int> idx (1); std::vector<float> sqrDist (1); int nres = tree_set[i]->nearestKSearch(*it, 1, idx, sqrDist); if ( nres >= 1 && sqrDist[0] <= sqrT ) { #pragma omp critical { bin_vec[count].push_back(i); } votes[i]++; } } } for( int it = 0 ; it < down_num ; it++ ) for( std::vector<int>::iterator ii = bin_vec[it].begin() ; ii < bin_vec[it].end() ; ii++ ) for( std::vector<int>::iterator jj = ii+1 ; jj < bin_vec[it].end() ; jj++ ) { adj_graph[*ii][*jj]++; adj_graph[*jj][*ii]++; } std::vector<bool> dead_flag(pose_num, 0); for( int i = 0 ; i < pose_num ; i++ ){ if( dead_flag[i] == true ) continue; for( int j = i+1 ; j < pose_num ; j++ ) { if( dead_flag[j] == true ) continue; int min_tmp = std::min(votes[i], votes[j]); if( (adj_graph[i][j]+0.0) / min_tmp >= 0.75 ) { if( votes[i] > votes[j] ) dead_flag[j] = true; else { dead_flag[i] = true; break; } } } } std::vector<poseT> refined_poses; for( int i = 0 ; i < pose_num ; i++ ) if( dead_flag[i] == false ) refined_poses.push_back(all_poses[i]); return refined_poses; }
uint32_t Book::currVotes() const { return votes(simulation()->t()); }