double dmatrix_GetAtCoord( void * void_dmat, int * theCoordinates, unsigned len ) { DSL_Dmatrix * dmat = reinterpret_cast<DSL_Dmatrix*>(void_dmat); DSL_intArray coords = DSL_intArray(len); for (unsigned i = 0; i < len; i++) coords[i] = theCoordinates[i]; double retval = dmat->Subscript(coords); return retval; }
void DSLPNLConverter::CreateFactors(DSL_network& dslNet, CBNet* pnlBNet) { int i,j,k; // Read number of nodes in the net int numberOfNodes = dslNet.GetNumberOfNodes(); // This is a way PNL likes it pnlBNet->AllocFactors(); DSL_Dmatrix* dslMatrix; CCPD* pnlCPD; for (i=0;i<numberOfNodes;i++) { // Get parents of the ith node // IMPORTANT -- we should preserve order from DSL_network, since // probabilities will be according DSL ordering DSL_intArray dslParents; dslParents = dslNet.GetParents(dslNet.FindNode(theIds[i])); // establish sizes and allocate memory int numberOfNodesInDomain = dslParents.NumItems() + 1; int* domain = new int[numberOfNodesInDomain]; CNodeType** nodeTypes = new CNodeType*[numberOfNodesInDomain]; // establish members of the domain for (j=0;j<numberOfNodesInDomain-1;j++) domain[j] = dslParents[j]; domain[numberOfNodesInDomain-1] = i; // Fill up node types for (j=0;j<numberOfNodesInDomain;j++) nodeTypes[j] = const_cast <CNodeType*> (pnlBNet->GetNodeType(domain[j])); // Read CPT from SMILE dslNet.GetNode(dslNet.FindNode(theIds[i]))->Definition()->GetDefinition(&dslMatrix); // Alloc space for CPT int sizeOfCPT = dslMatrix->GetSize(); float* flatCPT = new float[sizeOfCPT]; // Here we convert 'copy' numbers from SMILE to PNL // The painful part is convert double to float. // Additionally we check if after conversion they sum-up to 1 int numberOfMyStates = nodeTypes[numberOfNodesInDomain-1]->GetNodeSize(); int iterations = sizeOfCPT/numberOfMyStates; for (j=0;j<iterations;j++) { float sum = 0.0f; for (k=0;k<numberOfMyStates;k++) { flatCPT[j*numberOfMyStates+k] = static_cast <float> (dslMatrix->Subscript(j*numberOfMyStates+k)); sum += flatCPT[j*numberOfMyStates+k]; } if (sum!=1.0f) { for (k=0;k<numberOfMyStates;k++) flatCPT[j*numberOfMyStates+k] /= sum; } } #ifdef DSLPNL_DEBUG std::cerr << "Node "<< i << " domain : "; for (j=0;j<numberOfNodesInDomain;j++) { std::cerr << domain[j] << " "; } std::cerr << std::endl; for (j=0;j<sizeOfCPT;j++) std::cerr << flatCPT[j] << " "; std::cerr << std::endl; #endif CModelDomain* pMD = pnlBNet->GetModelDomain(); pnlCPD = CTabularCPD::Create(domain, numberOfNodesInDomain, pMD, flatCPT); if (pnlCPD==NULL) { std::cout << "We got a problem with creating CPD" << std::endl; return; } pnlBNet->AttachFactor(pnlCPD); delete[] nodeTypes; delete[] domain; delete[] flatCPT; } }
int dmatrix_CoordinatesToIndex( void * void_dmat, void * void_intarr ) { DSL_Dmatrix * dmat = reinterpret_cast<DSL_Dmatrix*>(void_dmat); DSL_intArray * arr = reinterpret_cast<DSL_intArray*>(void_intarr); return dmat->CoordinatesToIndex(*arr); }
int dmatrix_GetSizeOfDimension( void * void_dmat, int aDimension ) { DSL_Dmatrix * dmat = reinterpret_cast<DSL_Dmatrix*>(void_dmat); return dmat->GetSizeOfDimension( aDimension ); }
int dmatrix_GetSize( void * void_dmat ) { DSL_Dmatrix * dmat = reinterpret_cast<DSL_Dmatrix*>(void_dmat); return dmat->GetSize(); }
int dmatrix_GetNumberOfDimensions( void * void_dmat ) { DSL_Dmatrix * dmat = reinterpret_cast<DSL_Dmatrix*>(void_dmat); return dmat->GetNumberOfDimensions(); }
void dmatrix_SetAtInd( void * void_dmat, int index, double value ) { DSL_Dmatrix * dmat = reinterpret_cast<DSL_Dmatrix*>(void_dmat); dmat->Subscript(index) = value; }
double dmatrix_GetAtInd( void * void_dmat, int index ) { DSL_Dmatrix * dmat = reinterpret_cast<DSL_Dmatrix*>(void_dmat); double retval = dmat->Subscript(index); return retval; }