uint64_t e014(){ uint32_t n = 1000000, longest = 0, ret = 0; std::vector<uint32_t> chains (n, 0); for(uint32_t i = 1; i < n; ++i){ uint32_t m = i, length = 1; while(m != 1){ if(m & 1) m = 3 * m + 1; else m /= 2; if(m < n && chains[m] != 0){ length += chains[m]; break; } else length++; } if(length > longest){ longest = length; ret = i; } } return ret; }
void BottomUpLayout::combineChains() { StlVector<BasicBlock*> chains(mm); const Nodes& nodes = irManager->getFlowGraph()->getNodes(); for (Nodes::const_iterator it = nodes.begin(), end = nodes.end(); it!=end; ++it) { Node* node = *it; if (firstInChain[node->getDfNum()]) { assert(node->isBlockNode()); chains.push_back((BasicBlock*)node); } } std::sort(chains.begin(), chains.end(), chains_comparator(prevInLayoutBySuccessorId, (BasicBlock*)irManager->getFlowGraph()->getEntryNode())); assert(*chains.begin() ==irManager->getFlowGraph()->getEntryNode()); assert(*chains.begin() == irManager->getFlowGraph()->getEntryNode()); for (U_32 i = 0, n = (U_32)chains.size()-1; i<n;i++) { BasicBlock* firstChain = chains[i]; BasicBlock* secondChain= chains[i+1]; BasicBlock* lastInFirst = firstChain; while (lastInFirst->getLayoutSucc()!=NULL) { lastInFirst = lastInFirst->getLayoutSucc(); } lastInFirst->setLayoutSucc(secondChain); } }
TEST(gm,issue109_csv_header_consistent_with_samples) { std::vector<std::string> model_path; model_path.push_back("src"); model_path.push_back("test"); model_path.push_back("test-models"); model_path.push_back("compiled"); model_path.push_back("issue109"); std::string path = convert_model_path(model_path); std::string samples = path + ".csv"; std::string command = path + " sample num_warmup=0 num_samples=1" + " output file=" + samples; run_command_output out = run_command(command); EXPECT_EQ(int(stan::gm::error_codes::OK), out.err_code); EXPECT_FALSE(out.hasError); std::ifstream ifstream; ifstream.open(samples.c_str()); stan::mcmc::chains<> chains(stan::io::stan_csv_reader::parse(ifstream)); ifstream.close(); EXPECT_EQ(1, chains.num_samples()); EXPECT_FLOAT_EQ(1, chains.samples("z[1,1]")(0)); EXPECT_FLOAT_EQ(2, chains.samples("z[1,2]")(0)); EXPECT_FLOAT_EQ(3, chains.samples("z[2,1]")(0)); EXPECT_FLOAT_EQ(4, chains.samples("z[2,2]")(0)); EXPECT_FLOAT_EQ(1, chains.samples("z_mat[1,1]")(0)); EXPECT_FLOAT_EQ(2, chains.samples("z_mat[1,2]")(0)); EXPECT_FLOAT_EQ(3, chains.samples("z_mat[2,1]")(0)); EXPECT_FLOAT_EQ(4, chains.samples("z_mat[2,2]")(0)); }
TEST(gm,issue109_csv_header_consistent_with_samples) { char path_separator = get_path_separator(); std::vector<std::string> model_path; model_path.push_back("src"); model_path.push_back("test"); model_path.push_back("gm"); model_path.push_back("model_specs"); model_path.push_back("compiled"); model_path.push_back("issue109"); std::string path = convert_model_path(model_path); std::string samples = path + ".csv"; std::string command = path + " --iter=1" + " --warmup=0" + " --samples=" + samples; run_command(command); std::ifstream ifstream; ifstream.open(samples.c_str()); stan::mcmc::chains<> chains(stan::io::stan_csv_reader::parse(ifstream)); ifstream.close(); EXPECT_EQ(1, chains.num_samples()); EXPECT_FLOAT_EQ(1, chains.samples("z[1,1]")(0)); EXPECT_FLOAT_EQ(2, chains.samples("z[1,2]")(0)); EXPECT_FLOAT_EQ(3, chains.samples("z[2,1]")(0)); EXPECT_FLOAT_EQ(4, chains.samples("z[2,2]")(0)); }
int main(int argc, char** argv){ if (argc != 3 ){ std::cerr << "Please provide a filename and the chains to select" << std::endl; return EXIT_FAILURE; } std::list<std::string> chains_to_select; std::string chains(argv[2]); chains_to_select.push_back(chains); //discard hydrogens and water ESBTL::PDB_line_selector_chain sel(chains_to_select.begin(),chains_to_select.end(),false,false); std::vector<ESBTL::Default_system> systems; ESBTL::All_atom_system_builder<ESBTL::Default_system> builder(systems,sel.max_nb_systems()); typedef ESBTL::Generic_classifier<ESBTL::Name_and_radius_of_atom<double,ESBTL::Default_system::Atom> > T_Atom_classifier; T_Atom_classifier atom_classif; std::list<ESBTL::Point_3> solvant; if (ESBTL::read_a_pdb_file(argv[1],sel,builder,Accept_none_occupancy_policy())){ const ESBTL::Default_system::Model& model=*systems[0].models_begin(); std::pair<ESBTL::Point_3,double> cube=ESBTL::bounding_cube<ESBTL::Point_3>(model.atoms_begin(),model.atoms_end()); ESBTL::fcc_lattice<ESBTL::Point_3>(cube,1.4,10,std::back_inserter(solvant)); std::list<std::list<ESBTL::Point_3>::iterator > in_conflict; //find water molecule in conflict with the model report_steric_clashes<ESBTL::Point_3>( solvant.begin(),solvant.end(),model.atoms_begin(),model.atoms_end(),Simple_clash(),std::back_inserter(in_conflict) ); std::cout << "Before removing " << solvant.size() << std::endl; for( std::list<std::list<ESBTL::Point_3>::iterator >::iterator it=in_conflict.begin();it!=in_conflict.end();++it ){ solvant.erase(*it); } std::cout << "After removing " << solvant.size() << std::endl; unsigned nb_atoms=0; for (ESBTL::Default_system::Model::Atoms_const_iterator it_atm=model.atoms_begin();it_atm!=model.atoms_end();++it_atm){ T_Atom_classifier::Properties type=atom_classif.get_properties(*it_atm); ++nb_atoms; } std::cout << nb_atoms << std::endl; std::cout << "from pymol.cgo import *\nfrom pymol import cmd\nobj=[COLOR, 1.0, 0, 0," << 1 << "\n"; std::copy(solvant.begin(),solvant.end(),std::ostream_iterator<ESBTL::Point_3>(std::cout,"")); std::cout << "]\ncmd.load_cgo(obj,'cgo01')" <<std::endl; } }
/** * The Stan print function. * * @param argc Number of arguments * @param argv Arguments * * @return 0 for success, * non-zero otherwise */ int main(int argc, const char* argv[]) { if (argc == 1) { print_usage(); return 0; } // Parse any arguments specifying filenames std::ifstream ifstream; std::vector<std::string> filenames; for (int i = 1; i < argc; i++) { if (std::string(argv[i]).find("--autocorr=") != std::string::npos) continue; if (std::string(argv[i]).find("--sig_figs=") != std::string::npos) continue; if (std::string("--help") == std::string(argv[i])) { print_usage(); return 0; } ifstream.open(argv[i]); if (ifstream.good()) { filenames.push_back(argv[i]); ifstream.close(); } else { std::cout << "File " << argv[i] << " not found" << std::endl; } } if (!filenames.size()) { std::cout << "No valid input files, exiting." << std::endl; return 0; } // Parse specified files Eigen::VectorXd warmup_times(filenames.size()); Eigen::VectorXd sampling_times(filenames.size()); Eigen::VectorXi thin(filenames.size()); ifstream.open(filenames[0].c_str()); stan::io::stan_csv stan_csv = stan::io::stan_csv_reader::parse(ifstream); warmup_times(0) = stan_csv.timing.warmup; sampling_times(0) = stan_csv.timing.sampling; stan::mcmc::chains<> chains(stan_csv); ifstream.close(); thin(0) = stan_csv.metadata.thin; for (std::vector<std::string>::size_type chain = 1; chain < filenames.size(); chain++) { ifstream.open(filenames[chain].c_str()); stan_csv = stan::io::stan_csv_reader::parse(ifstream); chains.add(stan_csv); ifstream.close(); thin(chain) = stan_csv.metadata.thin; warmup_times(chain) = stan_csv.timing.warmup; sampling_times(chain) = stan_csv.timing.sampling; } double total_warmup_time = warmup_times.sum(); double total_sampling_time = sampling_times.sum(); // Compute largest variable name length const int skip = 0; std::string model_name = stan_csv.metadata.model; size_t max_name_length = 0; for (int i = skip; i < chains.num_params(); i++) if (chains.param_name(i).length() > max_name_length) max_name_length = chains.param_name(i).length(); for (int i = 0; i < 2; i++) if (chains.param_name(i).length() > max_name_length) max_name_length = chains.param_name(i).length(); // Prepare values int n = 9; Eigen::MatrixXd values(chains.num_params(), n); values.setZero(); Eigen::VectorXd probs(3); probs << 0.05, 0.5, 0.95; for (int i = 0; i < chains.num_params(); i++) { double sd = chains.sd(i); double n_eff = chains.effective_sample_size(i); values(i,0) = chains.mean(i); values(i,1) = sd / sqrt(n_eff); values(i,2) = sd; Eigen::VectorXd quantiles = chains.quantiles(i,probs); for (int j = 0; j < 3; j++) values(i,3+j) = quantiles(j); values(i,6) = n_eff; values(i,7) = n_eff / total_sampling_time; values(i,8) = chains.split_potential_scale_reduction(i); } // Prepare header Eigen::Matrix<std::string, Eigen::Dynamic, 1> headers(n); headers << "Mean", "MCSE", "StdDev", "5%", "50%", "95%", "N_Eff", "N_Eff/s", "R_hat"; // Set sig figs Eigen::VectorXi column_sig_figs(n); int sig_figs = 2; for (int k = 1; k < argc; k++) if (std::string(argv[k]).find("--sig_figs=") != std::string::npos) sig_figs = atoi(std::string(argv[k]).substr(11).c_str()); // Compute column widths Eigen::VectorXi column_widths(n); Eigen::Matrix<std::ios_base::fmtflags, Eigen::Dynamic, 1> formats(n); column_widths = calculate_column_widths(values, headers, sig_figs, formats); // Initial output std::cout << "Inference for Stan model: " << model_name << std::endl << chains.num_chains() << " chains: each with iter=(" << chains.num_kept_samples(0); for (int chain = 1; chain < chains.num_chains(); chain++) std::cout << "," << chains.num_kept_samples(chain); std::cout << ")"; // Timing output std::cout << "; warmup=(" << chains.warmup(0); for (int chain = 1; chain < chains.num_chains(); chain++) std::cout << "," << chains.warmup(chain); std::cout << ")"; std::cout << "; thin=(" << thin(0); for (int chain = 1; chain < chains.num_chains(); chain++) std::cout << "," << thin(chain); std::cout << ")"; std::cout << "; " << chains.num_samples() << " iterations saved." << std::endl << std::endl; std::string warmup_unit = "seconds"; if (total_warmup_time / 3600 > 1) { total_warmup_time /= 3600; warmup_unit = "hours"; } else if (total_warmup_time / 60 > 1) { total_warmup_time /= 60; warmup_unit = "minutes"; } std::cout << "Warmup took (" << std::fixed << std::setprecision(compute_precision(warmup_times(0), sig_figs, false)) << warmup_times(0); for (int chain = 1; chain < chains.num_chains(); chain++) std::cout << ", " << std::fixed << std::setprecision(compute_precision(warmup_times(chain), sig_figs, false)) << warmup_times(chain); std::cout << ") seconds, "; std::cout << std::fixed << std::setprecision(compute_precision(total_warmup_time, sig_figs, false)) << total_warmup_time << " " << warmup_unit << " total" << std::endl; std::string sampling_unit = "seconds"; if (total_sampling_time / 3600 > 1) { total_sampling_time /= 3600; sampling_unit = "hours"; } else if (total_sampling_time / 60 > 1) { total_sampling_time /= 60; sampling_unit = "minutes"; } std::cout << "Sampling took (" << std::fixed << std::setprecision(compute_precision(sampling_times(0), sig_figs, false)) << sampling_times(0); for (int chain = 1; chain < chains.num_chains(); chain++) std::cout << ", " << std::fixed << std::setprecision(compute_precision(sampling_times(chain), sig_figs, false)) << sampling_times(chain); std::cout << ") seconds, "; std::cout << std::fixed << std::setprecision(compute_precision(total_sampling_time, sig_figs, false)) << total_sampling_time << " " << sampling_unit << " total" << std::endl; std::cout << std::endl; // Header output std::cout << std::setw(max_name_length + 1) << ""; for (int i = 0; i < n; i++) { std::cout << std::setw(column_widths(i)) << headers(i); } std::cout << std::endl; // Value output for (int i = skip; i < chains.num_params(); i++) { if (!is_matrix(chains.param_name(i))) { std::cout << std::setw(max_name_length + 1) << std::left << chains.param_name(i); std::cout << std::right; for (int j = 0; j < n; j++) { std::cout.setf(formats(j), std::ios::floatfield); std::cout << std::setprecision( compute_precision(values(i,j), sig_figs, formats(j) == std::ios_base::scientific)) << std::setw(column_widths(j)) << values(i, j); } std::cout << std::endl; } else { std::vector<int> dims = dimensions(chains, i); std::vector<int> index(dims.size(), 1); int max = 1; for (int j = 0; j < dims.size(); j++) max *= dims[j]; for (int k = 0; k < max; k++) { int param_index = i + matrix_index(index, dims); std::cout << std::setw(max_name_length + 1) << std::left << chains.param_name(param_index); std::cout << std::right; for (int j = 0; j < n; j++) { std::cout.setf(formats(j), std::ios::floatfield); std::cout << std::setprecision(compute_precision(values(param_index,j), sig_figs, formats(j) == std::ios_base::scientific)) << std::setw(column_widths(j)) << values(param_index, j); } std::cout << std::endl; next_index(index, dims); } i += max-1; } } /// Footer output std::cout << std::endl; std::cout << "Samples were drawn using " << stan_csv.metadata.algorithm << " with " << stan_csv.metadata.engine << "." << std::endl << "For each parameter, N_Eff is a crude measure of effective sample size," << std::endl << "and R_hat is the potential scale reduction factor on split chains (at " << std::endl << "convergence, R_hat=1)." << std::endl << std::endl; // Print autocorrelation, if desired for (int k = 1; k < argc; k++) { if (std::string(argv[k]).find("--autocorr=") != std::string::npos) { const int c = atoi(std::string(argv[k]).substr(11).c_str()); if (c < 0 || c >= chains.num_chains()) { std::cout << "Bad chain index " << c << ", aborting autocorrelation display." << std::endl; break; } Eigen::MatrixXd autocorr(chains.num_params(), chains.num_samples(c)); for (int i = 0; i < chains.num_params(); i++) { autocorr.row(i) = chains.autocorrelation(c, i); } // Format and print header std::cout << "Displaying the autocorrelations for chain " << c << ":" << std::endl; std::cout << std::endl; const int n_autocorr = autocorr.row(0).size(); int lag_width = 1; int number = n_autocorr; while ( number != 0) { number /= 10; lag_width++; } std::cout << std::setw(lag_width > 4 ? lag_width : 4) << "Lag"; for (int i = 0; i < chains.num_params(); ++i) { std::cout << std::setw(max_name_length + 1) << std::right << chains.param_name(i); } std::cout << std::endl; // Print body for (int n = 0; n < n_autocorr; ++n) { std::cout << std::setw(lag_width) << std::right << n; for (int i = 0; i < chains.num_params(); ++i) { std::cout << std::setw(max_name_length + 1) << std::right << autocorr(i, n); } std::cout << std::endl; } } } return 0; }
virtual void Execute(const DataObject &object) { // Only execute if we're supposed to. if(false == object.read<bool>()) { return; } // So, here's how the Longest Road algorithm works: // // 1. If no player currently has Longest Road... // a) and no one has more than 5 connected roads, no one gets // Longest Road. // b) and multiple players have more than 5 connected roads, but // are tied, no one gets Longest Road. // c) and any one player has more than 5 connected roads and is in // the lead, they get Longest Road. // 2. If someone already has Longest Road... // a) and anyone else ties them in length, the original player // retains Longest Road. // b) and multiple players surpass them in length, but are tied, // Longest Road reverts to unclaimed. // c) and any one person surpasses them and everyone else in // length, they get Longest Road. // d) and the player drops below 5 in road length, loses Longest // Road, with Longest Road going to the appropriate player // based on the previous criteria. // // With that in mind, run through each player, retrieving their // longest chain of roads. wxInt32 players = numPlayers(); wxInt32 current = gameData<wxInt32>(shLongestRoadPlayer); std::vector<wxInt32> lengths(numPlayers(), 0); std::vector<PlayerGame::SideObjectArray> chains(numPlayers()); PlayerGame::SideObjectArray longestChain; for(wxInt32 i = 0; i < players; ++i) { DataObject input(i), output; RULE.Decide(shLogicRoadLength, input, output); lengths[i] = output.read<wxInt32>(); chains[i] = output.read<PlayerGame::SideObjectArray>(1); // While we're going through each player, clear any Longest Road // highlighting. PlayerGame::SideObjectArray objects; playerGame(i).getAllSideObjects(objects); PlayerGame::SideObjectArray::const_iterator it, itEnd = objects.end(); for(it = objects.begin(); it != itEnd; ++it) { (*it)->longest(false); } } // Now that we have all the lengths, figure out if Longest Road // changes. wxInt32 longestLength = 0; wxInt32 longestPlayer = -1; // In the special case where the original player drops below 5 in road // length, clear them out first and then apply the normal rules. if( (-1 != current) && (5 > lengths[current])) { RULE.Execute(shRuleLoseLongestRoad, DataObject(current)); current = -1; } // No current holder. if(-1 == current) { // With no current holder, see if anyone is over 5 and the clear // leader (no ties). for(wxInt32 i = 0; i < players; ++i) { wxInt32 len = lengths[i]; if(5 <= len) { // A clear leader. if(longestLength < len) { longestLength = len; longestPlayer = i; } // A tie. else if(longestLength == len) { longestPlayer = -1; } } } } // Someone already has it. else { // Set the mark that must be beaten. longestPlayer = current; longestLength = lengths[current]; for(wxInt32 i = 0; i < players; ++i) { // Skip the current holder. if(i == current) { continue; } wxInt32 len = lengths[i]; // Look for a clear leader. if(longestLength < len) { longestLength = len; longestPlayer = i; } // Here's where it gets interesting. If they tie the original // player, it counts for nothing. However, if they tie a new // player who has already surpassed the original player, then // it's a deadlock, and no one is the longest player. else if((longestLength == len) && (longestPlayer != current)) { longestPlayer = -1; } } } // Always update the highlighting on the longest road. if(-1 != longestPlayer) { PlayerGame::SideObjectArray& chain = chains[longestPlayer]; // Mark all of the longest chain. PlayerGame::SideObjectArray::const_iterator it, itEnd = chain.end(); for(it = chain.begin(); it != itEnd; ++it) { (*it)->longest(true); } } // If it has changed hands, take it away from the loser and award it // to the winner. if(current != longestPlayer) { // Take it away from the loser. if(-1 != current) { RULE.Execute(shRuleLoseLongestRoad, DataObject(current)); } // Give it to the winner. if(-1 != longestPlayer) { RULE.Execute(shRuleGainLongestRoad, DataObject(longestPlayer)); } } // Regardless of what happens, update every player. Controller::get().Transmit(shEventPlayerUI, DataObject(GetGame(), -1)); }