void CMlDynamicStructLearn::Learn()
{
    RearrangeEvidences();
    int nNodesSlice = m_pGrModel->GetNumberOfNodes();
    intVector vAncestor, vDescent;
    for(int i=0; i<nNodesSlice; i++)
    {
	vAncestor.push_back(i);
	vDescent.push_back(i+nNodesSlice);
    }
    //currently only the hill climbing algorithm available
    CMlStaticStructLearn* pSSL = CMlStaticStructLearnHC::Create(m_pGrModel->GetStaticModel(), itStructLearnML,
	StructLearnHC, BIC, m_nMaxFanIn,
	vAncestor, vDescent, m_nRestarts);
    pSSL->SetData(m_vEvidences.size(), &m_vEvidences.front());
    pSSL->SetMaxIterIPF(m_nMaxIters);
    static_cast<CMlStaticStructLearnHC*>(pSSL) ->SetMinProgress(m_minProgress);
    pSSL->Learn();
    const CDAG* p2SDAG = pSSL->GetResultDAG();
    //	p2SDAG->Dump();
    if(!m_LearnPriorSlice)
    {
	m_pResultDag = const_cast<CDAG*>(p2SDAG)->Clone();
	delete pSSL;
	return;
    }
    intVector vA, vD;
    CStaticGraphicalModel* pGrModel0 = m_pGrModel->CreatePriorSliceGrModel();
    CMlStaticStructLearn* pSSL0 = CMlStaticStructLearnHC::Create(pGrModel0, itStructLearnML,
	StructLearnHC, BIC, m_nMaxFanIn,
	vA, vD, m_nRestarts);
    pSSL0->SetData(m_vEvidence0.size(), &m_vEvidence0.front());
    pSSL0->SetMaxIterIPF(m_nMaxIters / 2);
    static_cast<CMlStaticStructLearnHC*>(pSSL0) ->SetMinProgress(m_minProgress);

    pSSL0->Learn();
    const CDAG* p0SDAG = pSSL0->GetResultDAG();
    //	p0SDAG->Dump();
    CDAG* pDAG = const_cast<CDAG*>(p2SDAG)->Clone();
    if(pDAG->SetSubDag(vAncestor, const_cast<CDAG*>(p0SDAG)))
    {
	m_pResultDag = pDAG->Clone();
	delete pDAG;
    }
    else
	PNL_THROW(CInternalError, "InternalError, can not generate a DAG");

    delete pSSL;
    delete pSSL0;
}
Esempio n. 2
0
int main()
{
    int i, j;
    CBNet* pAlarm = CreateAlarmBNet();
    const int nnodes = pAlarm->GetNumberOfNodes();
    pAlarm->GetGraph()->Dump();
    
    CGraph *graph = CGraph::Create( nnodes, NULL, NULL, NULL );	
    for(i=0; i<nnodes-1; i++)
    {
	graph->AddEdge(i, i+1, 1);
    }
    
    
    CBNet *bnet = CBNet::CreateWithRandomMatrices( graph, pAlarm->GetModelDomain() );
    
      
    int nEv = 50;
  
    
    CEvidence **pEvidences = new CEvidence *[nEv];
    int dataSize = nnodes;//summ all sizes
    
    int* obs_nodes = new int[nnodes];
    for(i=0; i<nnodes; i++)obs_nodes[i] = i;
    
    //read data from file alarm.dat to evidences
    FILE * fp = fopen("../../examples/Data/alarm.dat", "r");
    
    if( !fp && !(fp = fopen("../../c_pgmtk/examples/Data/alarm.dat", "r")) )
    {
        std::cout<<"can't open cases file"<<std::endl;
        exit(1);
    }
    
    valueVector input_data;
    input_data.resize(dataSize);
    
    for(i = 0 ; i < nEv; i++)
    {
        for (j = 0; j < dataSize ; j++)
        {
            int val;
            fscanf(fp, "%d,", &val);
            input_data[j].SetInt(val);
        }
        pEvidences[i] = CEvidence::Create(pAlarm, nnodes, obs_nodes, input_data);
    }
    
    fclose(fp);
    intVector vA;
    intVector vD;
    CMlStaticStructLearn *pLearn = CMlStaticStructLearnHC::Create(bnet, itStructLearnML, 
	StructLearnHC, BIC, 5, vA, vD, 1);
    
    pLearn->SetData(nEv, pEvidences);
    pLearn->Learn();
    //	pLearn->CreateResultBNet(const_cast<CDAG*>(pLearn->GetResultDAG()));
    //	const CBNet* pBNet = pLearn->GetResultBNet();
    ///////////////////////////////////////////////////////////////////////////////
    const CDAG* pDAG = pLearn->GetResultDAG();
    CDAG* iDAG = CDAG::Create(*(pAlarm->GetGraph()));
    int diff = iDAG->SymmetricDifference(pDAG);
    pDAG->Dump();
    delete pLearn;
    delete iDAG;
    delete[] obs_nodes;
    delete pAlarm;
    delete bnet;
   
    for( i = 0; i < nEv; i++)
    {
	delete pEvidences[i];
    }
    return 1;
}