int main() { int n, i, v; char verb[4], prep[4]; block *blocks[MAX]; block *a, *b; for(i = 0; i < MAX; i++) table[i] = blocks[i] = NULL; /* Read number of blocks `n`. */ Si(n); /* Initialize world with `n` blocks. */ for(i = 0; i < n; i++) { table[i] = blocks[i] = (block *) malloc(sizeof(block)); blocks[i]->above = NULL; blocks[i]->below = NULL; blocks[i]->table_loc = i; blocks[i]->value = i; } while(1) { /* Read the verb. */ Ss(verb); /* Exit when verb is quit. */ if(verb[0] == 'q') break; /* Read the object. */ Si(v); a = blocks[v]; /* Read the preposition. */ Ss(prep); /* Read the complement. */ Si(v); b = blocks[v]; /* Ignore action if it is illegal. */ if(a->table_loc == b->table_loc) continue; /* When verb is `move`, put back that are above the object `a`. */ if(verb[0] == 'm') put_back(a); /* When preposition is `onto`, put back the blocks that are above the complement `b`. */ if(prep[1] == 'n') put_back(b); /* Pile object `a` over complement `b`. */ stack(a, b); } print_table(n); for(i = 0; i < n; i++) free(blocks[i]); return 0; }
int main(int argc, char* argv[]) { Env = TEnv(argc, argv, TNotify::StdNotify); Env.PrepArgs(TStr::Fmt("ragm. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm())); TExeTm ExeTm; Try TStr OutFPrx = Env.GetIfArgPrefixStr("-o:", "", "Output Graph data prefix"); const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "../as20graph.txt", "Input edgelist file name"); const TStr LabelFNm = Env.GetIfArgPrefixStr("-l:", "", "Input file name for node names (Node ID, Node label) "); int OptComs = Env.GetIfArgPrefixInt("-c:", -1, "The number of communities to detect (-1: detect automatically)"); const int MinComs = Env.GetIfArgPrefixInt("-mc:", 5, "Minimum number of communities to try"); const int MaxComs = Env.GetIfArgPrefixInt("-xc:", 100, "Maximum number of communities to try"); const int DivComs = Env.GetIfArgPrefixInt("-nc:", 10, "How many trials for the number of communities"); const int NumThreads = Env.GetIfArgPrefixInt("-nt:", 1, "Number of threads for parallelization"); const double StepAlpha = Env.GetIfArgPrefixFlt("-sa:", 0.3, "Alpha for backtracking line search"); const double StepBeta = Env.GetIfArgPrefixFlt("-sb:", 0.3, "Beta for backtracking line search"); PUNGraph G; TIntStrH NIDNameH; if (InFNm.IsStrIn(".ungraph")) { TFIn GFIn(InFNm); G = TUNGraph::Load(GFIn); } else { G = TAGMUtil::LoadEdgeListStr<PUNGraph>(InFNm, NIDNameH); } if (LabelFNm.Len() > 0) { TSsParser Ss(LabelFNm, ssfTabSep); while (Ss.Next()) { if (Ss.Len() > 0) { NIDNameH.AddDat(Ss.GetInt(0), Ss.GetFld(1)); } } } else { } printf("Graph: %d Nodes %d Edges\n", G->GetNodes(), G->GetEdges()); TVec<TIntV> EstCmtyVV; TExeTm RunTm; TAGMFast RAGM(G, 10, 10); if (OptComs == -1) { printf("finding number of communities\n"); OptComs = RAGM.FindComsByCV(NumThreads, MaxComs, MinComs, DivComs, OutFPrx, StepAlpha, StepBeta); } RAGM.NeighborComInit(OptComs); if (NumThreads == 1 || G->GetEdges() < 1000) { RAGM.MLEGradAscent(0.0001, 1000 * G->GetNodes(), "", StepAlpha, StepBeta); } else { RAGM.MLEGradAscentParallel(0.0001, 1000, NumThreads, "", StepAlpha, StepBeta); } RAGM.GetCmtyVV(EstCmtyVV); TAGMUtil::DumpCmtyVV(OutFPrx + "cmtyvv.txt", EstCmtyVV, NIDNameH); TAGMUtil::SaveGephi(OutFPrx + "graph.gexf", G, EstCmtyVV, 1.5, 1.5, NIDNameH); Catch printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr()); return 0; }
void MakeSignEpinions() { TSsParser Ss("/u/ana/data/EpinionRatings/user_rating.txt", ssfTabSep); //PSignNet Net = TSignNet::New(); TPt<TNodeEDatNet<TInt, TInt> > Net = TNodeEDatNet<TInt, TInt>::New(); TStrHash<TInt> StrSet(Mega(1), true); while (Ss.Next()) { if ( ((TStr)Ss[0]).IsPrefix("#") ) continue; const int SrcNId = StrSet.AddKey(Ss[0]); const int DstNId = StrSet.AddKey(Ss[1]); if (! Net->IsNode(SrcNId)) { Net->AddNode(SrcNId); } if (! Net->IsNode(DstNId)) { Net->AddNode(DstNId); } const int Sign = ((TStr)Ss[2]).GetInt(); Net->AddEdge(SrcNId, DstNId, Sign); } // PrintGraphStatTable(Graph, OutFNm, Desc); TStr OutFNm = "soc-sign-epinions-user-ratings"; TStr Desc = "Epinions signed social network"; // copied from gio.h - line 111 FILE *F = fopen(OutFNm.CStr(), "wt"); fprintf(F, "# Directed graph: %s\n", OutFNm.CStr()); if (! Desc.Empty()) fprintf(F, "# %s\n", (Desc).CStr()); fprintf(F, "# Nodes: %d Edges: %d\n", Net->GetNodes(), Net->GetEdges()); fprintf(F, "# FromNodeId\tToNodeId\tSign\n"); for (TNodeEDatNet<TInt,TInt>::TEdgeI ei = Net->BegEI(); ei < Net->EndEI(); ei++) { fprintf(F, "%d\t%d\t%d\n", ei.GetSrcNId(), ei.GetDstNId(), ei()()); } fclose(F); PrintGraphStatTable(Net, OutFNm, Desc); }
PTable TTable::LoadSS(const TStr& TableName, const Schema& S, const TStr& InFNm, const char& Separator, TBool HasTitleLine){ TSsParser Ss(InFNm, Separator); PTable T = New(TableName, S); // if title line (i.e. names of the columns) is included as first row in the // input file - use it to validate schema if(HasTitleLine){ Ss.Next(); if(S.Len() != Ss.GetFlds()){TExcept::Throw("Table Schema Mismatch!");} for(TInt i = 0; i < Ss.GetFlds(); i++){ // remove carriage return char TInt L = strlen(Ss[i]); if(Ss[i][L-1] < ' '){ Ss[i][L-1] = 0;} if(T->GetSchemaColName(i) != Ss[i]){ TExcept::Throw("Table Schema Mismatch!");} } } TInt RowLen = S.Len(); TVec<TYPE> ColTypes = TVec<TYPE>(RowLen); for(TInt i = 0; i < RowLen; i++){ ColTypes[i] = T->GetSchemaColType(i); } // populate table columns while(Ss.Next()){ TInt IntColIdx = 0; TInt FltColIdx = 0; TInt StrColIdx = 0; Assert(Ss.GetFlds() == RowLen); // compiled only in debug for(TInt i = 0; i < RowLen; i++){ switch(ColTypes[i]){ case INT: T->IntCols[IntColIdx].Add(Ss.GetInt(i)); IntColIdx++; break; case FLT: T->FltCols[FltColIdx].Add(Ss.GetFlt(i)); FltColIdx++; break; case STR: T->AddStrVal(StrColIdx, Ss[i]); StrColIdx++; break; } } } // set number of rows and "Next" vector T->NumRows = Ss.GetLineNo()-1; if(HasTitleLine){T->NumRows--;} T->NumValidRows = T->NumRows; T->Next = TIntV(T->NumRows,0); for(TInt i = 0; i < T->NumRows-1; i++){ T->Next.Add(i+1); } T->Next.Add(Last); return T; }
void TEpidemModel::LoadTxt(const TStr& InFNm, const int& ColId, TFltV& ValV) { ValV.Clr(); if (! TFile::Exists(InFNm)) { printf("*** %s not found!\n", InFNm.CStr()); return; } TSsParser Ss(InFNm, ssfTabSep); while (Ss.Next()) { ValV.Add(Ss.GetFlt(ColId)); } }
TIntH LoadNodeList(TStr InFNmNodes) { TSsParser Ss(InFNmNodes, ssfWhiteSep, true, true, true); TIntIntH Nodes; int br = 0, NId; while (Ss.Next()) { if (Ss.GetInt(0, NId)) { Nodes.AddDat(br, NId); br++; } } return Nodes; }
// Slashdot network void MakeSlashdotNet(TStr InFNm, TStr OutFNm, TStr Desc) { TSsParser Ss(InFNm, ssfTabSep); PNGraph Graph = TNGraph::New(); TStrHash<TInt> StrSet(Mega(1), true); while (Ss.Next()) { const int SrcNId = StrSet.AddKey(Ss[0]); if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); } for (int dst = 2; dst < Ss.Len(); dst++) { const int DstNId = StrSet.AddKey(Ss[dst]); if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); } Graph->AddEdge(SrcNId, DstNId); } } PrintGraphStatTable(Graph, OutFNm, Desc); }
tensor TotalLagrangianFD8NodeBrick::getSurfaceForce(void) { int S_dim[] = {NumNodes, NumDof}; tensor Ss(2,S_dim,0.0); // Need Work Here! return Ss; }
int main(int argc, char* argv[]) { Env = TEnv(argc, argv, TNotify::StdNotify); Env.PrepArgs(TStr::Fmt("agmgen. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm())); TExeTm ExeTm; Try const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "DEMO", "Community affiliation data"); const TStr OutFPrx = Env.GetIfArgPrefixStr("-o:", "agm", "out file name prefix"); const int RndSeed = Env.GetIfArgPrefixInt("-rs:",10,"Rnd Seed"); const double DensityCoef= Env.GetIfArgPrefixFlt("-a:",0.6,"Power-law Coefficient a of density (density ~ N^(-a)"); const double ScaleCoef= Env.GetIfArgPrefixFlt("-c:",1.3,"Scaling Coefficient c of density (density ~ c"); TRnd Rnd(RndSeed); TVec<TIntV> CmtyVV; if(InFNm=="DEMO") { CmtyVV.Gen(2); TIntV NIdV; for(int i=0;i<25;i++) { TIntV& CmtyV = CmtyVV[0]; CmtyV.Add(i+1); } for(int i=15;i<40;i++) { TIntV& CmtyV = CmtyVV[1]; CmtyV.Add(i+1); } } else { TVec<TIntV> CmtyVV; TSsParser Ss(InFNm, ssfWhiteSep); while (Ss.Next()) { if(Ss.GetFlds()>0) { TIntV CmtyV; for(int i=0;i<Ss.GetFlds();i++) { if(Ss.IsInt(i)){CmtyV.Add(Ss.GetInt(i));} } CmtyVV.Add(CmtyV); } } printf("community loading completed (%d communities)\n",CmtyVV.Len()); } PUNGraph AG = TAGM::GenAGM(CmtyVV,DensityCoef,ScaleCoef,Rnd); TSnap::SaveEdgeList(AG,OutFPrx + ".edgelist.txt"); if(AG->GetNodes()<50) { TAGM::GVizComGraph(AG,CmtyVV,OutFPrx + ".graph.gif"); } Catch printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr()); return 0; }
/*文字列分割*/ std::vector<std::string> TextClass::StringSplit(const std::string &Str,char Sep){ //いろいろ宣言 std::vector<std::string> V; std::stringstream Ss(Str); std::string Buffer; const std::string EndOfText = "\0"; //文字列分割 while( std::getline(Ss,Buffer,Sep) ) V.push_back(Buffer); //終端文字 '\0' を挿入 V.push_back(EndOfText); return V; }
/// load bipartite community affiliation graph from text file (each row contains the member node IDs for each community) void TAGMUtil::LoadCmtyVV(const TStr& InFNm, TVec<TIntV>& CmtyVV) { CmtyVV.Gen(Kilo(100), 0); TSsParser Ss(InFNm, ssfWhiteSep); while (Ss.Next()) { if(Ss.GetFlds() > 0) { TIntV CmtyV; for (int i = 0; i < Ss.GetFlds(); i++) { if (Ss.IsInt(i)) { CmtyV.Add(Ss.GetInt(i)); } } CmtyVV.Add(CmtyV); } } CmtyVV.Pack(); printf("community loading completed (%d communities)\n",CmtyVV.Len()); }
// "W:\\Data\\CiteSeer\\old\\citeseer-links.csv" PWgtNet TWgtNet::LoadCiteSeerCoAuth(const TStr& FNm) { PWgtNet Net = TWgtNet::New(); TStrSet AuthorSet; TSsParser Ss(FNm, ssfCommaSep); while (Ss.Next()) { for (int a1 = 2; a1 < Ss.Len(); a1++) { const int n1 = AuthorSet.AddKey(Ss[a1]); for (int a2 = 2; a2 < Ss.Len(); a2++) { if (a1 == a2) { continue; } const int n2 = AuthorSet.AddKey(Ss[a2]); if (! Net->IsNode(n1)) { Net->AddNode(n1, Ss[a1]); } if (! Net->IsNode(n2)) { Net->AddNode(n2, Ss[a2]); } if (Net->IsEdge(n1, n2)) { Net->GetEDat(n1, n2) += 1; } else { Net->AddEdge(n1, n2, 1); } } } } TGBase::PrintInfo(Net); printf(" Edge weight: %f\n", Net->GetEdgeWgt()); return Net; }
int BuildCapacityNetwork(const TStr& InFNm, PNEANet &Net, const int& SrcColId = 0, const int& DstColId = 1, const int& CapColId = 2) { TSsParser Ss(InFNm, ssfWhiteSep, true, true, true); TRnd Random; Net.Clr(); Net = TNEANet::New(); int SrcNId, DstNId, CapVal, EId; int MaxCap = 0; while (Ss.Next()) { if (! Ss.GetInt(SrcColId, SrcNId) || ! Ss.GetInt(DstColId, DstNId)) { continue; } Ss.GetInt(CapColId, CapVal); //CapVal = Random.GetUniDevInt(1, 10000); MaxCap = max(CapVal, MaxCap); if (! Net->IsNode(SrcNId)) { Net->AddNode(SrcNId); } if (! Net->IsNode(DstNId)) { Net->AddNode(DstNId); } EId = Net->AddEdge(SrcNId, DstNId); Net->AddIntAttrDatE(EId, CapVal, TSnap::CapAttrName); } Net->Defrag(); return MaxCap; }
void MakeLJNets(TStr InFNm, TStr OutFNm, TStr Desc){ TStrHash<TInt> StrSet(Mega(1), true); for (int i = 1; i < 13; i++){ TStr tmp = ""; if (i < 10) tmp = InFNm + "ljgraph.0" + TInt::GetStr(i); else tmp = InFNm + "ljgraph." + TInt::GetStr(i); printf("%s\n",tmp()); TSsParser Ss(tmp, ssfTabSep); PNGraph Graph = TNGraph::New(); while (Ss.Next()) { const int SrcNId = StrSet.AddKey(Ss[0]); if (! Graph->IsNode(SrcNId)) { Graph->AddNode(SrcNId); } for (int dst = 2; dst < Ss.Len(); dst++) { TStr ls,rs; ((TStr)Ss[dst]).SplitOnCh(ls,' ',rs); if (ls == ">"){ const int DstNId = StrSet.AddKey(rs); if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); } Graph->AddEdge(SrcNId, DstNId); } } } if (i < 10) OutFNm = "soc-lj-friends.0"+TInt::GetStr(i); else OutFNm = "soc-lj-friends."+TInt::GetStr(i); PrintGraphStatTable(Graph, OutFNm, Desc); } }
void MakeLJGroupsNets(const TStr InFNm, TStr OutFNm, TStr Desc){ TStrHash<TInt> StrSetU(Mega(1), true); TStrHash<TInt> StrSetG(Mega(1), true); for (int i = 1; i < 13; i++){ TStr tmp = ""; if (i < 10) tmp = InFNm + "ljgraph.0" + TInt::GetStr(i); else tmp = InFNm + "ljgraph." + TInt::GetStr(i); printf("%s\n",tmp()); TSsParser Ss(tmp, ssfTabSep); PNGraph Graph = TNGraph::New(); while (Ss.Next()) { const int SrcNId = StrSetU.AddKey(Ss[0]); if (! Graph->IsNode(SrcNId)) Graph->AddNode(SrcNId); for (int dst = 2; dst < Ss.Len(); dst++) { TStr ls,rs; ((TStr)Ss[dst]).SplitOnCh(ls,' ',rs); if (ls == ">"){ const int DstNId = StrSetU.AddKey(rs); if (! Graph->IsNode(DstNId)) { Graph->AddNode(DstNId); } Graph->AddEdge(SrcNId, DstNId); } } } if (i < 10) OutFNm = "soc-lj-friends.0"+TInt::GetStr(i); else OutFNm = "soc-lj-friends."+TInt::GetStr(i); // PrintGraphStatTable(Graph, OutFNm, Desc); } for (int i = 5; i < 14; i++){ TStr tmp = ""; tmp = InFNm + "ljcomm." + TInt::GetStr(i); printf("%s\n",tmp()); TSsParser Ss1(tmp, ssfTabSep); PNGraph Graph1 = TNGraph::New(); PNGraph Graph2 = TNGraph::New(); TSsParser Ss(tmp, ssfTabSep); while (Ss.Next()) { const int SrcNId = StrSetU.AddKey(Ss[0]); if (! Graph1->IsNode(SrcNId)) { Graph1->AddNode(SrcNId); } if (! Graph2->IsNode(SrcNId)) { Graph2->AddNode(SrcNId); } for (int dst = 2; dst < Ss.Len(); dst++) { TStr ls,rs; ((TStr)Ss[dst]).SplitOnCh(ls,' ',rs); const int DstNId = StrSetG.AddKey(rs) + 10000000; if (! Graph1->IsNode(DstNId)) { Graph1->AddNode(DstNId); } if (! Graph2->IsNode(DstNId)) { Graph2->AddNode(DstNId); } if (ls == "<") // member Graph1->AddEdge(SrcNId, DstNId); else if (ls == ">") // watching Graph2->AddEdge(SrcNId, DstNId); } } OutFNm = "soc-lj-comm-"; TStr s = ""; if (i < 10) s = "0"; PrintGraphStatTable(Graph1, OutFNm+"members-" + s + TInt::GetStr(i), Desc+" communities - members"); PrintGraphStatTable(Graph2, OutFNm+"watchers-"+ s + TInt::GetStr(i), Desc+" communities - watchers"); } }
bool SparseMatrixTest(const size_t & size, const C_FLOAT64 & sparseness, const unsigned C_INT32 & seed, const bool & RMP, const bool & dgemmFlag, const bool & SMP, const bool & CCMP) { size_t i, j, l, loop = 1; CRandom * pRandom = CRandom::createGenerator(CRandom::mt19937, seed); // If the sparseness is not specified we expect 4 metabolites per reaction C_FLOAT64 Sparseness = sparseness; if (Sparseness == 0.0) Sparseness = 4.0 / size; CMatrix< C_FLOAT64 > M(size - 3, size); CSparseMatrix S(size - 3, size); CMatrix< C_FLOAT64 > MM(size, size + 3); CSparseMatrix Ss(size, size + 3); C_FLOAT64 tmp; for (i = 0; i < size - 3; i++) for (j = 0; j < size; j++) { if (pRandom->getRandomCC() < Sparseness) S(i, j) = (pRandom->getRandomCC() - 0.5) * 100.0; } for (i = 0; i < size; i++) for (j = 0; j < size + 3; j++) { if (pRandom->getRandomCC() < Sparseness) Ss(i, j) = (pRandom->getRandomCC() - 0.5) * 100.0; } M = S; MM = Ss; CCompressedColumnFormat C(S); CCompressedColumnFormat CC(Ss); std::cout << "Memory requirements for sparseness:\t" << Sparseness << std::endl; tmp = (C_FLOAT64) sizeof(CMatrix< C_FLOAT64 >) + size * size * sizeof(C_FLOAT64); std::cout << "Matrix(" << size << "x" << size << "):\t" << tmp << std::endl; C_FLOAT64 tmp2 = (C_FLOAT64) sizeof(CSparseMatrix) + 2 * size * sizeof(std::vector<CSparseMatrixElement *>) + 2 * size * sizeof(C_FLOAT64) + S.numNonZeros() * sizeof(CSparseMatrixElement); std::cout << "Sparse(" << size << "x" << size << "):\t" << tmp2 << std::endl; std::cout << "Sparse/Matrix:\t" << tmp2 / tmp << std::endl; tmp2 = (C_FLOAT64) sizeof(CCompressedColumnFormat) + 2 * C.numNonZeros() * sizeof(C_FLOAT64) + (size + 1) * sizeof(C_FLOAT64); std::cout << "CompressedColumnFormat(" << size << "x" << size << "):\t" << tmp2 << std::endl; std::cout << "CompressedColumnFormat/Matrix:\t" << tmp2 / tmp << std::endl << std::endl; CCopasiTimer CPU(CCopasiTimer::PROCESS); CCopasiTimer WALL(CCopasiTimer::WALL); if (RMP) { // Regular Matrix Product CPU.start(); WALL.start(); for (l = 0; l < loop; l++) { CMatrix< C_FLOAT64 > MR(M.numRows(), MM.numCols()); const C_FLOAT64 *pTmp1, *pTmp2, *pTmp4, *pTmp5; const C_FLOAT64 *pEnd1, *pEnd2, *pEnd4; C_FLOAT64 *pTmp3; size_t LDA = M.numCols(); size_t LDB = MM.numCols(); pTmp1 = M.array(); pEnd1 = pTmp1 + M.numRows() * LDA; pEnd2 = MM.array() + LDB; pTmp3 = MR.array(); for (; pTmp1 < pEnd1; pTmp1 += LDA) for (pTmp2 = MM.array(); pTmp2 < pEnd2; pTmp2++, pTmp3++) { *pTmp3 = 0.0; for (pTmp4 = pTmp1, pTmp5 = pTmp2, pEnd4 = pTmp4 + LDA; pTmp4 < pEnd4; pTmp4++, pTmp5 += LDB) * pTmp3 += *pTmp4 **pTmp5; } } CPU.calculateValue(); WALL.calculateValue(); std::cout << "Matrix * Matrix:\t"; CPU.print(&std::cout); std::cout << "\t"; WALL.print(&std::cout); std::cout << std::endl; } if (dgemmFlag) { CPU.start(); WALL.start(); for (l = 0; l < loop; l++) { CMatrix< C_FLOAT64 > dgemmR(M.numRows(), MM.numCols()); char T = 'N'; C_INT m = (C_INT) MM.numCols(); /* LDA, LDC */ C_INT n = (C_INT) M.numRows(); C_INT k = (C_INT) M.numCols(); /* LDB */ C_FLOAT64 Alpha = 1.0; C_FLOAT64 Beta = 0.0; dgemm_(&T, &T, &m, &n, &k, &Alpha, MM.array(), &m, M.array(), &k, &Beta, dgemmR.array(), &m); } /* for (i = 0; i < MR.numRows(); i++) for (j = 0; j < MR.numCols(); j++) assert(fabs(MR(i, j) - dgemmR(i, j)) <= 100.0 * std::numeric_limits< C_FLOAT64 >::epsilon() * fabs(MR(i, j))); */ CPU.calculateValue(); WALL.calculateValue(); std::cout << "dgemm(Matrix, Matrix):\t"; CPU.print(&std::cout); std::cout << "\t"; WALL.print(&std::cout); std::cout << std::endl; } // Sparse Matrix Product if (SMP) { CPU.start(); WALL.start(); for (l = 0; l < loop; l++) { CSparseMatrix SR(S.numRows(), Ss.numCols()); C_FLOAT64 Tmp; std::vector< std::vector< CSparseMatrixElement * > >::const_iterator itRow; std::vector< std::vector< CSparseMatrixElement * > >::const_iterator endRow; std::vector< CSparseMatrixElement * >::const_iterator itRowElement; std::vector< CSparseMatrixElement * >::const_iterator endRowElement; std::vector< std::vector< CSparseMatrixElement * > >::const_iterator itCol; std::vector< std::vector< CSparseMatrixElement * > >::const_iterator endCol; std::vector< CSparseMatrixElement * >::const_iterator itColElement; std::vector< CSparseMatrixElement * >::const_iterator endColElement; for (itRow = S.getRows().begin(), endRow = S.getRows().end(); itRow != endRow; ++itRow) { endRowElement = itRow->end(); for (itCol = Ss.getColumns().begin(), endCol = Ss.getColumns().end(); itCol != endCol; ++itCol) { Tmp = 0; itRowElement = itRow->begin(); itColElement = itCol->begin(); endColElement = itCol->end(); while (itRowElement != endRowElement && itColElement != endColElement) { while (itRowElement != endRowElement && (*itRowElement)->col() < (*itColElement)->row()) ++itRowElement; if (itRowElement == endRowElement) break; while (itColElement != endColElement && (*itColElement)->row() < (*itRowElement)->col()) ++itColElement; if (itColElement == endColElement) break; if ((*itRowElement)->col() != (*itColElement)->row()) continue; Tmp += **itRowElement ***itColElement; ++itRowElement; ++itColElement; } if (fabs(Tmp) < SR.getTreshold()) continue; SR.insert((*itRow->begin())->row(), (*itCol->begin())->col(), Tmp); } } } CPU.calculateValue(); WALL.calculateValue(); std::cout << "Sparse * Sparse:\t"; CPU.print(&std::cout); std::cout << "\t"; WALL.print(&std::cout); std::cout << std::endl; /* for (i = 0; i < MR.numRows(); i++) for (j = 0; j < MR.numCols(); j++) assert(fabs(MR(i, j) - SR(i, j)) < SR.getTreshold()); */ } // Compressed Column Format Product if (CCMP) { CPU.start(); WALL.start(); for (l = 0; l < loop; l++) { CSparseMatrix TmpR(C.numRows(), CC.numCols()); CCompressedColumnFormat CR(C.numRows(), CC.numCols(), 0); C_FLOAT64 Tmp; size_t imax = CR.numRows(); size_t jmax = CR.numCols(); C_FLOAT64 * pColElement, * pEndColElement; size_t * pColElementRow, * pEndColElementRow; size_t * pColStart; CCompressedColumnFormat::const_row_iterator itRowElement; CCompressedColumnFormat::const_row_iterator endRowElement = C.endRow(0); for (j = 0, pColStart = CC.getColumnStart(); j < jmax; j++, pColStart++) { for (i = 0; i < imax; i++) { Tmp = 0; itRowElement = C.beginRow(i); pColElement = CC.getValues() + *pColStart; pEndColElement = CC.getValues() + *(pColStart + 1); pColElementRow = CC.getRowIndex() + *pColStart; pEndColElementRow = CC.getRowIndex() + *(pColStart + 1); while (itRowElement != endRowElement && pColElement != pEndColElement) { while (itRowElement != endRowElement && itRowElement.getColumnIndex() < *pColElementRow) ++itRowElement; if (!(itRowElement != endRowElement)) break; while (pColElement != pEndColElement && *pColElementRow < itRowElement.getColumnIndex()) { ++pColElement; ++pColElementRow; } if (pColElement == pEndColElement) break; if (itRowElement.getColumnIndex() != *pColElementRow) continue; Tmp += *itRowElement **pColElement; ++itRowElement; ++pColElement; ++pColElementRow; } if (fabs(Tmp) < TmpR.getTreshold()) continue; TmpR.insert(i, j, Tmp); } } CR = TmpR; } CPU.calculateValue(); WALL.calculateValue(); std::cout << "Compressed * Compressed:\t"; CPU.print(&std::cout); std::cout << "\t"; WALL.print(&std::cout); std::cout << std::endl; /* for (i = 0; i < MR.numRows(); i++) for (j = 0; j < MR.numCols(); j++) assert(fabs(MR(i, j) - TmpR(i, j)) < SR.getTreshold()); */ } std::cout << std::endl; std::cout << std::endl; return true; }
// Parse files downloaded from IMDB. Actors point to movies. // Files: actors.list.gz, languages.list.gz, countries.list.gz PImdbNet TImdbNet::LoadFromImdb(const TStr& DataDir) { PImdbNet Net = TImdbNet::New(); Net->Reserve((int) Mega(2.5), -1); // ACTORS { TSsParser Ss(DataDir+"\\actors.list.gz", ssfTabSep); while (Ss.Next() && strcmp(Ss[0],"THE ACTORS LIST")!=0) { } Ss.Next(); Ss.Next(); Ss.Next(); int ActorId = -1, NAct=0; for (int i = 0; Ss.Next(); i++) { //printf("%s\n", Ss.DumpStr()); int mPos = 0; if (Ss.Len() > 1) { // actor ActorId = Net->AddStr(Ss[0]); if (Net->IsNode(ActorId)) { printf(" actor '%s'(%d) is already a node %s\n", Ss[0], ActorId, TImdbNet::GetMovieTyStr((TMovieTy) Net->GetNDat(ActorId).Type.Val).CStr()); continue; } else { Net->AddNode(ActorId); } TImdbNode& Node = Net->GetNDat(ActorId); Node.Name = ActorId; NAct++; Node.Type = mtyActor; mPos = 1; while (strlen(Ss[mPos])==0) { mPos++; } } // movie (delete everything last [) // also parse the position <>, but is a property of an edge (actor, movie) pair char *DelFrom; char *C1 = strrchr(Ss[mPos], '<'); char *C2 = strrchr(Ss[mPos], '['); if (C1==NULL) { DelFrom=C2; } else if (C2==NULL) { DelFrom=C1; } else { DelFrom = TMath::Mn(C1, C2); } if (DelFrom != NULL) { char *mov = DelFrom; while (*mov) { *mov=0; mov++; } mov = (char *) DelFrom-1; while (TCh::IsWs(*mov)) { *mov=0; mov--; } } const int MovNId = Net->AddStr(Ss[mPos]); if (! Net->IsNode(MovNId)) { Net->AddNode(MovNId); TImdbNode& Node = Net->GetNDat(MovNId); Node.Type = mtyMovie; Node.Year = GetYearFromTitle(Ss[mPos]); } if (Net->IsEdge(ActorId, MovNId)) { printf(" already an edge %d %d\n", ActorId, MovNId); } else { Net->AddEdge(ActorId, MovNId); } if ((i+1) % Kilo(10) == 0) { printf("\r %d", (i+1)/1000); if ((i+1) % Kilo(100) == 0) { printf(" nodes: %d, edges: %d, actors: %d", Net->GetNodes(), Net->GetEdges(), NAct); } } } printf("\n=== nodes: %d, edges: %d, actors: %d", Net->GetNodes(), Net->GetEdges(), NAct); } // MOVIE LANGUAGE */ { TSsParser Ss(DataDir+"\\language.list.gz", ssfTabSep); while (Ss.Next() && strcmp(Ss[0],"LANGUAGE LIST")!=0) { } Ss.Next(); int LangCnt=0, i; for (i = 0; Ss.Next(); i++) { char *Mov = Ss[0]; char *Lang = Ss[Ss.Len()-1]; if (Net->IsStr(Mov)) { const int NId = Net->GetStrId(Mov); Net->GetNDat(NId).Lang = Net->AddStr(Lang); LangCnt++; } //else { printf("movie not found: '%s'\n", Mov); } if ((i+1) % Kilo(10) == 0) { printf("\r %d found %d ", (i+1), LangCnt); } } printf("\n LANG: total movies: %d, found %d\n", (i+1), LangCnt); } // MOVIE COUNTRY { TSsParser Ss(DataDir+"\\countries.list.gz", ssfTabSep); while (Ss.Next() && strcmp(Ss[0],"COUNTRIES LIST")!=0) { } Ss.Next(); int LangCnt=0, i; for (i = 0; Ss.Next(); i++) { char *Mov = Ss[0]; char *Cntry = Ss[Ss.Len()-1]; if (Net->IsStr(Mov)) { const int NId = Net->GetStrId(Mov); Net->GetNDat(NId).Cntry = Net->AddStr(Cntry); LangCnt++; } //else { printf("country not found: '%s'\n", Mov); } if ((i+1) % Kilo(10) == 0) { printf("\n %d found %d ", (i+1), LangCnt); } } printf("\r CNTRY: total movies: %d, found %d\n", (i+1), LangCnt); } return Net; }
int main(int argc, char* argv[]) { Env = TEnv(argc, argv, TNotify::StdNotify); Env.PrepArgs(TStr::Fmt("cesna. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm())); TExeTm ExeTm; Try TStr OutFPrx = Env.GetIfArgPrefixStr("-o:", "", "Output Graph data prefix"); const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "./1912.edges", "Input edgelist file name"); const TStr LabelFNm = Env.GetIfArgPrefixStr("-l:", "", "Input file name for node names (Node ID, Node label) "); const TStr AttrFNm = Env.GetIfArgPrefixStr("-a:", "./1912.nodefeat", "Input node attribute file name"); const TStr ANameFNm = Env.GetIfArgPrefixStr("-n:", "./1912.nodefeatnames", "Input file name for node attribute names"); int OptComs = Env.GetIfArgPrefixInt("-c:", 10, "The number of communities to detect (-1: detect automatically)"); const int MinComs = Env.GetIfArgPrefixInt("-mc:", 3, "Minimum number of communities to try"); const int MaxComs = Env.GetIfArgPrefixInt("-xc:", 20, "Maximum number of communities to try"); const int DivComs = Env.GetIfArgPrefixInt("-nc:", 5, "How many trials for the number of communities"); const int NumThreads = Env.GetIfArgPrefixInt("-nt:", 4, "Number of threads for parallelization"); const double AttrWeight = Env.GetIfArgPrefixFlt("-aw:", 0.5, "We maximize (1 - aw) P(Network) + aw * P(Attributes)"); const double LassoWeight = Env.GetIfArgPrefixFlt("-lw:", 1.0, "Weight for l-1 regularization on learning the logistic model parameters"); const double StepAlpha = Env.GetIfArgPrefixFlt("-sa:", 0.05, "Alpha for backtracking line search"); const double StepBeta = Env.GetIfArgPrefixFlt("-sb:", 0.3, "Beta for backtracking line search"); const double MinFeatFrac = Env.GetIfArgPrefixFlt("-mf:", 0.0, "If the fraction of nodes with positive values for an attribute is smaller than this, we ignore that attribute"); #ifdef USE_OPENMP omp_set_num_threads(NumThreads); #endif PUNGraph G; TIntStrH NIDNameH; TStrHash<TInt> NodeNameH; TVec<TFltV> Wck; TVec<TIntV> EstCmtyVV; if (InFNm.IsStrIn(".ungraph")) { TFIn GFIn(InFNm); G = TUNGraph::Load(GFIn); } else { G = TAGMUtil::LoadEdgeListStr<PUNGraph>(InFNm, NodeNameH); NIDNameH.Gen(NodeNameH.Len()); for (int s = 0; s < NodeNameH.Len(); s++) { NIDNameH.AddDat(s, NodeNameH.GetKey(s)); } } if (LabelFNm.Len() > 0) { TSsParser Ss(LabelFNm, ssfTabSep); while (Ss.Next()) { if (Ss.Len() > 0) { NIDNameH.AddDat(Ss.GetInt(0), Ss.GetFld(1)); } } } printf("Graph: %d Nodes %d Edges\n", G->GetNodes(), G->GetEdges()); //load attribute TIntV NIDV; G->GetNIdV(NIDV); THash<TInt, TIntV> RawNIDAttrH, NIDAttrH; TIntStrH RawFeatNameH, FeatNameH; if (ANameFNm.Len() > 0) { TSsParser Ss(ANameFNm, ssfTabSep); while (Ss.Next()) { if (Ss.Len() > 0) { RawFeatNameH.AddDat(Ss.GetInt(0), Ss.GetFld(1)); } } } TCesnaUtil::LoadNIDAttrHFromNIDKH(NIDV, AttrFNm, RawNIDAttrH, NodeNameH); TCesnaUtil::FilterLowEntropy(RawNIDAttrH, NIDAttrH, RawFeatNameH, FeatNameH, MinFeatFrac); TExeTm RunTm; TCesna CS(G, NIDAttrH, 10, 10); if (OptComs == -1) { printf("finding number of communities\n"); OptComs = CS.FindComs(NumThreads, MaxComs, MinComs, DivComs, "", false, 0.1, StepAlpha, StepBeta); } CS.NeighborComInit(OptComs); CS.SetWeightAttr(AttrWeight); CS.SetLassoCoef(LassoWeight); if (NumThreads == 1 || G->GetEdges() < 1000) { CS.MLEGradAscent(0.0001, 1000 * G->GetNodes(), "", StepAlpha, StepBeta); } else { CS.MLEGradAscentParallel(0.0001, 1000, NumThreads, "", StepAlpha, StepBeta); } CS.GetCmtyVV(EstCmtyVV, Wck); TAGMUtil::DumpCmtyVV(OutFPrx + "cmtyvv.txt", EstCmtyVV, NIDNameH); FILE* F = fopen((OutFPrx + "weights.txt").CStr(), "wt"); if (FeatNameH.Len() == Wck[0].Len()) { fprintf(F, "#"); for (int k = 0; k < FeatNameH.Len(); k++) { fprintf(F, "%s", FeatNameH[k].CStr()); if (k < FeatNameH.Len() - 1) { fprintf(F, "\t"); } } fprintf(F, "\n"); } for (int c = 0; c < Wck.Len(); c++) { for (int k = 0; k < Wck[c].Len(); k++) { fprintf(F, "%f", Wck[c][k].Val); if (k < Wck[c].Len() - 1) { fprintf(F, "\t"); } } fprintf(F, "\n"); } fclose(F); Catch printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr()); return 0; }
int main(int argc, char* argv[]) { Env = TEnv(argc, argv, TNotify::StdNotify); Env.PrepArgs(TStr::Fmt("cpm. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm())); TExeTm ExeTm; Try TStr OutFPrx = Env.GetIfArgPrefixStr("-o:", "", "Output file name prefix"); const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "football.edgelist", "Input edgelist file name. DEMO: AGM with 2 communities"); const TStr LabelFNm = Env.GetIfArgPrefixStr("-l:", "football.labels", "Input file name for node names (Node ID, Node label) "); const TInt RndSeed = Env.GetIfArgPrefixInt("-s:", 0, "Random seed for AGM"); const TFlt Epsilon = Env.GetIfArgPrefixFlt("-e:", 0, "Edge probability between the nodes that do not share any community (default (0.0): set it to be 1 / N^2)"); const TInt Coms = Env.GetIfArgPrefixInt("-c:", 0, "Number of communities (0: determine it by AGM)"); PUNGraph G = TUNGraph::New(); TVec<TIntV> CmtyVV; TIntStrH NIDNameH; if (InFNm == "DEMO") { TVec<TIntV> TrueCmtyVV; TRnd AGMRnd(RndSeed); //generate community bipartite affiliation const int ABegin = 0, AEnd = 70, BBegin = 30, BEnd = 100; TrueCmtyVV.Add(TIntV()); TrueCmtyVV.Add(TIntV()); for (int u = ABegin; u < AEnd; u++) { TrueCmtyVV[0].Add(u); } for (int u = BBegin; u < BEnd; u++) { TrueCmtyVV[1].Add(u); } G = TAGM::GenAGM(TrueCmtyVV, 0.0, 0.2, AGMRnd); } else if (LabelFNm.Len() > 0) { G = TSnap::LoadEdgeList<PUNGraph>(InFNm); TSsParser Ss(LabelFNm, ssfTabSep); while (Ss.Next()) { if (Ss.Len() > 0) { NIDNameH.AddDat(Ss.GetInt(0), Ss.GetFld(1)); } } } else { G = TAGMUtil::LoadEdgeListStr<PUNGraph>(InFNm, NIDNameH); } printf("Graph: %d Nodes %d Edges\n", G->GetNodes(), G->GetEdges()); int MaxIter = 50 * G->GetNodes() * G->GetNodes(); if (MaxIter < 0) { MaxIter = TInt::Mx; } int NumComs = Coms; if (NumComs < 2) { int InitComs; if (G->GetNodes() > 1000) { InitComs = G->GetNodes() / 5; NumComs = TAGMUtil::FindComsByAGM(G, InitComs, MaxIter, RndSeed, 1.5, Epsilon, OutFPrx); } else { InitComs = G->GetNodes() / 5; NumComs = TAGMUtil::FindComsByAGM(G, InitComs, MaxIter, RndSeed, 1.2, Epsilon, OutFPrx); } } TAGMFit AGMFit(G, NumComs, RndSeed); if (Epsilon > 0) { AGMFit.SetPNoCom(Epsilon); } AGMFit.RunMCMC(MaxIter, 10); AGMFit.GetCmtyVV(CmtyVV, 0.9999); TAGMUtil::DumpCmtyVV(OutFPrx + "cmtyvv.txt", CmtyVV, NIDNameH); TAGMUtil::SaveGephi(OutFPrx + "graph.gexf", G, CmtyVV, 1.5, 1.5, NIDNameH); AGMFit.PrintSummary(); Catch printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr()); return 0; }