// delete all edges between the two nodes void TNEGraph::DelEdge(const int& SrcNId, const int& DstNId, const bool& Dir) { int EId; IAssert(IsEdge(SrcNId, DstNId, EId, Dir)); // there is at least one edge while (IsEdge(SrcNId, DstNId, EId, Dir)) { GetNode(SrcNId).OutEIdV.DelIfIn(EId); GetNode(DstNId).InEIdV.DelIfIn(EId); } EdgeH.DelKey(EId); }
// delete all edges between the two nodes void TNEANet::DelEdge(const int& SrcNId, const int& DstNId, const bool& IsDir) { int EId = 0; bool Edge = IsEdge(SrcNId, DstNId, EId, IsDir); IAssert(Edge); // there is at least one edge while (Edge) { DelEdge(EId); Edge = IsEdge(SrcNId, DstNId, EId, IsDir); } }
bool TNEGraph::IsOk(const bool& ThrowExcept) const { bool RetVal = true; for (int N = NodeH.FFirstKeyId(); NodeH.FNextKeyId(N); ) { const TNode& Node = NodeH[N]; if (! Node.OutEIdV.IsSorted()) { const TStr Msg = TStr::Fmt("Out-edge list of node %d is not sorted.", Node.GetId()); if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false; } if (! Node.InEIdV.IsSorted()) { const TStr Msg = TStr::Fmt("In-edge list of node %d is not sorted.", Node.GetId()); if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false; } // check out-edge ids int prevEId = -1; for (int e = 0; e < Node.GetOutDeg(); e++) { if (! IsEdge(Node.GetOutEId(e))) { const TStr Msg = TStr::Fmt("Out-edge id %d of node %d does not exist.", Node.GetOutEId(e), Node.GetId()); if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false; } if (e > 0 && prevEId == Node.GetOutEId(e)) { const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge id %d.", Node.GetId(), Node.GetOutEId(e)); if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false; } prevEId = Node.GetOutEId(e); } // check in-edge ids prevEId = -1; for (int e = 0; e < Node.GetInDeg(); e++) { if (! IsEdge(Node.GetInEId(e))) { const TStr Msg = TStr::Fmt("Out-edge id %d of node %d does not exist.", Node.GetInEId(e), Node.GetId()); if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false; } if (e > 0 && prevEId == Node.GetInEId(e)) { const TStr Msg = TStr::Fmt("Node %d has duplidate out-edge id %d.", Node.GetId(), Node.GetInEId(e)); if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false; } prevEId = Node.GetInEId(e); } } for (int E = EdgeH.FFirstKeyId(); EdgeH.FNextKeyId(E); ) { const TEdge& Edge = EdgeH[E]; if (! IsNode(Edge.GetSrcNId())) { const TStr Msg = TStr::Fmt("Edge %d source node %d does not exist.", Edge.GetId(), Edge.GetSrcNId()); if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false; } if (! IsNode(Edge.GetDstNId())) { const TStr Msg = TStr::Fmt("Edge %d destination node %d does not exist.", Edge.GetId(), Edge.GetDstNId()); if (ThrowExcept) { EAssertR(false, Msg); } else { ErrNotify(Msg.CStr()); } RetVal=false; } } return RetVal; }
// IN-OUT edges are swapped (so that the prog runs faster) // Send message via IN edge proportional to the OUT edge weight void TWgtNet::ReinforceEdges(const int& NIters) { THash<TInt, TFlt> OutWgtSumH; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { double wgt = 0; for (int e = 0; e < NI.GetOutDeg(); e++) { wgt += NI.GetOutEDat(e); } OutWgtSumH.AddDat(NI.GetId(), wgt); } printf("Reinforcing edges for %d iterations\n", NIters); // iterate TExeTm ExeTm; for (int iter = 0; iter < NIters; iter++) { for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { const double X = TInt::Rnd.GetUniDev() * OutWgtSumH.GetDat(NI.GetId()); double x = 0; int e = 0; for ( ; x + NI.GetOutEDat(e) < X; e++) { x += NI.GetOutEDat(e); } IAssert(IsEdge(NI.GetOutNId(e), NI.GetId())); GetEDat(NI.GetOutNId(e), NI.GetId()) += 1; // reinforce the edge OutWgtSumH.GetDat(NI.GetOutNId(e)) += 1; } if (iter % (NIters/100) == 0) { printf("\r%d [%s]", iter, ExeTm.GetStr()); } } printf(" done.\n"); }
static void FindEdges(const HitData &Hit, const GLIX &HitGlix, const IIX &IntervalIndex) { int *QueryMatches; int *TargetMatches; const int QueryMatchCount = IntervalIndex.LookupGlobal(Hit.QueryFrom, Hit.QueryTo, &QueryMatches); const int TargetMatchCount = IntervalIndex.LookupGlobal(Hit.TargetFrom, Hit.TargetTo, &TargetMatches); const int MatchCount = QueryMatchCount + TargetMatchCount; int *Matches = all(int, MatchCount); memcpy(Matches, QueryMatches, QueryMatchCount*sizeof(int)); memcpy(Matches + QueryMatchCount, TargetMatches, TargetMatchCount*sizeof(int)); for (int i = 0; i < MatchCount; ++i) { const int m = Matches[i]; if (m < 0 || m > CandCount) Quit("m=%d count=%d", m, CandCount); } for (int i = 0; i < MatchCount; ++i) { const int CandIndex1 = Matches[i]; for (int j = 0; j < i; ++j) { const int CandIndex2 = Matches[j]; if (IsEdge(HitGlix, Hit, CandIndex1, CandIndex2)) { EdgeData Edge; Edge.Node1 = CandIndex1; Edge.Node2 = CandIndex2; Edge.Rev = Hit.Rev; Edges.push_back(Edge); } } } }
void TNEGraph::DelEdge(const int& EId) { IAssert(IsEdge(EId)); const int SrcNId = GetEdge(EId).GetSrcNId(); const int DstNId = GetEdge(EId).GetDstNId(); GetNode(SrcNId).OutEIdV.DelIfIn(EId); GetNode(DstNId).InEIdV.DelIfIn(EId); EdgeH.DelKey(EId); }
int TNGraph::AddEdge(const int& SrcNId, const int& DstNId) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); //IAssert(! IsEdge(SrcNId, DstNId)); if (IsEdge(SrcNId, DstNId)) { return -2; } GetNode(SrcNId).OutNIdV.AddSorted(DstNId); GetNode(DstNId).InNIdV.AddSorted(SrcNId); return -1; // edge id }
// Add an edge between SrcNId and DstNId to the graph. int TUNGraph::AddEdge(const int& SrcNId, const int& DstNId) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); if (IsEdge(SrcNId, DstNId)) { return -2; } // edge already exists GetNode(SrcNId).NIdV.AddSorted(DstNId); if (SrcNId!=DstNId) { // not a self edge GetNode(DstNId).NIdV.AddSorted(SrcNId); } return -1; // edge id }
Context* RelationGraph::GetContext(int s, int t) { if (IsEdge(s, t)) { for (int i = 0; i < contexts.size(); i++) { pair<int, int> dIds = contexts[i]->GetDomainIds(); if ((dIds.first == s && dIds.second == t) || (dIds.first == t && dIds.second == s)) return contexts[i]; } } else return NULL; }
int TNEGraph::AddEdge(const int& SrcNId, const int& DstNId, int EId) { if (EId == -1) { EId = MxEId; MxEId++; } else { MxEId = TMath::Mx(EId+1, MxEId()); } IAssert(! IsEdge(EId)); IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); EdgeH.AddDat(EId, TEdge(EId, SrcNId, DstNId)); GetNode(SrcNId).OutEIdV.AddSorted(EId); GetNode(DstNId).InEIdV.AddSorted(EId); return EId; }
void TMultimodalGraphImplB::DelEdge(const TPair<TInt,TInt>& SrcNId, const TPair<TInt,TInt>& DstNId) { IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId.GetVal2(), DstNId.GetVal2()).CStr()); if (!IsEdge(SrcNId, DstNId)) { return; // Edge doesn't exist } NEdges--; TPair<TInt,TInt> ModeIdsKey = GetModeIdsKey(SrcNId.GetVal1(), DstNId.GetVal1()); Graphs.GetDat(ModeIdsKey).DelEdge(SrcNId.GetVal2(), DstNId.GetVal2()); }
int TNEANetMP::AddEdge(const int& SrcNId, const int& DstNId, int EId) { int i; if (EId == -1) { EId = MxEId; MxEId++; } else { MxEId = TMath::Mx(EId+1, MxEId()); } IAssertR(!IsEdge(EId), TStr::Fmt("EdgeId %d already exists", EId)); IAssertR(IsNode(SrcNId) && IsNode(DstNId), TStr::Fmt("%d or %d not a node.", SrcNId, DstNId).CStr()); EdgeH.AddDat(EId, TEdge(EId, SrcNId, DstNId)); GetNode(SrcNId).OutEIdV.AddSorted(EId); GetNode(DstNId).InEIdV.AddSorted(EId); // update attribute columns for (i = 0; i < VecOfIntVecsE.Len(); i++) { TVec<TInt>& IntVec = VecOfIntVecsE[i]; IntVec.Ins(EdgeH.GetKeyId(EId), TInt::Mn); } TVec<TStr> DefIntVec = TVec<TStr>(); IntDefaultsE.GetKeyV(DefIntVec); for (i = 0; i < DefIntVec.Len(); i++) { TStr attr = DefIntVec[i]; TVec<TInt>& IntVec = VecOfIntVecsE[KeyToIndexTypeE.GetDat(DefIntVec[i]).Val2]; IntVec[EdgeH.GetKeyId(EId)] = GetIntAttrDefaultE(attr); } for (i = 0; i < VecOfStrVecsE.Len(); i++) { TVec<TStr>& StrVec = VecOfStrVecsE[i]; StrVec.Ins(EdgeH.GetKeyId(EId), TStr::GetNullStr()); } TVec<TStr> DefStrVec = TVec<TStr>(); IntDefaultsE.GetKeyV(DefStrVec); for (i = 0; i < DefStrVec.Len(); i++) { TStr attr = DefStrVec[i]; TVec<TStr>& StrVec = VecOfStrVecsE[KeyToIndexTypeE.GetDat(DefStrVec[i]).Val2]; StrVec[EdgeH.GetKeyId(EId)] = GetStrAttrDefaultE(attr); } for (i = 0; i < VecOfFltVecsE.Len(); i++) { TVec<TFlt>& FltVec = VecOfFltVecsE[i]; FltVec.Ins(EdgeH.GetKeyId(EId), TFlt::Mn); } TVec<TStr> DefFltVec = TVec<TStr>(); FltDefaultsE.GetKeyV(DefFltVec); for (i = 0; i < DefFltVec.Len(); i++) { TStr attr = DefFltVec[i]; TVec<TFlt>& FltVec = VecOfFltVecsE[KeyToIndexTypeE.GetDat(DefFltVec[i]).Val2]; FltVec[NodeH.GetKeyId(EId)] = GetFltAttrDefaultE(attr); } return EId; }
// Wgt == -1 : take the weight of the edge in the opposite direction void TWgtNet::AddBiDirEdges(const double& Wgt) { TIntPrV EdgeV; for (TNodeI NI = BegNI(); NI < EndNI(); NI++) { for (int e = 0; e < NI.GetOutDeg(); e++) { if (! IsEdge(NI.GetOutNId(e), NI.GetId())) { EdgeV.Add(TIntPr(NI.GetOutNId(e), NI.GetId())); } } } for (int e = 0; e < EdgeV.Len(); e++) { if (Wgt != -1) { AddEdge(EdgeV[e].Val1, EdgeV[e].Val2, Wgt); } else { // edge weight in the opposite direction AddEdge(EdgeV[e].Val1, EdgeV[e].Val2, GetEDat(EdgeV[e].Val2, EdgeV[e].Val1)); } } }
int TNEANet::AddStrAttrDatE(const int& EId, const TStr& value, const TStr& attr) { int i; TInt CurrLen; if (!IsEdge(EId)) { //AddEdge(EId); return -1; } if (KeyToIndexTypeE.IsKey(attr)) { TVec<TStr>& NewVec = VecOfStrVecsE[KeyToIndexTypeE.GetDat(attr).Val2]; NewVec[EdgeH.GetKeyId(EId)] = value; } else { CurrLen = VecOfStrVecsE.Len(); KeyToIndexTypeE.AddDat(attr, TIntPr(StrType, CurrLen)); TVec<TStr> NewVec = TVec<TStr>(); for (i = 0; i < MxEId; i++) { NewVec.Ins(i, GetStrAttrDefaultE(attr)); } NewVec[EdgeH.GetKeyId(EId)] = value; VecOfStrVecsE.Add(NewVec); } return 0; }
void TNEANet::DelEdge(const int& EId) { int i; IAssert(IsEdge(EId)); const int SrcNId = GetEdge(EId).GetSrcNId(); const int DstNId = GetEdge(EId).GetDstNId(); GetNode(SrcNId).OutEIdV.DelIfIn(EId); GetNode(DstNId).InEIdV.DelIfIn(EId); EdgeH.DelKey(EId); for (i = 0; i < VecOfIntVecsE.Len(); i++) { TVec<TInt>& IntVec = VecOfIntVecsE[i]; IntVec.Ins(EdgeH.GetKeyId(EId), TInt::Mn); } for (i = 0; i < VecOfStrVecsE.Len(); i++) { TVec<TStr>& StrVec = VecOfStrVecsE[i]; StrVec.Ins(EdgeH.GetKeyId(EId), TStr::GetNullStr()); } for (i = 0; i < VecOfFltVecsE.Len(); i++) { TVec<TFlt>& FltVec = VecOfFltVecsE[i]; FltVec.Ins(EdgeH.GetKeyId(EId), TFlt::Mn); } }
//分析两只 void CAndroidAI::AnalyseTwo() { BYTE byIndex; for( BYTE i = 0; i < m_byRemainThreeCount; i++ ) { if( !m_bSelect[i] ) { m_bSelect[i] = true; //搜索两只相同 if( SearchSameCardRemain(m_byRemainThree[i],byIndex,i+1) ) { //临时记录 m_byTwoCard[m_byTwoCount*2] = m_byRemainThree[i]; m_byTwoCard[m_byTwoCount*2+1] = m_byRemainThree[byIndex]; //判断将 m_byTwoCount++; int nGoodSame = 90; if( !m_bHaveJiang ) { m_bHaveJiang = true; nGoodSame = 120; } //递归 m_nScoreTwo += nGoodSame; m_bSelect[byIndex] = true; AnalyseTwo(); m_bSelect[byIndex] = false; if( 120 == nGoodSame ) m_bHaveJiang = false; m_nScoreTwo -= nGoodSame; m_byTwoCount--; } //搜索紧连牌 if( SearchLinkCardRemain(m_byRemainThree[i],0,byIndex,i+1) ) { //临时记录 m_byTwoCard[m_byTwoCount*2] = m_byRemainThree[i]; m_byTwoCard[m_byTwoCount*2+1] = m_byRemainThree[byIndex]; //判断边 m_byTwoCount++; int nGoodLink; if( IsEdge(m_byRemainThree[i],m_byRemainThree[byIndex]) ) nGoodLink = 80; else nGoodLink = 100; //递归 m_nScoreTwo += nGoodLink; m_bSelect[byIndex] = true; AnalyseTwo(); m_bSelect[byIndex] = false; m_nScoreTwo -= nGoodLink; m_byTwoCount--; } //搜索有卡的连牌 if( SearchLinkCardRemain(m_byRemainThree[i],1,byIndex,i+1) ) { //临时记录 m_byTwoCard[m_byTwoCount*2] = m_byRemainThree[i]; m_byTwoCard[m_byTwoCount*2+1] = m_byRemainThree[byIndex]; //判断边 m_byTwoCount++; int nGoodLink; if( IsEdge(m_byRemainThree[i],m_byRemainThree[byIndex]) ) nGoodLink = 70; else nGoodLink = 90; //递归 m_nScoreTwo += nGoodLink; m_bSelect[byIndex] = true; AnalyseTwo(); m_bSelect[byIndex] = false; m_nScoreTwo -= nGoodLink; m_byTwoCount--; } m_bSelect[i] = false; } } //如果有分数更高的 if( m_nScoreTwo > m_nMaxScoreTwo ) { //记录剩下的 m_nMaxScoreTwo = m_nScoreTwo; m_byRemainTwoCount = 0; for( i = 0; i < m_byRemainThreeCount; i++ ) { if( !m_bSelect[i] ) m_byRemainTwo[m_byRemainTwoCount++] = m_byRemainThree[i]; } //记录最佳两只组合 m_byGoodTwoCount = m_byTwoCount; CopyMemory(m_byGoodTwoCard,m_byTwoCard,sizeof(m_byTwoCard)); } }