/* Merges two VectorSiteContainers into a single VectorSiteContainer */ shared_ptr<VectorSiteContainer> merge_vscs(shared_ptr<VectorSiteContainer> s1, shared_ptr<VectorSiteContainer> s2) throw (AlphabetMismatchException, Exception) { if (s1->getAlphabet()->getAlphabetType() != s2->getAlphabet()->getAlphabetType()) throw AlphabetMismatchException("SiteContainerTools::merge.", s1->getAlphabet(), s2->getAlphabet()); auto alphabet = s1->getAlphabet(); const int unknown_char = alphabet->getUnknownCharacterCode(); size_t l1 = s1->getNumberOfSites(); size_t l2 = s2->getNumberOfSites(); auto n1 = s1->getSequencesNames(); auto n2 = s2->getSequencesNames(); vector<string> allnames = merge_vectors(n1, n2); unique_ptr<VectorSequenceContainer> tmp_container(new VectorSequenceContainer(alphabet)); for (auto &n : allnames) { vector<int> seqvec1; vector<int> seqvec2; seqvec1.reserve(l1+l2); seqvec2.reserve(l2); if (s1->hasSequence(n)) { auto tmpvec = s1->getSequence(n).getContent(); seqvec1.insert( seqvec1.end(), tmpvec.begin(), tmpvec.end()); } else { vector<int> tmpvec(l1, unknown_char); seqvec1.insert( seqvec1.end(), tmpvec.begin(), tmpvec.end()); } if (s2->hasSequence(n)) { auto tmpvec = s2->getSequence(n).getContent(); seqvec2.insert( seqvec2.end(), tmpvec.begin(), tmpvec.end()); } else { vector<int> tmpvec(l2, unknown_char); seqvec2.insert( seqvec2.end(), tmpvec.begin(), tmpvec.end()); } seqvec1.insert(seqvec1.end(), seqvec2.begin(), seqvec2.end()); unique_ptr<BasicSequence> seq(new BasicSequence(n, seqvec1, alphabet)); tmp_container->addSequence(*seq, false); } auto output = make_shared<VectorSiteContainer>(*tmp_container); return output; }
std::vector<int> Lotto(int qty, int ch) { int buff[qty]; for (int i = 1, limit = qty+1; i < limit; i++ ) buff[i-1] = i; std::vector<int> tmpvec (buff, buff + qty); std::vector<int> tmpres(ch); std::random_shuffle(tmpvec.begin(), tmpvec.end()); std::copy(tmpvec.begin(), tmpvec.begin()+ch, tmpres.begin()); return tmpres; }
void egHWU::rdWeight(string inputfile) { gfun::printLOG("Reading Weight Matrix at [ " + inputfile + " ]\n"); ifstream result(inputfile.c_str()); if(!result) gfun::error("\ncould not read the Weight Matrix file\n"); //result<<"#result of association scanning (0=A1A1, 1=A1A2, 2=A2A2)\n"; //result<<"#order\t"<<"Chr\tName\tPos\tBp\tAllel1\tAllel2\t"<<"P_value\n"; //vector<string> str_vec; //string buf_str; //getline(result,buf_str); //int dim=gfun::split_string(buf_str,str_vec); int dim=0; result>>dim; if(dim!=_n_sub) gfun::error("Dim of Matrix != sample size\n"); vector<double> tmpvec(dim,0); vector< vector<double> > weight(dim,tmpvec); double tmp; for(int i=0; i<_n_sub; i++){ for(int j=i; j<_n_sub; j++){ result>>tmp; double tmptmp=tmp; weight[i][j]=tmptmp; weight[j][i]=weight[i][j]; //_weight->assign(i,j,tmp); //_weight->assign(j,i,tmp); } } result.close(); //assign to _weight _Kappa.resize(_n_sub,_n_sub); for(int i=0; i<weight.size(); i++){ for(int j=0; j<_n_sub; j++){ _Kappa(i,j)=weight[i][j]; } } _weight_flag=true; cout<<"\nRelateness Matrix Done.\n\n"; }
unsigned int optCoef(arma::mat& weights, const arma::icube& obs, const arma::cube& emission, const arma::mat& initk, const arma::cube& beta, const arma::mat& scales, arma::mat& coef, const arma::mat& X, const arma::ivec& cumsumstate, const arma::ivec& numberOfStates, int trace) { int iter = 0; double change = 1.0; while ((change > 1e-10) & (iter < 100)) { arma::vec tmpvec(X.n_cols * (weights.n_rows - 1)); bool solve_ok = arma::solve(tmpvec, hCoef(weights, X), gCoef(obs, beta, scales, emission, initk, weights, X, cumsumstate, numberOfStates)); if (solve_ok == false) { return (4); } arma::mat coefnew(coef.n_rows, coef.n_cols - 1); for (unsigned int i = 0; i < (weights.n_rows - 1); i++) { coefnew.col(i) = coef.col(i + 1) - tmpvec.subvec(i * X.n_cols, (i + 1) * X.n_cols - 1); } change = arma::accu(arma::abs(coef.submat(0, 1, coef.n_rows - 1, coef.n_cols - 1) - coefnew)) / coefnew.n_elem; coef.submat(0, 1, coef.n_rows - 1, coef.n_cols - 1) = coefnew; iter++; if (trace == 3) { Rcout << "coefficient optimization iter: " << iter; Rcout << " new coefficients: " << std::endl << coefnew << std::endl; Rcout << " relative change: " << change << std::endl; } weights = exp(X * coef).t(); if (!weights.is_finite()) { return (5); } weights.each_row() /= sum(weights, 0); } return (0); }
bool NetServer::loadFromSavegame( ClientPtr clt, Cockpit *cp ) { ObjSerial cltserial = getUniqueSerial(); QVector tmpvec( 0, 0, 0 ); bool update = true; float credits; vector< string >savedships; string str( "" ); COUT<<"-> LOADING SAVE FROM NETWORK"<<endl; cp->savegame->SetStarSystem( string() ); cp->savegame->ParseSaveGame( "", str, "", tmpvec, update, credits, savedships, cltserial, clt->savegame[0], false ); //Generate the system we enter in if needed and add the client in it if ( savedships.empty() ) { COUT<<"There are no saved ships... corrupted save file for "<<clt->callsign<<endl; std::string logoutnetbuf; addSimpleChar( logoutnetbuf, ACCT_LOGOUT ); addSimpleString( logoutnetbuf, clt->callsign ); addSimpleString( logoutnetbuf, clt->passwd ); //We can't find the unit saved for player -> send a login error this->logout( clt ); return false; } COUT<<"\tcredits = "<<credits<<endl; COUT<<"\tfaction = "<<cp->savegame->GetPlayerFaction()<<endl; COUT<<"-> SAVE LOADED"<<endl; //WARNING : WE DON'T SAVE FACTION NOR FLIGHTGROUP YET COUT<<"-> UNIT FACTORY WITH XML"<<endl; //We may have to determine which is the current ship of the player if we handle several ships for one player string PLAYER_SHIPNAME = savedships[0]; string PLAYER_FACTION_STRING = cp->savegame->GetPlayerFaction(); int saved_faction = FactionUtil::GetFactionIndex( PLAYER_FACTION_STRING ); //vector<vector <string> > path = lookforUnit( savedships[0].c_str(), saved_faction, false); bool exist = true; //(VSFileSystem::LookForFile( savedships[0], VSFileSystem::UnitFile)<=VSFileSystem::Ok); static std::string loadfailed( "LOAD_FAILED" ); Unit *un = NULL; if ( !PLAYER_SHIPNAME.empty() ) { un = UnitFactory::createUnit( PLAYER_SHIPNAME.c_str(), false, saved_faction, string( "" ), Flightgroup::newFlightgroup( clt->callsign, PLAYER_SHIPNAME, PLAYER_FACTION_STRING, "default", 1, 1, "", "", mission ), 0, &clt->savegame[1] ); } if (!un) { exist = false; } else if (un->name == loadfailed) { exist = false; un->Kill(); } if (!exist) { unsigned short serial = cltserial; std::string logoutnetbuf; addSimpleChar( logoutnetbuf, ACCT_LOGOUT ); addSimpleString( logoutnetbuf, clt->callsign ); addSimpleString( logoutnetbuf, clt->passwd ); //We can't find the unit saved for player -> send a login error this->logout( clt ); Packet p2; //Send the account server a logout info if ( !acct_sock->sendstr( logoutnetbuf ) ) COUT<<"ERROR sending LOGOUT to account server"<<endl; cerr<<"WARNING : Unit file ("<<savedships[0]<<") not found for "<<clt->callsign<<endl; return cp; } COUT<<"\tAFTER UNIT FACTORY WITH XML"<<endl; clt->game_unit.SetUnit( un ); //Assign its serial to client* un->SetSerial( cltserial ); un->SetPosAndCumPos( cp->savegame->GetPlayerLocation() ); //Affect the created unit to the cockpit COUT<<"-> UNIT LOADED"<<endl; cp->SetParent( un, PLAYER_SHIPNAME.c_str(), "", tmpvec ); COUT<<"-> COCKPIT AFFECTED TO UNIT"<<endl; COUT<<"SHIP -- "<<savedships[0]<<" -- LOCATION: x="<<tmpvec.i<<",y="<<tmpvec.j<<",z="<<tmpvec.k<<endl; return true; }
bool NetServer::loadFromNewGame( ClientPtr clt, Cockpit *cp, string fighter ) { ObjSerial cltserial = getUniqueSerial(); string PLAYER_SHIPNAME = fighter; Mission *mission = NULL; if (active_missions.size() > 0) mission = active_missions[0]; if (!mission) { COUT<<"Cannot login player without acctserver: No missions available"; sendLoginError( clt ); return false; } int saved_faction = 0; //NETFIXME: Send faction over network too! cp->savegame->SetSavedCredits( XMLSupport::parse_float( mission->getVariable( "credits", "0" ) ) ); cp->savegame->SetStarSystem( mission->getVariable( "system", "Sol/Sol" ) ); if ( !mission->flightgroups.empty() ) cp->savegame->SetPlayerFaction( mission->flightgroups[0]->faction ); COUT<<"\tcredits = "<<cp->savegame->GetSavedCredits()<<endl; COUT<<"\tfaction = "<<cp->savegame->GetPlayerFaction()<<endl; COUT<<"-> SAVE LOADED"<<endl; cp->credits = cp->savegame->GetSavedCredits(); //WARNING : WE DON'T SAVE FACTION NOR FLIGHTGROUP YET COUT<<"-> UNIT FACTORY WITH XML"<<endl; //We may have to determine which is the current ship of the player if we handle several ships for one player string PLAYER_FACTION_STRING = cp->savegame->GetPlayerFaction(); saved_faction = FactionUtil::GetFactionIndex( PLAYER_FACTION_STRING ); bool exist = true; //(VSFileSystem::LookForFile( savedships[0], VSFileSystem::UnitFile)<=VSFileSystem::Ok); static std::string loadfailed( "LOAD_FAILED" ); Unit *un = NULL; if ( !PLAYER_SHIPNAME.empty() ) { un = UnitFactory::createUnit( PLAYER_SHIPNAME.c_str(), false, saved_faction, string( "" ), Flightgroup::newFlightgroup( clt->callsign, PLAYER_SHIPNAME, PLAYER_FACTION_STRING, "default", 1, 1, "", "", mission ), 0 ); } if (!un) { exist = false; } else if (un->name == loadfailed) { exist = false; un->Kill(); } if (!exist) { //We can't find the unit saved for player -> send a login error this->sendLoginError( clt ); cerr<<"WARNING : Unit file ("<<PLAYER_SHIPNAME<<") not found for "<<clt->callsign<<endl; return false; } COUT<<"\tAFTER UNIT FACTORY WITH XML"<<endl; clt->game_unit.SetUnit( un ); //Assign its serial to client* un->SetSerial( cltserial ); un->PrimeOrders(); //Accept Comm messages //Affect the created unit to the cockpit COUT<<"-> UNIT LOADED"<<endl; QVector tmpvec( 0, 0, 0 ); cp->SetParent( un, PLAYER_SHIPNAME.c_str(), "", tmpvec ); COUT<<"-> COCKPIT AFFECTED TO UNIT"<<endl; { string savestr, xmlstr; clt->savegame.resize( 0 ); //Get the save parts in a string array cp->activeStarSystem = zonemgr->addZone( cp->savegame->GetStarSystem() ); //Needed for GetSaveStrings. SaveNetUtil::GetSaveStrings( clt, savestr, xmlstr, true ); clt->savegame.push_back( savestr ); clt->savegame.push_back( xmlstr ); } return true; }