/*! Constructor.
 
 Translate the gene starting at position i of genome g into a deterministic HMM node.
 */
fn::hmm::detail::deterministic_node::deterministic_node(const genome& g, int start, int* where)
: _ndr(0) {
	// are we building in previously allocated memory?
	if(where) {
		_ndr = reinterpret_cast<table_header*>(where);
	} else {
		_data.reset(new char[nodesize(g,start)]);
		_ndr = reinterpret_cast<table_header*>(_data.get());
	}
	
	// now fill in the memory:
	_ndr->start_codon[0] = g[start+SC0];
	_ndr->start_codon[1] = g[start+SC1];
	_ndr->nin = hmm::options::num_inputs(g[start+NIN]);
	_ndr->nout = hmm::options::num_outputs(g[start+NOUT]);
	_ndr->oin = sizeof(table_header)/sizeof(int);
	_ndr->oout = _ndr->oin + _ndr->nin;
	_ndr->ncol = 1; // always one for deterministic tables...
	_ndr->otable = _ndr->oout + _ndr->nout;
	
	for(int i=0; i<(_ndr->nin+_ndr->nout); ++i) {
		_ndr->_data[_ndr->oin+i] = g[start+BEGIN_IO+i];
	}
	
	const int dtablestart=start + BEGIN_IO + _ndr->nin + _ndr->nout;
	for(int i=0; i<(1<<_ndr->nin); ++i) { // row
		dtable(i) = g[dtablestart+i] % (1<<_ndr->nout);
	}
}
Beispiel #2
0
  void
  GC::Heap::sweep(void) {
#if defined(MINIZINC_GC_STATS)
    std::cerr << "=============== GC sweep =============\n";
#endif
    HeapPage* p = _page;
    HeapPage* prev = NULL;
    while (p) {
      size_t off = 0;
      bool wholepage = false;
      while (off < p->used) {
        ASTNode* n = reinterpret_cast<ASTNode*>(p->data+off);
        size_t ns = nodesize(n);
        assert(ns != 0);
#if defined(MINIZINC_GC_STATS)
        GCStat& stats = gc_stats[n->_id];
        stats.first++;
        stats.total += ns;
#endif
        if (n->_gc_mark==0) {
          switch (n->_id) {
            case Item::II_FUN:
              static_cast<FunctionI*>(n)->ann().~Annotation();
              break;
            case Item::II_SOL:
              static_cast<SolveI*>(n)->ann().~Annotation();
              break;
            case Expression::E_VARDECL:
              // Reset WeakRef inside VarDecl
              static_cast<VarDecl*>(n)->flat(NULL);
              // fall through
            default:
              if (n->_id >= ASTNode::NID_END+1 && n->_id <= Expression::EID_END) {
                static_cast<Expression*>(n)->ann().~Annotation();
              }
          }
          if (ns >= _fl_size[0] && ns <= _fl_size[_max_fl]) {
            FreeListNode* fln = static_cast<FreeListNode*>(n);
            new (fln) FreeListNode(ns, _fl[_fl_slot(ns)]);
            _fl[_fl_slot(ns)] = fln;
            _free_mem += ns;
            assert(_alloced_mem >= _free_mem);
          } else {
            assert(off==0);
            assert(p->used==p->size);
            wholepage = true;
          }
        } else {
#if defined(MINIZINC_GC_STATS)
          stats.second++;
#endif
          if (n->_id != ASTNode::NID_FL)
            n->_gc_mark=0;
        }
        off += ns;
      }
      if (wholepage) {
#ifndef NDEBUG
        memset(p->data,42,p->size);
#endif
        if (prev) {
          prev->next = p->next;
        } else {
          assert(p==_page);
          _page = p->next;
        }
        HeapPage* pf = p;
        p = p->next;
        _alloced_mem -= pf->size;
        assert(_alloced_mem >= _free_mem);
        ::free(pf);
      } else {
        prev = p;
        p = p->next;
      }
    }
#if defined(MINIZINC_GC_STATS)
    for (auto stat: gc_stats) {
      std::cerr << _nodeid[stat.first] << ":\t" << stat.second.first << " / " << stat.second.second
      << " / " << stat.second.keepalive << " / " << stat.second.inmodel << " / ";
      if (stat.second.total > 1024) {
        if (stat.second.total > 1024*1024) {
          std::cerr << (stat.second.total / 1024 / 1024) << "M";
        } else {
          std::cerr << (stat.second.total / 1024) << "K";
        }
      } else {
        std::cerr << (stat.second.total);
      }
      std::cerr << std::endl;
    }
#endif
  }
void CLWSamplingInfEngine::
MarginalNodes( const int *queryIn, int querySz, int notExpandJPD)
{
	PNL_CHECK_IS_NULL_POINTER(queryIn);

	if( m_bNormalized == false ) NormalizeWeight();

	int i, j, k;
	int offset;
	int nsamples = m_particleCount;
    	
	const CBNet *pBNet = static_cast<const CBNet *>( m_pGraphicalModel );
	int type = 0;
	int totalnodesizes = 0;
	intVector nodesize(querySz);
	intVector mulnodesize(querySz, 1); 
	
	for( i = querySz; --i >=0; )
	{
		const CNodeType* pNodeType = pBNet->GetNodeType(queryIn[i]);
		nodesize[i] = pNodeType->GetNodeSize();			
		
		if(i == querySz-1)	
			mulnodesize[i] = 1;
		else
			mulnodesize[i] *= nodesize[i+1];

		if(pNodeType->IsDiscrete())
		{
			type++;
			
			if(totalnodesizes == 0)
				totalnodesizes = nodesize[i];
			else
				totalnodesizes = totalnodesizes * nodesize[i]; 
		}
		else
		{
			totalnodesizes = totalnodesizes + nodesize[i]; 
		}
	}
	
	if(type == querySz)
	{
		//all query nodes are discrete
		float *tab = new float[totalnodesizes];
		for(i = 0; i < totalnodesizes; i++) tab[i] = 0;

		for( i = 0; i < nsamples; i++)
		{
			CEvidence* pEvidence = m_currentEvVec[i];
			
			int index = 0;
			for(j = 0; j < querySz; j++)
			{
				Value* pValue = pEvidence->GetValue(queryIn[j]);
				index += pValue->GetInt() * mulnodesize[j];
			}					
			
			tab[index] += m_particleWeight[i];
		}

		m_pQueryJPD = CTabularPotential::Create(queryIn, querySz, pBNet->GetModelDomain ()  );
		m_pQueryJPD->AllocMatrix( tab, matTable );

		delete []tab;
	}
	else if(type == 0)
	{
		//all query nodes are gaussian
		float* val  = new float[totalnodesizes];
		float* mean = new float[totalnodesizes];
		float* cov  = new float[totalnodesizes * totalnodesizes];
		for( i = 0; i < totalnodesizes; i++) mean[i] = 0;
		for( i = 0; i < totalnodesizes * totalnodesizes; i++) cov[i] = 0;

		// mean
		for( i = 0; i < nsamples; i++)
		{
			CEvidence* pEvidence = m_currentEvVec[i];
			
			for(j = 0, 	offset = 0; j < querySz; j++)
			{
				Value* pValue = pEvidence->GetValue(queryIn[j]);
				for(k = 0; k < nodesize[j]; k++)
				{
					mean[offset] += m_particleWeight[i] * pValue[k].GetFlt();
					offset++;
				}
			}
		}
		
		// covariance
		for( i = 0; i < nsamples; i++)
		{
			CEvidence* pEvidence = m_currentEvVec[i];
			
			for(j = 0, 	offset = 0; j < querySz; j++)
			{
				Value* pValue = pEvidence->GetValue(queryIn[j]);
				for(k = 0; k < nodesize[j]; k++)
				{
					val[offset] = pValue[k].GetFlt();
					offset++;
				}
			}
			
			for(j = 0; j < totalnodesizes; j++)
			{
				for(k = j; k < totalnodesizes; k++)
				{
					cov[k*totalnodesizes+j] += ( m_particleWeight[i] * ( val[j]- mean[j]) * (val[k] - mean[k]) );
					cov[j*totalnodesizes+k] = cov[k*totalnodesizes+j];
				}
			}
		}

		m_pQueryJPD = CGaussianPotential::Create( queryIn, querySz, pBNet->GetModelDomain () );
		m_pQueryJPD->AllocMatrix( mean, matMean );
		m_pQueryJPD->AllocMatrix( cov, matCovariance);

		delete []val;
		delete []mean;
		delete []cov;
	}

	//Get MPE
	delete m_pEvidenceMPE;
	m_pEvidenceMPE = NULL;

    m_pEvidenceMPE = m_pQueryJPD->GetMPE();
}