static void tester (long geneLength, long segmentLength, long minNumSegment, bool_t doPrint) { gene_t* genePtr; segments_t* segmentsPtr; random_t* randomPtr; bitmap_t* startBitmapPtr; long i; long j; genePtr = gene_alloc(geneLength); segmentsPtr = segments_alloc(segmentLength, minNumSegment); randomPtr = random_alloc(); startBitmapPtr = bitmap_alloc(geneLength); random_seed(randomPtr, 0); gene_create(genePtr, randomPtr); random_seed(randomPtr, 0); segments_create(segmentsPtr, genePtr, randomPtr); assert(segmentsPtr->minNum == minNumSegment); assert(vector_getSize(segmentsPtr->contentsPtr) >= minNumSegment); if (doPrint) { printf("Gene = %s\n", genePtr->contents); } /* Check that each segment occurs in gene */ for (i = 0; i < vector_getSize(segmentsPtr->contentsPtr); i++) { char *charPtr = strstr(genePtr->contents, (char*)vector_at(segmentsPtr->contentsPtr, i)); assert(charPtr != NULL); j = charPtr - genePtr->contents; bitmap_set(startBitmapPtr, j); if (doPrint) { printf("Segment %li (@%li) = %s\n", i, j, (char*)vector_at(segmentsPtr->contentsPtr, i)); } } /* Check that there is complete overlap */ assert(bitmap_isSet(startBitmapPtr, 0)); for (i = 0, j = 0; i < geneLength; i++ ) { if (bitmap_isSet(startBitmapPtr, i)) { assert((i-j-1) < segmentLength); j = i; } } gene_free(genePtr); segments_free(segmentsPtr); random_free(randomPtr); bitmap_free(startBitmapPtr); }
/* ============================================================================= * net_findDescendants * -- Contents of bitmapPtr set to 1 if descendants, else 0 * -- Returns false if id is not root node (i.e., has cycle back id) * ============================================================================= */ bool_t net_findDescendants (net_t* netPtr, long id, bitmap_t* descendantBitmapPtr, queue_t* workQueuePtr) { bool_t status; vector_t* nodeVectorPtr = netPtr->nodeVectorPtr; assert(descendantBitmapPtr->numBit == vector_getSize(nodeVectorPtr)); bitmap_clearAll(descendantBitmapPtr); queue_clear(workQueuePtr); { net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); list_t* childIdListPtr = nodePtr->childIdListPtr; list_iter_t it; list_iter_reset(&it, childIdListPtr); while (list_iter_hasNext(&it, childIdListPtr)) { long childId = (long)list_iter_next(&it, childIdListPtr); status = bitmap_set(descendantBitmapPtr, childId); assert(status); status = queue_push(workQueuePtr, (void*)childId); assert(status); } } while (!queue_isEmpty(workQueuePtr)) { long childId = (long)queue_pop(workQueuePtr); if (childId == id) { queue_clear(workQueuePtr); return FALSE; } net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, childId); list_t* grandChildIdListPtr = nodePtr->childIdListPtr; list_iter_t it; list_iter_reset(&it, grandChildIdListPtr); while (list_iter_hasNext(&it, grandChildIdListPtr)) { long grandChildId = (long)list_iter_next(&it, grandChildIdListPtr); if (!bitmap_isSet(descendantBitmapPtr, grandChildId)) { status = bitmap_set(descendantBitmapPtr, grandChildId); assert(status); status = queue_push(workQueuePtr, (void*)grandChildId); assert(status); } } } return TRUE; }
/* ============================================================================= * net_findAncestors * -- Contents of bitmapPtr set to 1 if ancestor, else 0 * -- Returns false if id is not root node (i.e., has cycle back id) * ============================================================================= */ bool_t net_findAncestors (net_t* netPtr, long id, bitmap_t* ancestorBitmapPtr, queue_t* workQueuePtr) { bool_t status; vector_t* nodeVectorPtr = LocalLoad(&netPtr->nodeVectorPtr); assert(LocalLoad(&ancestorBitmapPtr->numBit) == vector_getSize(nodeVectorPtr)); bitmap_clearAll(ancestorBitmapPtr); queue_clear(workQueuePtr); { net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); list_t* parentIdListPtr = LocalLoad(&nodePtr->parentIdListPtr); list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); status = bitmap_set(ancestorBitmapPtr, parentId); assert(status); status = queue_push(workQueuePtr, (void*)parentId); assert(status); } } while (!queue_isEmpty(workQueuePtr)) { long parentId = (long)queue_pop(workQueuePtr); if (parentId == id) { queue_clear(workQueuePtr); return FALSE; } net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, parentId); list_t* grandParentIdListPtr = LocalLoad(&nodePtr->parentIdListPtr); list_iter_t it; list_iter_reset(&it, grandParentIdListPtr); while (list_iter_hasNext(&it, grandParentIdListPtr)) { long grandParentId = (long)list_iter_next(&it, grandParentIdListPtr); if (!bitmap_isSet(ancestorBitmapPtr, grandParentId)) { status = bitmap_set(ancestorBitmapPtr, grandParentId); assert(status); status = queue_push(workQueuePtr, (void*)grandParentId); assert(status); } } } return TRUE; }
/* ============================================================================= * net_isPath * ============================================================================= */ bool_t net_isPath (net_t* netPtr, long fromId, long toId, bitmap_t* visitedBitmapPtr, queue_t* workQueuePtr) { bool_t status; vector_t* nodeVectorPtr = netPtr->nodeVectorPtr; assert(visitedBitmapPtr->numBit == vector_getSize(nodeVectorPtr)); bitmap_clearAll(visitedBitmapPtr); queue_clear(workQueuePtr); status = queue_push(workQueuePtr, (void*)fromId); assert(status); while (!queue_isEmpty(workQueuePtr)) { long id = (long)queue_pop(workQueuePtr); if (id == toId) { queue_clear(workQueuePtr); return TRUE; } status = bitmap_set(visitedBitmapPtr, id); assert(status); net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); list_t* childIdListPtr = nodePtr->childIdListPtr; list_iter_t it; list_iter_reset(&it, childIdListPtr); while (list_iter_hasNext(&it, childIdListPtr)) { long childId = (long)list_iter_next(&it, childIdListPtr); if (!bitmap_isSet(visitedBitmapPtr, childId)) { status = queue_push(workQueuePtr, (void*)childId); assert(status); } } } return FALSE; }
/* ============================================================================= * data_generate * -- Binary variables of random PDFs * -- If seed is <0, do not reseed * -- Returns random network * ============================================================================= */ net_t* data_generate (data_t* dataPtr, long seed, long maxNumParent, long percentParent) { random_t* randomPtr = dataPtr->randomPtr; if (seed >= 0) { random_seed(randomPtr, seed); } /* * Generate random Bayesian network */ long numVar = dataPtr->numVar; net_t* netPtr = net_alloc(numVar); assert(netPtr); net_generateRandomEdges(netPtr, maxNumParent, percentParent, randomPtr); /* * Create a threshold for each of the possible permutation of variable * value instances */ long** thresholdsTable = (long**)SEQ_MALLOC(numVar * sizeof(long*)); assert(thresholdsTable); long v; for (v = 0; v < numVar; v++) { list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v); long numThreshold = 1 << list_getSize(parentIdListPtr); long* thresholds = (long*)SEQ_MALLOC(numThreshold * sizeof(long)); assert(thresholds); long t; for (t = 0; t < numThreshold; t++) { long threshold = random_generate(randomPtr) % (DATA_PRECISION + 1); thresholds[t] = threshold; } thresholdsTable[v] = thresholds; } /* * Create variable dependency ordering for record generation */ long* order = (long*)SEQ_MALLOC(numVar * sizeof(long)); assert(order); long numOrder = 0; queue_t* workQueuePtr = queue_alloc(-1); assert(workQueuePtr); vector_t* dependencyVectorPtr = vector_alloc(1); assert(dependencyVectorPtr); bitmap_t* orderedBitmapPtr = bitmap_alloc(numVar); assert(orderedBitmapPtr); bitmap_clearAll(orderedBitmapPtr); bitmap_t* doneBitmapPtr = bitmap_alloc(numVar); assert(doneBitmapPtr); bitmap_clearAll(doneBitmapPtr); v = -1; while ((v = bitmap_findClear(doneBitmapPtr, (v + 1))) >= 0) { list_t* childIdListPtr = net_getChildIdListPtr(netPtr, v); long numChild = list_getSize(childIdListPtr); if (numChild == 0) { bool status; /* * Use breadth-first search to find net connected to this leaf */ queue_clear(workQueuePtr); status = queue_push(workQueuePtr, (void*)v); assert(status); while (!queue_isEmpty(workQueuePtr)) { long id = (long)queue_pop(workQueuePtr); status = bitmap_set(doneBitmapPtr, id); assert(status); status = vector_pushBack(dependencyVectorPtr, (void*)id); assert(status); list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, id); list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); status = queue_push(workQueuePtr, (void*)parentId); assert(status); } } /* * Create ordering */ long i; long n = vector_getSize(dependencyVectorPtr); for (i = 0; i < n; i++) { long id = (long)vector_popBack(dependencyVectorPtr); if (!bitmap_isSet(orderedBitmapPtr, id)) { bitmap_set(orderedBitmapPtr, id); order[numOrder++] = id; } } } } assert(numOrder == numVar); /* * Create records */ char* record = dataPtr->records; long r; long numRecord = dataPtr->numRecord; for (r = 0; r < numRecord; r++) { long o; for (o = 0; o < numOrder; o++) { long v = order[o]; list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, v); long index = 0; list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); long value = record[parentId]; assert(value != DATA_INIT); index = (index << 1) + value; } long rnd = random_generate(randomPtr) % DATA_PRECISION; long threshold = thresholdsTable[v][index]; record[v] = ((rnd < threshold) ? 1 : 0); } record += numVar; assert(record <= (dataPtr->records + numRecord * numVar)); } /* * Clean up */ bitmap_free(doneBitmapPtr); bitmap_free(orderedBitmapPtr); vector_free(dependencyVectorPtr); queue_free(workQueuePtr); SEQ_FREE(order); for (v = 0; v < numVar; v++) { SEQ_FREE(thresholdsTable[v]); } SEQ_FREE(thresholdsTable); return netPtr; }
/* ============================================================================= * segments_create * -- Populates 'contentsPtr' * ============================================================================= */ void segments_create (segments_t* segmentsPtr, gene_t* genePtr, random_t* randomPtr) { vector_t* segmentsContentsPtr; char** strings; long segmentLength; long minNumSegment; char* geneString; long geneLength; bitmap_t* startBitmapPtr; long numStart; long i; long maxZeroRunLength; assert(segmentsPtr != NULL); assert(genePtr != NULL); assert(randomPtr != NULL); segmentsContentsPtr = segmentsPtr->contentsPtr; strings = segmentsPtr->strings; segmentLength = segmentsPtr->length; minNumSegment = segmentsPtr->minNum; geneString = genePtr->contents; geneLength = genePtr->length; startBitmapPtr = genePtr->startBitmapPtr; numStart = geneLength - segmentLength + 1; /* Pick some random segments to start */ for (i = 0; i < minNumSegment; i++) { long j = (long)(random_generate(randomPtr) % numStart); bool_t status = bitmap_set(startBitmapPtr, j); assert(status); memcpy(strings[i], &(geneString[j]), segmentLength * sizeof(char)); status = vector_pushBack(segmentsContentsPtr, (void*)strings[i]); assert(status); } /* Make sure segment covers start */ i = 0; if (!bitmap_isSet(startBitmapPtr, i)) { char* string = (char*)malloc((segmentLength+1) * sizeof(char)); string[segmentLength] = '\0'; memcpy(string, &(geneString[i]), segmentLength * sizeof(char)); bool_t status = vector_pushBack(segmentsContentsPtr, (void*)string); assert(status); status = bitmap_set(startBitmapPtr, i); assert(status); } /* Add extra segments to fill holes and ensure overlap */ maxZeroRunLength = segmentLength - 1; for (i = 0; i < numStart; i++) { long i_stop = MIN((i+maxZeroRunLength), numStart); for ( /* continue */; i < i_stop; i++) { if (bitmap_isSet(startBitmapPtr, i)) { break; } } if (i == i_stop) { /* Found big enough hole */ char* string = (char*)malloc((segmentLength+1) * sizeof(char)); string[segmentLength] = '\0'; i = i - 1; memcpy(string, &(geneString[i]), segmentLength * sizeof(char)); bool_t status = vector_pushBack(segmentsContentsPtr, (void*)string); assert(status); status = bitmap_set(startBitmapPtr, i); assert(status); } } }
int main () { long numNode = 100; puts("Starting tests..."); bool_t status; net_t* netPtr = net_alloc(numNode); assert(netPtr); bitmap_t* visitedBitmapPtr = bitmap_alloc(numNode); assert(visitedBitmapPtr); queue_t* workQueuePtr = queue_alloc(-1); assert(workQueuePtr); assert(!net_isCycle(netPtr)); long aId = 31; long bId = 14; long cId = 5; long dId = 92; net_applyOperation(netPtr, OPERATION_INSERT, aId, bId); assert(net_isPath(netPtr, aId, bId, visitedBitmapPtr, workQueuePtr)); assert(!net_isPath(netPtr, bId, aId, visitedBitmapPtr, workQueuePtr)); assert(!net_isPath(netPtr, aId, cId, visitedBitmapPtr, workQueuePtr)); assert(!net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr)); assert(!net_isCycle(netPtr)); net_applyOperation(netPtr, OPERATION_INSERT, bId, cId); net_applyOperation(netPtr, OPERATION_INSERT, aId, cId); net_applyOperation(netPtr, OPERATION_INSERT, dId, aId); assert(!net_isCycle(netPtr)); net_applyOperation(netPtr, OPERATION_INSERT, cId, dId); assert(net_isCycle(netPtr)); net_applyOperation(netPtr, OPERATION_REVERSE, cId, dId); assert(!net_isCycle(netPtr)); net_applyOperation(netPtr, OPERATION_REVERSE, dId, cId); assert(net_isCycle(netPtr)); assert(net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr)); net_applyOperation(netPtr, OPERATION_REMOVE, cId, dId); assert(!net_isPath(netPtr, aId, dId, visitedBitmapPtr, workQueuePtr)); bitmap_t* ancestorBitmapPtr = bitmap_alloc(numNode); assert(ancestorBitmapPtr); status = net_findAncestors(netPtr, cId, ancestorBitmapPtr, workQueuePtr); assert(status); assert(bitmap_isSet(ancestorBitmapPtr, aId)); assert(bitmap_isSet(ancestorBitmapPtr, bId)); assert(bitmap_isSet(ancestorBitmapPtr, dId)); assert(bitmap_getNumSet(ancestorBitmapPtr) == 3); bitmap_t* descendantBitmapPtr = bitmap_alloc(numNode); assert(descendantBitmapPtr); status = net_findDescendants(netPtr, aId, descendantBitmapPtr, workQueuePtr); assert(status); assert(bitmap_isSet(descendantBitmapPtr, bId)); assert(bitmap_isSet(descendantBitmapPtr, cId)); assert(bitmap_getNumSet(descendantBitmapPtr) == 2); bitmap_free(visitedBitmapPtr); queue_free(workQueuePtr); bitmap_free(ancestorBitmapPtr); bitmap_free(descendantBitmapPtr); net_free(netPtr); random_t* randomPtr = random_alloc(); assert(randomPtr); netPtr = net_alloc(numNode); assert(netPtr); net_generateRandomEdges(netPtr, 10, 10, randomPtr); net_free(netPtr); puts("All tests passed."); return 0; }
int main () { bitmap_t* bitmapPtr; long numBit = 32; long i; long j; srand(0); puts("Starting..."); bitmapPtr = bitmap_alloc(numBit); assert(bitmapPtr->numBit == numBit); assert(bitmapPtr->numWord == (DIVIDE_AND_ROUND_UP(numBit, NUM_BIT_PER_WORD))); /* Check that initial is all clear */ for (i = 0; i < numBit; i++) { assert(bitmap_isClear(bitmapPtr, i)); assert(!bitmap_isSet(bitmapPtr, i)); } assert(bitmap_getNumClear(bitmapPtr) == numBit); assert(bitmap_getNumSet(bitmapPtr) == 0); /* Check bounds */ assert(!bitmap_clear(bitmapPtr, -1)); assert(!bitmap_set(bitmapPtr, -1)); assert(!bitmap_clear(bitmapPtr, numBit+1)); assert(!bitmap_set(bitmapPtr, numBit+1)); /* Set random bits */ for (i = 0, j = 0; i < numBit; i+=(rand() % 5 + 1)) { assert(bitmap_set(bitmapPtr, i)); assert(bitmap_set(bitmapPtr, i)); assert(bitmap_clear(bitmapPtr, i)); assert(bitmap_set(bitmapPtr, i)); assert(bitmap_set(bitmapPtr, i)); assert(bitmap_isSet(bitmapPtr, i)); j++; } assert(bitmap_getNumClear(bitmapPtr) == (numBit - j)); assert(bitmap_getNumSet(bitmapPtr) == j); /* Clear set bits */ while ((i = bitmap_findSet(bitmapPtr, -1)) >= 0) { assert(bitmap_clear(bitmapPtr, i)); i++; } assert(bitmap_getNumClear(bitmapPtr) == numBit); assert(bitmap_getNumSet(bitmapPtr) == 0); assert(bitmap_findSet(bitmapPtr, -1) == -1); /* Set all bits */ i = -1; while ((i = bitmap_findClear(bitmapPtr, i)) >= 0) { assert(bitmap_set(bitmapPtr, i)); i++; } assert(bitmap_getNumClear(bitmapPtr) == 0); assert(bitmap_getNumSet(bitmapPtr) == numBit); assert(bitmap_findClear(bitmapPtr, -1) == -1); /* Clear random bits */ for (i = 0, j = 0; i < numBit; i+=(rand() % 5 + 1)) { assert(bitmap_clear(bitmapPtr, i)); assert(bitmap_clear(bitmapPtr, i)); assert(bitmap_set(bitmapPtr, i)); assert(bitmap_clear(bitmapPtr, i)); assert(bitmap_clear(bitmapPtr, i)); assert(bitmap_isClear(bitmapPtr, i)); j++; } assert(bitmap_getNumClear(bitmapPtr) == j); assert(bitmap_getNumSet(bitmapPtr) == (numBit - j)); bitmap_free(bitmapPtr); puts("All tests passed."); return 0; }