void CGibbsSamplingInfEngine::
Sampling( int statTime, int endTime )
{
  
  intVector ndsForSampling;
  GetNdsForSampling( &ndsForSampling );
  boolVector sampleIsNeed;
  GetSamplingNdsFlags( &sampleIsNeed );
  
  int numNdsForSampling = ndsForSampling.size();
  
  pEvidencesVector currentEvidences;
  GetCurrentEvidences( &currentEvidences );
  CEvidence * pCurrentEvidence;
  
  int t; 
  int i;
  for( t = statTime; t < endTime; t++ )
  {
    int series;
    for( series = 0; series < GetNumStreams(); series++ )
    {
      pCurrentEvidence = currentEvidences[series];
      
      
      for( i = 0; i < numNdsForSampling; i++ )
      {
        if( sampleIsNeed[i] )
        {
          pCurrentEvidence->ToggleNodeStateBySerialNumber(1, &i);
          bool canBeSample = ConvertingFamilyToPot( ndsForSampling[i], pCurrentEvidence );
          if(canBeSample)
          {
            
            GetPotToSampling(ndsForSampling[i])->GenerateSample( pCurrentEvidence, m_bMaximize );
          }
          else
          {
            pCurrentEvidence->ToggleNodeStateBySerialNumber(1, &i);
          }
        }
        
      }	
      
    }
    if( t > GetBurnIn())
    {
      pFactorVector queryFactors;
      GetQueryFactors( &queryFactors );
      int i;
      for( i = 0; i < queryFactors.size(); i++ )
      {
        queryFactors[i]->UpdateStatisticsML( &(GetCurrentEvidences()->front()), GetNumStreams() );
      }  
      
    }
  }
  
}
void CGibbsSamplingInfEngine::
EnterEvidence( const CEvidence *pEvidenceIn, int maximize, int sumOnMixtureNode )
{
  if( !m_queryes.size() )
  {
    PNL_THROW( CAlgorithmicException, "Possible queryes must be defined");
  }
  
  PNL_CHECK_IS_NULL_POINTER(pEvidenceIn);
  m_pEvidence = pEvidenceIn;
  m_bMaximize = maximize;
  
  DestroyCurrentEvidences();
  DestroyQueryFactors();
  
  
  if(GetModel()->GetModelType() == mtBNet)
  {
    static_cast< const CBNet* >(GetModel())->
      GenerateSamples( GetCurrentEvidences(), GetNumStreams(), pEvidenceIn );
  }
  else
  {
    static_cast< const CMNet* >(GetModel())->
      GenerateSamples( GetCurrentEvidences(), GetNumStreams(), pEvidenceIn );
    
  }
  
  CreateQueryFactors();
  
  boolVector sampleIsNeed;
  if( m_bUsingDSep )
  {
    ConsDSep( m_queryes, &sampleIsNeed, m_pEvidence );
  }
  else
  {
    FindCurrentNdsForSampling( &sampleIsNeed );
  }
  SetSamplingNdsFlags(sampleIsNeed);
  
  Sampling( 0, GetMaxTime() );
}
Exemplo n.º 3
0
uint32 PVAviFileHeader::GetNumAudioChannels(uint32 aStreamNo)
{
    uint32 strnum = GetNumStreams();
    uint32 retType = 0;
    uint32 ii = 0;

    for (ii = 0; ii < strnum; ii++)
    {
        if ((iStreamList[ii].GetStreamType() == PV_2_AUDIO) && (ii == aStreamNo))
        {
            retType = iStreamList[ii].GetNumAudioChannels();
        }
    }
    return retType;

}
Exemplo n.º 4
0
uint32 PVAviFileHeader::GetAudioFormat(uint32 aStreamNo)
{
    uint32 strnum = GetNumStreams();
    uint32 retType = WAVE_FORMAT_UNKNOWN;
    uint32 ii = 0;

    for (ii = 0; ii < strnum; ii++)
    {
        if ((iStreamList[ii].GetStreamType() == PV_2_AUDIO) && (aStreamNo == ii))
        {
            retType = iStreamList[ii].GetAudioFormat();
        }
    }

    return retType;
}
void CGibbsSamplingInfEngine::
MarginalNodes( const int *queryIn, int querySz, int notExpandJPD )
{
  delete m_pQueryJPD;
  m_pQueryJPD = NULL;
  
  delete m_pPotMPE;
  m_pPotMPE = NULL;
  
  delete m_pEvidenceMPE;
  m_pEvidenceMPE = NULL;
  
  const CFactor *pFactor;
  CPotential *pPot =  NULL;
  int *begin1;
  int *end1;
  int *begin2;
  int *end2;
  
  intVector domainVec;
  intVector queryVec;
  intVector obsQueryVec;
  queryVec.reserve(querySz);
  obsQueryVec.reserve(querySz);
  int i;
  for( i = 0; i < querySz; i++ )
  {
    m_pEvidence->IsNodeObserved(queryIn[i]) ? 
      obsQueryVec.push_back(queryIn[i]):
    queryVec.push_back(queryIn[i]);
  }
  
  
  CPotential *tmpPot = NULL;
  
  if( queryVec.size() )
  {
    for( i = 0; i < m_queryFactors.size(); i++)     
    {
      
      domainVec.clear();
      pFactor = m_queryFactors[i];
      pFactor->GetDomain(&domainVec);
      begin1 = &domainVec.front();
      end1 = &domainVec.back() + 1;
      std::sort(begin1, end1);
      
      begin2 = &queryVec.front();
      end2 = &queryVec.back() + 1;
      std::sort(begin2, end2);
      
      if( std::includes(begin1, end1, begin2, end2) )
      {
        pPot = pFactor->ConvertStatisticToPot( (GetMaxTime()-GetBurnIn()-1)*GetNumStreams() );
        tmpPot = pPot->Marginalize( queryVec );
        delete pPot;
        break;
      }
      		   
    }
    if( !tmpPot )
    {
      PNL_THROW(CInvalidOperation, "Invalid query");
    }
  }
  delete m_pQueryJPD; 
  
  if( obsQueryVec.size() )
  {
    
    EDistributionType paramDistrType = 
      pnlDetermineDistributionType( GetModel()->GetModelDomain(), querySz, queryIn, m_pEvidence);
    
    
    CPotential *pQueryPot;
    switch( paramDistrType )
    {
    case dtTabular:
      {
        pQueryPot = CTabularPotential::CreateUnitFunctionDistribution(
          queryIn, querySz, m_pGraphicalModel->GetModelDomain() );
        break;
      }
      
    case dtGaussian:
      {
        pQueryPot = CGaussianPotential::CreateUnitFunctionDistribution(
          queryIn, querySz, m_pGraphicalModel->GetModelDomain()  );
        break;
      }
    case dtScalar:
      {
        pQueryPot = CScalarPotential::Create(
          queryIn, querySz, m_pGraphicalModel->GetModelDomain()  );
        break;
      }
    case dtCondGaussian:
      {
        PNL_THROW( CNotImplemented, "conditional gaussian factors" )
          break;
      }
    default:
      {
        PNL_THROW( CInconsistentType, "distribution type" )
      }
    }
    
    if( tmpPot)
    {
      (*pQueryPot) *= (*tmpPot);
      delete tmpPot;
    }
    
    if( m_bMaximize )
    {
      m_pPotMPE   = static_cast<CPotential*>
        ( pQueryPot->ExpandObservedNodes( m_pEvidence, 0) );
      
      m_pEvidenceMPE = m_pPotMPE->GetMPE();
    }
    else
    {
      m_pQueryJPD = static_cast<CPotential*>( pQueryPot->ExpandObservedNodes( m_pEvidence, 0) );
    }
    
    delete pQueryPot;
  }