/** * Called when the release event happened on an association. * If the button pressed isn't left button, the association being created is * cleaned. If it is left button, and the first widget is set and is a * classifier widget, it creates an association class. Otherwise, the * association being created is cleaned. */ void ToolBarStateAssociation::mouseReleaseAssociation() { if (m_pMouseEvent->button() != Qt::LeftButton || !m_firstWidget || m_firstWidget->baseType() != WidgetBase::wt_Class) { cleanAssociation(); return; } currentAssociation()->createAssocClassLine( static_cast<ClassifierWidget*>(m_firstWidget), currentAssociation()->associationLine()->closestSegmentIndex(m_pMouseEvent->scenePos())); m_firstWidget->addAssoc(currentAssociation()); cleanAssociation(); }
void CBICLearningEngine::Learn() { CEMLearningEngine *pLearn = NULL; float resultBIC = -FLT_MAX; CBNet *pResultBNet = NULL; intVector resultOrder; pEvidencesVector pEv(m_Vector_pEvidences.size(), NULL ); CModelDomain *pMD = m_pGrModel->GetModelDomain(); int nnodes = m_pGrModel->GetNumberOfNodes(); nodeTypeVector varTypes; pMD->GetVariableTypes(&varTypes); intVector varAss( pMD->GetVariableAssociations(), pMD->GetVariableAssociations() + nnodes ); intVector currentAssociation(nnodes); intVector currentObsNodes(nnodes); int i; for( i = 0; i < nnodes; i++ ) { currentObsNodes[i] = i; } CGraph *pGraph = CGraph::Create(nnodes, NULL, NULL, NULL); CBNet *pBNet; int lineSz = int( nnodes * ( nnodes - 1 ) / 2 ); intVecVector connect; intVector indexes(lineSz, 0); int startNode, endNode; int ind; for( ind = 0; ind < lineSz ; ) { if( indexes[ind] == 1 ) { FindNodesByNumber(&startNode, &endNode, nnodes, ind); pGraph->RemoveEdge(startNode, endNode ); indexes[ind] = 0; ind++; } else { FindNodesByNumber(&startNode, &endNode, nnodes, ind); pGraph->AddEdge(startNode, endNode, 1 ); indexes[ind] = 1; ind = 0; connect.clear(); pGraph->GetConnectivityComponents(&connect); if( connect.size() == 1 ) { do { CGraph *pCopyGraph = CGraph::Copy(pGraph); int j; for( j = 0; j < nnodes; j++ ) { currentAssociation[j] = varAss[currentObsNodes[j]]; } pBNet = CBNet::Create(nnodes, varTypes, currentAssociation, pCopyGraph); pBNet->AllocFactors(); for( j = 0; j < nnodes; j++ ) { pBNet->AllocFactor( j ); pBNet->GetFactor(j)->CreateAllNecessaryMatrices(); } int dimOfModel = DimOfModel(pBNet); int k; for( k = 0; k < pEv.size(); k++ ) { valueVector vls; m_Vector_pEvidences[k]->GetRawData(&vls); pEv[k] = CEvidence::Create( pBNet->GetModelDomain(),currentObsNodes, vls ); } pLearn = CEMLearningEngine::Create(pBNet); pLearn->SetData(pEv.size(), &pEv.front()); pLearn->Learn(); int nsteps; const float *score; pLearn->GetCriterionValue(&nsteps, &score); float log_lik = score[nsteps-1]; float BIC = log_lik - 0.5f*float( dimOfModel*log(float(pEv.size())) ); if( BIC >= resultBIC ) { delete pResultBNet; resultBIC = BIC; m_critValue.push_back(BIC); pResultBNet = pBNet; resultOrder.assign( currentObsNodes.begin(), currentObsNodes.end() ); } else { delete pBNet; } for( k = 0; k < pEv.size(); k++ ) { delete pEv[k]; } delete pLearn; }while(std::next_permutation(currentObsNodes.begin(), currentObsNodes.end())); } } } delete pGraph; m_pResultGrModel = pResultBNet; m_resultRenaming.assign(resultOrder.begin(), resultOrder.end()); }