#define OPERATION_INSERT 0
#define OPERATION_REMOVE 1
#define OPERATION_REVERSE 2
+#define NUM_OPERATION 3
public class Learner {
Adtree adtreePtr;
#endif
}
-
- /* =============================================================================
- * compareQuery
- * -- Want smallest ID first
- * -- For vector_sort
- * =============================================================================
- */
- /*
- static int
- compareQuery (const void* aPtr, const void* bPtr)
- {
- query_t* aQueryPtr = (query_t*)(*(void**)aPtr);
- query_t* bQueryPtr = (query_t*)(*(void**)bPtr);
-
- return (aQueryPtr.index - bQueryPtr.index);
- }
- */
-
-
/* =============================================================================
* learner_alloc
* =============================================================================
*/
public static Learner
- learner_alloc (Data dataPtr, Adtree adtreePtr, int numThread)
+ learner_alloc (Data dataPtr,
+ Adtree adtreePtr,
+ int numThread,
+ int global_insertPenalty,
+ int global_maxNumEdgeLearned,
+ float global_operationQualityFactor)
{
Learner learnerPtr = new Learner();
learnerPtr.tasks = new LearnerTask[dataPtr.numVar];
learnerPtr.taskListPtr = List.list_alloc();
learnerPtr.numTotalParent = 0;
+#ifndef TEST_LEARNER
+ learnerPtr.global_insertPenalty = global_insertPenalty;
+ learnerPtr.global_maxNumEdgeLearned = global_maxNumEdgeLearned;
+ learnerPtr.global_operationQualityFactor = global_operationQualityFactor;
+#endif
}
return learnerPtr;
/* =============================================================================
* TMpopTask
- * -- Returns NULL is list is empty
+ * -- Returns null is list is empty
* =============================================================================
*/
-/*
- learner_task_t*
- TMpopTask (TM_ARGDECL list_t* taskListPtr)
+ public LearnerTask TMpopTask (List taskListPtr)
{
- learner_task_t* taskPtr = NULL;
-
- list_iter_t it;
- TMLIST_ITER_RESET(&it, taskListPtr);
- if (TMLIST_ITER_HASNEXT(&it, taskListPtr)) {
- taskPtr = (learner_task_t*)TMLIST_ITER_NEXT(&it, taskListPtr);
- boolean status = TMLIST_REMOVE(taskListPtr, (void*)taskPtr);
- assert(status);
+ LearnerTask taskPtr = null;
+
+ ListNode it = taskListPtr.head;
+ taskListPtr.list_iter_reset(it);
+ if (taskListPtr.list_iter_hasNext(it)) {
+ it = it.nextPtr;
+ taskPtr = taskListPtr.list_iter_next(it);
+ boolean status = taskListPtr.list_remove(taskPtr);
+ if(status == false) {
+ System.out.println("Assert failed: when removing from a list in TMpopTask()");
+ System.exit(0);
+ }
}
return taskPtr;
}
- */
/* =============================================================================
* -- Modifies contents of parentQueryVectorPtr
* =============================================================================
*/
- /*
- static void
- TMpopulateParentQueryVector (TM_ARGDECL
- net_t* netPtr,
+ public void
+ TMpopulateParentQueryVector (Net netPtr,
int id,
- query_t* queries,
- vector_t* parentQueryVectorPtr)
+ Query[] queries,
+ Vector_t parentQueryVectorPtr)
{
- vector_clear(parentQueryVectorPtr);
-
- list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, id);
- list_iter_t it;
- TMLIST_ITER_RESET(&it, parentIdListPtr);
- while (TMLIST_ITER_HASNEXT(&it, parentIdListPtr)) {
- int parentId = (int)TMLIST_ITER_NEXT(&it, parentIdListPtr);
- boolean status = PVECTOR_PUSHBACK(parentQueryVectorPtr,
- (void*)&queries[parentId]);
- assert(status);
+ parentQueryVectorPtr.vector_clear();
+
+ IntList parentIdListPtr = netPtr.net_getParentIdListPtr(id);
+ IntListNode it = parentIdListPtr.head;
+ parentIdListPtr.list_iter_reset(it);
+
+ while (parentIdListPtr.list_iter_hasNext(it)) {
+ it = it.nextPtr;
+ int parentId = parentIdListPtr.list_iter_next(it);
+ boolean status = parentQueryVectorPtr.vector_pushBack(queries[parentId]);
+ if(status == false) {
+ System.out.println("Assert failed: unable to pushBack in queue in TMpopulateParentQueryVector()");
+ System.exit(0);
+ }
}
}
- */
/* =============================================================================
boolean status;
status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr);
if(status == false ) {
- System.out.println("Assert failed: whille vector copy in populateQueryVectors()");
+ System.out.println("Assert failed: while vector copy in populateQueryVectors()");
System.exit(0);
}
status = queryVectorPtr.vector_pushBack(queries[id]);
if(status == false ) {
- System.out.println("Assert failed: whille vector pushBack in populateQueryVectors()");
+ System.out.println("Assert failed: while vector pushBack in populateQueryVectors()");
System.exit(0);
}
* -- Modifies contents of queryVectorPtr and parentQueryVectorPtr
* =============================================================================
*/
- /*
- static void
- TMpopulateQueryVectors (TM_ARGDECL
- net_t* netPtr,
+ public void
+ TMpopulateQueryVectors (Net netPtr,
int id,
- query_t* queries,
- vector_t* queryVectorPtr,
- vector_t* parentQueryVectorPtr)
+ Query[] queries,
+ Vector_t queryVectorPtr,
+ Vector_t parentQueryVectorPtr)
{
- TMpopulateParentQueryVector(TM_ARG netPtr, id, queries, parentQueryVectorPtr);
+ TMpopulateParentQueryVector(netPtr, id, queries, parentQueryVectorPtr);
boolean status;
- status = PVECTOR_COPY(queryVectorPtr, parentQueryVectorPtr);
- assert(status);
- status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[id]);
- assert(status);
- PVECTOR_SORT(queryVectorPtr, &compareQuery);
+ status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr);
+ if(status == false ) {
+ System.out.println("Assert failed: while vector copy in TMpopulateQueryVectors()");
+ System.exit(0);
+ }
+ status = queryVectorPtr.vector_pushBack(queries[id]);
+ if(status == false ) {
+ System.out.println("Assert failed: while vector pushBack in TMpopulateQueryVectors()");
+ System.exit(0);
+ }
+
+ queryVectorPtr.vector_sort();
}
-*/
/* =============================================================================
* computeLocalLogLikelihoodHelper
* TMfindBestInsertTask
* =============================================================================
*/
- /*
- static learner_task_t
- TMfindBestInsertTask (TM_ARGDECL findBestTaskArg_t* argPtr)
+ public LearnerTask
+ TMfindBestInsertTask (FindBestTaskArg argPtr)
{
- int toId = argPtr.toId;
- learner_t* learnerPtr = argPtr.learnerPtr;
- query_t* queries = argPtr.queries;
- vector_t* queryVectorPtr = argPtr.queryVectorPtr;
- vector_t* parentQueryVectorPtr = argPtr.parentQueryVectorPtr;
- int numTotalParent = argPtr.numTotalParent;
- float basePenalty = argPtr.basePenalty;
- float baseLogLikelihood = argPtr.baseLogLikelihood;
- bitmap_t* invalidBitmapPtr = argPtr.bitmapPtr;
- queue_t* workQueuePtr = argPtr.workQueuePtr;
- vector_t* baseParentQueryVectorPtr = argPtr.aQueryVectorPtr;
- vector_t* baseQueryVectorPtr = argPtr.bQueryVectorPtr;
+ int toId = argPtr.toId;
+ Learner learnerPtr = argPtr.learnerPtr;
+ Query[] queries = argPtr.queries;
+ Vector_t queryVectorPtr = argPtr.queryVectorPtr;
+ Vector_t parentQueryVectorPtr = argPtr.parentQueryVectorPtr;
+ int numTotalParent = argPtr.numTotalParent;
+ float basePenalty = argPtr.basePenalty;
+ float baseLogLikelihood = argPtr.baseLogLikelihood;
+ BitMap invalidBitmapPtr = argPtr.bitmapPtr;
+ Queue workQueuePtr = argPtr.workQueuePtr;
+ Vector_t baseParentQueryVectorPtr = argPtr.aQueryVectorPtr;
+ Vector_t baseQueryVectorPtr = argPtr.bQueryVectorPtr;
boolean status;
- adtree_t* adtreePtr = learnerPtr.adtreePtr;
- net_t* netPtr = learnerPtr.netPtr;
- float* localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods;
+ Adtree adtreePtr = learnerPtr.adtreePtr;
+ Net netPtr = learnerPtr.netPtr;
- TMpopulateParentQueryVector(TM_ARG netPtr, toId, queries, parentQueryVectorPtr);
- */
+ TMpopulateParentQueryVector(netPtr, toId, queries, parentQueryVectorPtr);
/*
* Create base query and parentQuery
*/
- /*
- status = PVECTOR_COPY(baseParentQueryVectorPtr, parentQueryVectorPtr);
- assert(status);
+ status = Vector_t.vector_copy(baseParentQueryVectorPtr, parentQueryVectorPtr);
+ if(status == false) {
+ System.out.println("Assert failed: copying baseParentQuery vector in TMfindBestInsertTask");
+ System.exit(0);
+ }
+
+ status = Vector_t.vector_copy(baseQueryVectorPtr, baseParentQueryVectorPtr);
+ if(status == false) {
+ System.out.println("Assert failed: copying baseQuery vector in TMfindBestInsertTask");
+ System.exit(0);
+ }
+
+ status = baseQueryVectorPtr.vector_pushBack(queries[toId]);
+ if(status == false ) {
+ System.out.println("Assert failed: while vector pushBack in TMfindBestInsertTask()");
+ System.exit(0);
+ }
- status = PVECTOR_COPY(baseQueryVectorPtr, baseParentQueryVectorPtr);
- assert(status);
- status = PVECTOR_PUSHBACK(baseQueryVectorPtr, (void*)&queries[toId]);
- assert(status);
- PVECTOR_SORT(queryVectorPtr, &compareQuery);
- */
+ queryVectorPtr.vector_sort();
/*
* Search all possible valid operations for better local log likelihood
*/
- /*
- float bestFromId = toId; // flag for not found
- float oldLocalLogLikelihood =
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]);
+ int bestFromId = toId; // flag for not found
+ float oldLocalLogLikelihood = learnerPtr.localBaseLogLikelihoods[toId];
float bestLocalLogLikelihood = oldLocalLogLikelihood;
- status = TMNET_FINDDESCENDANTS(netPtr, toId, invalidBitmapPtr, workQueuePtr);
- assert(status);
+ status = netPtr.net_findDescendants(toId, invalidBitmapPtr, workQueuePtr);
+ if(status == false) {
+ System.out.println("Assert failed: while net_findDescendants in TMfindBestInsertTask()");
+ System.exit(0);
+ }
+
int fromId = -1;
- list_t* parentIdListPtr = net_getParentIdListPtr(netPtr, toId);
+ IntList parentIdListPtr = netPtr.net_getParentIdListPtr(toId);
int maxNumEdgeLearned = global_maxNumEdgeLearned;
if ((maxNumEdgeLearned < 0) ||
- (TMLIST_GETSIZE(parentIdListPtr) <= maxNumEdgeLearned))
+ (parentIdListPtr.list_getSize() <= maxNumEdgeLearned))
{
- list_iter_t it;
- TMLIST_ITER_RESET(&it, parentIdListPtr);
- while (TMLIST_ITER_HASNEXT(&it, parentIdListPtr)) {
- int parentId = (int)TMLIST_ITER_NEXT(&it, parentIdListPtr);
- bitmap_set(invalidBitmapPtr, parentId); // invalid since already have edge
+ IntListNode it = parentIdListPtr.head;
+ parentIdListPtr.list_iter_reset(it);
+
+ while( parentIdListPtr.list_iter_hasNext(it)) {
+ it = it.nextPtr;
+ int parentId = parentIdListPtr.list_iter_next(it);
+ invalidBitmapPtr.bitmap_set(parentId); // invalid since already have edge
}
- while ((fromId = bitmap_findClear(invalidBitmapPtr, (fromId + 1))) >= 0) {
+ while ((fromId = invalidBitmapPtr.bitmap_findClear((fromId + 1))) >= 0) {
if (fromId == toId) {
continue;
}
- status = PVECTOR_COPY(queryVectorPtr, baseQueryVectorPtr);
- assert(status);
- status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[fromId]);
- assert(status);
- PVECTOR_SORT(queryVectorPtr, &compareQuery);
+ status = Vector_t.vector_copy(queryVectorPtr, baseQueryVectorPtr);
+ if(status == false) {
+ System.out.println("Assert failed: copying query vector in TMfindBestInsertTask");
+ System.exit(0);
+ }
+
+ status = queryVectorPtr.vector_pushBack(queries[fromId]);
+ if(status == false) {
+ System.out.println("Assert failed: vector pushback for query in TMfindBestInsertTask");
+ System.exit(0);
+ }
+
+ queryVectorPtr.vector_sort();
+
+ status = Vector_t.vector_copy(parentQueryVectorPtr, baseParentQueryVectorPtr);
+ if(status == false) {
+ System.out.println("Assert failed: copying parentQuery vector in TMfindBestInsertTask");
+ System.exit(0);
+ }
+
+ status = parentQueryVectorPtr.vector_pushBack(queries[fromId]);
+ if(status == false) {
+ System.out.println("Assert failed: vector pushBack for parentQuery in TMfindBestInsertTask");
+ System.exit(0);
+ }
- status = PVECTOR_COPY(parentQueryVectorPtr, baseParentQueryVectorPtr);
- assert(status);
- status = PVECTOR_PUSHBACK(parentQueryVectorPtr, (void*)&queries[fromId]);
- assert(status);
- PVECTOR_SORT(parentQueryVectorPtr, &compareQuery);
+ parentQueryVectorPtr.vector_sort();
float newLocalLogLikelihood =
computeLocalLogLikelihood(toId,
} // foreach valid parent
} // if have not exceeded max number of edges to learn
- */
/*
* Return best task; Note: if none is better, fromId will equal toId
*/
- /*
- learner_task_t bestTask;
+ LearnerTask bestTask = new LearnerTask();
bestTask.op = OPERATION_INSERT;
bestTask.fromId = bestFromId;
bestTask.toId = toId;
- bestTask.score = 0.0;
+ bestTask.score = 0.0f;
if (bestFromId != toId) {
int numRecord = adtreePtr.numRecord;
- int numParent = TMLIST_GETSIZE(parentIdListPtr) + 1;
+ int numParent = parentIdListPtr.list_getSize() + 1;
float penalty =
(numTotalParent + numParent * global_insertPenalty) * basePenalty;
float logLikelihood = numRecord * (baseLogLikelihood +
return bestTask;
}
- */
-
#ifdef LEARNER_TRY_REMOVE
/* =============================================================================
* TMfindBestRemoveTask
* =============================================================================
*/
- /*
- static learner_task_t
- TMfindBestRemoveTask (TM_ARGDECL findBestTaskArg_t* argPtr)
+ public LearnerTask
+ TMfindBestRemoveTask (FindBestTaskArg argPtr)
{
int toId = argPtr.toId;
- learner_t* learnerPtr = argPtr.learnerPtr;
- query_t* queries = argPtr.queries;
- vector_t* queryVectorPtr = argPtr.queryVectorPtr;
- vector_t* parentQueryVectorPtr = argPtr.parentQueryVectorPtr;
+ Learner learnerPtr = argPtr.learnerPtr;
+ Query[] queries = argPtr.queries;
+ Vector_t queryVectorPtr = argPtr.queryVectorPtr;
+ Vector_t parentQueryVectorPtr = argPtr.parentQueryVectorPtr;
int numTotalParent = argPtr.numTotalParent;
- float basePenalty = argPtr.basePenalty;
- float baseLogLikelihood = argPtr.baseLogLikelihood;
- vector_t* origParentQueryVectorPtr = argPtr.aQueryVectorPtr;
+ float basePenalty = argPtr.basePenalty;
+ float baseLogLikelihood = argPtr.baseLogLikelihood;
+ Vector_t origParentQueryVectorPtr = argPtr.aQueryVectorPtr;
boolean status;
- adtree_t* adtreePtr = learnerPtr.adtreePtr;
- net_t* netPtr = learnerPtr.netPtr;
- float* localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods;
+ Adtree adtreePtr = learnerPtr.adtreePtr;
+ Net netPtr = learnerPtr.netPtr;
+ float[] localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods;
- TMpopulateParentQueryVector(TM_ARG
- netPtr, toId, queries, origParentQueryVectorPtr);
- int numParent = PVECTOR_GETSIZE(origParentQueryVectorPtr);
- */
+ TMpopulateParentQueryVector(netPtr, toId, queries, origParentQueryVectorPtr);
+ int numParent = origParentQueryVectorPtr.vector_getSize();
/*
* Search all possible valid operations for better local log likelihood
*/
- /*
- float bestFromId = toId; // flag for not found
- float oldLocalLogLikelihood =
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]);
+ int bestFromId = toId; // flag for not found
+ float oldLocalLogLikelihood = localBaseLogLikelihoods[toId];
float bestLocalLogLikelihood = oldLocalLogLikelihood;
int i;
for (i = 0; i < numParent; i++) {
- query_t* queryPtr = (query_t*)PVECTOR_AT(origParentQueryVectorPtr, i);
+ Query queryPtr = (Query) (origParentQueryVectorPtr.vector_at(i));
int fromId = queryPtr.index;
- */
/*
* Create parent query (subset of parents since remove an edge)
*/
- /*
+ parentQueryVectorPtr.vector_clear();
- PVECTOR_CLEAR(parentQueryVectorPtr);
-
- int p;
- for (p = 0; p < numParent; p++) {
+ for (int p = 0; p < numParent; p++) {
if (p != fromId) {
- query_t* queryPtr = PVECTOR_AT(origParentQueryVectorPtr, p);
- status = PVECTOR_PUSHBACK(parentQueryVectorPtr,
- (void*)&queries[queryPtr.index]);
- assert(status);
+ Query tmpqueryPtr = (Query) (origParentQueryVectorPtr.vector_at(p));
+ status = parentQueryVectorPtr.vector_pushBack(queries[tmpqueryPtr.index]);
+ if(status == false) {
+ System.out.println("Assert failed: vector_pushBack to parentQuery in TMfindBestRemoveTask()");
+ System.exit(0);
+ }
}
} // create new parent query
- */
/*
* Create query
*/
- /*
- status = PVECTOR_COPY(queryVectorPtr, parentQueryVectorPtr);
- assert(status);
- status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[toId]);
- assert(status);
- PVECTOR_SORT(queryVectorPtr, &compareQuery);
- */
+ status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr);
+ if(status == false) {
+ System.out.println("Assert failed: while vector copy to query in TMfindBestRemoveTask()");
+ System.exit(0);
+ }
+
+ status = queryVectorPtr.vector_pushBack(queries[toId]);
+ if(status == false) {
+ System.out.println("Assert failed: while vector_pushBack to query in TMfindBestRemoveTask()");
+ System.exit(0);
+ }
+
+ queryVectorPtr.vector_sort();
/*
* See if removing parent is better
*/
- /*
float newLocalLogLikelihood =
computeLocalLogLikelihood(toId,
adtreePtr,
}
} // for each parent
- */
/*
* Return best task; Note: if none is better, fromId will equal toId
*/
- /*
- learner_task_t bestTask;
+ LearnerTask bestTask = new LearnerTask();
bestTask.op = OPERATION_REMOVE;
bestTask.fromId = bestFromId;
bestTask.toId = toId;
- bestTask.score = 0.0;
+ bestTask.score = 0.0f;
if (bestFromId != toId) {
int numRecord = adtreePtr.numRecord;
return bestTask;
}
- */
#endif /* LEARNER_TRY_REMOVE */
* TMfindBestReverseTask
* =============================================================================
*/
- /*
- static learner_task_t
- TMfindBestReverseTask (TM_ARGDECL findBestTaskArg_t* argPtr)
+ public LearnerTask
+ TMfindBestReverseTask (FindBestTaskArg argPtr)
{
int toId = argPtr.toId;
- learner_t* learnerPtr = argPtr.learnerPtr;
- query_t* queries = argPtr.queries;
- vector_t* queryVectorPtr = argPtr.queryVectorPtr;
- vector_t* parentQueryVectorPtr = argPtr.parentQueryVectorPtr;
+ Learner learnerPtr = argPtr.learnerPtr;
+ Query[] queries = argPtr.queries;
+ Vector_t queryVectorPtr = argPtr.queryVectorPtr;
+ Vector_t parentQueryVectorPtr = argPtr.parentQueryVectorPtr;
int numTotalParent = argPtr.numTotalParent;
- float basePenalty = argPtr.basePenalty;
- float baseLogLikelihood = argPtr.baseLogLikelihood;
- bitmap_t* visitedBitmapPtr = argPtr.bitmapPtr;
- queue_t* workQueuePtr = argPtr.workQueuePtr;
- vector_t* toOrigParentQueryVectorPtr = argPtr.aQueryVectorPtr;
- vector_t* fromOrigParentQueryVectorPtr = argPtr.bQueryVectorPtr;
+ float basePenalty = argPtr.basePenalty;
+ float baseLogLikelihood = argPtr.baseLogLikelihood;
+ BitMap visitedBitmapPtr = argPtr.bitmapPtr;
+ Queue workQueuePtr = argPtr.workQueuePtr;
+ Vector_t toOrigParentQueryVectorPtr = argPtr.aQueryVectorPtr;
+ Vector_t fromOrigParentQueryVectorPtr = argPtr.bQueryVectorPtr;
boolean status;
- adtree_t* adtreePtr = learnerPtr.adtreePtr;
- net_t* netPtr = learnerPtr.netPtr;
- float* localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods;
+ Adtree adtreePtr = learnerPtr.adtreePtr;
+ Net netPtr = learnerPtr.netPtr;
+ float[] localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods;
- TMpopulateParentQueryVector(TM_ARG
- netPtr, toId, queries, toOrigParentQueryVectorPtr);
- int numParent = PVECTOR_GETSIZE(toOrigParentQueryVectorPtr);
- */
+ TMpopulateParentQueryVector(netPtr, toId, queries, toOrigParentQueryVectorPtr);
+ int numParent = toOrigParentQueryVectorPtr.vector_getSize();
/*
* Search all possible valid operations for better local log likelihood
*/
- /*
int bestFromId = toId; // flag for not found
- float oldLocalLogLikelihood =
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]);
+ float oldLocalLogLikelihood = localBaseLogLikelihoods[toId];
float bestLocalLogLikelihood = oldLocalLogLikelihood;
int fromId = 0;
- int i;
- for (i = 0; i < numParent; i++) {
+ for (int i = 0; i < numParent; i++) {
- query_t* queryPtr = (query_t*)PVECTOR_AT(toOrigParentQueryVectorPtr, i);
+ Query queryPtr = (Query) (toOrigParentQueryVectorPtr.vector_at(i));
fromId = queryPtr.index;
bestLocalLogLikelihood =
- oldLocalLogLikelihood +
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[fromId]);
+ oldLocalLogLikelihood + localBaseLogLikelihoods[fromId];
- TMpopulateParentQueryVector(TM_ARG
- netPtr,
+ TMpopulateParentQueryVector(netPtr,
fromId,
queries,
fromOrigParentQueryVectorPtr);
- */
/*
* Create parent query (subset of parents since remove an edge)
*/
- /*
-
- PVECTOR_CLEAR(parentQueryVectorPtr);
+ parentQueryVectorPtr.vector_clear();
- int p;
- for (p = 0; p < numParent; p++) {
+ for (int p = 0; p < numParent; p++) {
if (p != fromId) {
- query_t* queryPtr = PVECTOR_AT(toOrigParentQueryVectorPtr, p);
- status = PVECTOR_PUSHBACK(parentQueryVectorPtr,
- (void*)&queries[queryPtr.index]);
- assert(status);
+ Query tmpqueryPtr = (Query) (toOrigParentQueryVectorPtr.vector_at(p));
+ status = parentQueryVectorPtr.vector_pushBack(queries[tmpqueryPtr.index]);
+ if(status == false) {
+ System.out.println("Assert failed: while vector_pushBack parentQueryVectorPtr");
+ System.exit(0);
+ }
}
} // create new parent query
- */
/*
* Create query
*/
- /*
- status = PVECTOR_COPY(queryVectorPtr, parentQueryVectorPtr);
- assert(status);
- status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[toId]);
- assert(status);
- PVECTOR_SORT(queryVectorPtr, &compareQuery);
- */
+ status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr);
+ if(status == false) {
+ System.out.println("Assert failed: while vector_copy in TMfindBestReverseTask()");
+ System.exit(0);
+ }
+
+ status = queryVectorPtr.vector_pushBack(queries[toId]);
+ if(status == false) {
+ System.out.println("Assert failed: while vector_pushBack in TMfindBestReverseTask()");
+ System.exit(0);
+ }
+
+ queryVectorPtr.vector_sort();
/*
* Get log likelihood for removing parent from toId
*/
- /*
float newLocalLogLikelihood =
computeLocalLogLikelihood(toId,
queries,
queryVectorPtr,
parentQueryVectorPtr);
- */
/*
* Get log likelihood for adding parent to fromId
*/
- /*
+ status = Vector_t.vector_copy(parentQueryVectorPtr, fromOrigParentQueryVectorPtr);
+ if(status == false) {
+ System.out.println("Assert failed: while parentQuery vector_copy in TMfindBestReverseTask()");
+ System.exit(0);
+ }
+
+ status = parentQueryVectorPtr.vector_pushBack(queries[toId]);
+ if(status == false) {
+ System.out.println("Assert failed: while vector_pushBack into parentQuery on TMfindBestReverseTask()");
+ System.exit(0);
+ }
+
+ parentQueryVectorPtr.vector_sort();
+
+ status = Vector_t.vector_copy(queryVectorPtr, parentQueryVectorPtr);
+ if(status == false) {
+ System.out.println("Assert failed: while vector_copy on TMfindBestReverseTask()");
+ System.exit(0);
+ }
- status = PVECTOR_COPY(parentQueryVectorPtr, fromOrigParentQueryVectorPtr);
- assert(status);
- status = PVECTOR_PUSHBACK(parentQueryVectorPtr, (void*)&queries[toId]);
- assert(status);
- PVECTOR_SORT(parentQueryVectorPtr, &compareQuery);
+ status = queryVectorPtr.vector_pushBack(queries[fromId]);
+ if(status == false) {
+ System.out.println("Assert failed: while vector_pushBack on TMfindBestReverseTask()");
+ System.exit(0);
+ }
- status = PVECTOR_COPY(queryVectorPtr, parentQueryVectorPtr);
- assert(status);
- status = PVECTOR_PUSHBACK(queryVectorPtr, (void*)&queries[fromId]);
- assert(status);
- PVECTOR_SORT(queryVectorPtr, &compareQuery);
+ queryVectorPtr.vector_sort();
newLocalLogLikelihood +=
computeLocalLogLikelihood(fromId,
queries,
queryVectorPtr,
parentQueryVectorPtr);
- */
/*
* Record best
*/
- /*
+
if (newLocalLogLikelihood > bestLocalLogLikelihood) {
bestLocalLogLikelihood = newLocalLogLikelihood;
bestFromId = fromId;
}
} // for each parent
- */
/*
* Check validity of best
*/
- /*
if (bestFromId != toId) {
- boolean isTaskValid = TRUE;
- TMNET_APPLYOPERATION(netPtr, OPERATION_REMOVE, bestFromId, toId);
- if (TMNET_ISPATH(netPtr,
- bestFromId,
+ boolean isTaskValid = true;
+ netPtr.net_applyOperation(OPERATION_REMOVE, bestFromId, toId);
+ if (netPtr.net_isPath(bestFromId,
toId,
visitedBitmapPtr,
workQueuePtr))
{
- isTaskValid = FALSE;
+ isTaskValid = false;
}
- TMNET_APPLYOPERATION(netPtr, OPERATION_INSERT, bestFromId, toId);
+ netPtr.net_applyOperation(OPERATION_INSERT, bestFromId, toId);
if (!isTaskValid) {
bestFromId = toId;
}
}
- */
/*
* Return best task; Note: if none is better, fromId will equal toId
*/
- /*
- learner_task_t bestTask;
+ LearnerTask bestTask = new LearnerTask();
bestTask.op = OPERATION_REVERSE;
bestTask.fromId = bestFromId;
bestTask.toId = toId;
- bestTask.score = 0.0;
+ bestTask.score = 0.0f;
if (bestFromId != toId) {
- float fromLocalLogLikelihood =
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[bestFromId]);
+ float fromLocalLogLikelihood = localBaseLogLikelihoods[bestFromId];
int numRecord = adtreePtr.numRecord;
float penalty = numTotalParent * basePenalty;
float logLikelihood = numRecord * (baseLogLikelihood +
return bestTask;
}
- */
+
#endif /* LEARNER_TRY_REVERSE */
* threads.
* =============================================================================
*/
- /*
- static void
- learnStructure (void* argPtr)
+ public static void
+ learnStructure (int myId, int numThread, Learner learnerPtr)
{
- TM_THREAD_ENTER();
- learner_t* learnerPtr = (learner_t*)argPtr;
- net_t* netPtr = learnerPtr.netPtr;
- adtree_t* adtreePtr = learnerPtr.adtreePtr;
- int numRecord = adtreePtr.numRecord;
- float* localBaseLogLikelihoods = learnerPtr.localBaseLogLikelihoods;
- list_t* taskListPtr = learnerPtr.taskListPtr;
+ int numRecord = learnerPtr.adtreePtr.numRecord;
- float operationQualityFactor = global_operationQualityFactor;
+ float operationQualityFactor = learnerPtr.global_operationQualityFactor;
- bitmap_t* visitedBitmapPtr = PBITMAP_ALLOC(learnerPtr.adtreePtr.numVar);
- assert(visitedBitmapPtr);
- queue_t* workQueuePtr = PQUEUE_ALLOC(-1);
- assert(workQueuePtr);
+ BitMap visitedBitmapPtr = BitMap.bitmap_alloc(learnerPtr.adtreePtr.numVar);
+ if(visitedBitmapPtr == null) {
+ System.out.println("Assert failed: for bitmap alloc in learnStructure()");
+ System.exit(0);
+ }
- int numVar = adtreePtr.numVar;
- query_t* queries = (query_t*)P_MALLOC(numVar * sizeof(query_t));
- assert(queries);
- int v;
- for (v = 0; v < numVar; v++) {
+ Queue workQueuePtr = Queue.queue_alloc(-1);
+ if(workQueuePtr == null) {
+ System.out.println("Assert failed: for vector alloc in learnStructure()");
+ System.exit(0);
+ }
+
+ int numVar = learnerPtr.adtreePtr.numVar;
+ Query[] queries = new Query[numVar];
+
+ if(queries == null) {
+ System.out.println("Assert failed: for queries alloc in learnStructure()");
+ System.exit(0);
+ }
+
+ for (int v = 0; v < numVar; v++) {
+ queries[v] = new Query();
queries[v].index = v;
queries[v].value = QUERY_VALUE_WILDCARD;
}
- float basePenalty = (float)(-0.5 * log((double)numRecord));
+ float basePenalty = (float)(-0.5 * Math.log((double)numRecord));
- vector_t* queryVectorPtr = PVECTOR_ALLOC(1);
- assert(queryVectorPtr);
- vector_t* parentQueryVectorPtr = PVECTOR_ALLOC(1);
- assert(parentQueryVectorPtr);
- vector_t* aQueryVectorPtr = PVECTOR_ALLOC(1);
- assert(aQueryVectorPtr);
- vector_t* bQueryVectorPtr = PVECTOR_ALLOC(1);
- assert(bQueryVectorPtr);
+ Vector_t queryVectorPtr = Vector_t.vector_alloc(1);
+ if(queryVectorPtr == null) {
+ System.out.println("Assert failed: for vector_alloc in learnStructure()");
+ System.exit(0);
+ }
- findBestTaskArg_t arg;
+ Vector_t parentQueryVectorPtr = Vector_t.vector_alloc(1);
+ if(parentQueryVectorPtr == null) {
+ System.out.println("Assert failed: for vector_alloc in learnStructure()");
+ System.exit(0);
+ }
+
+ Vector_t aQueryVectorPtr = Vector_t.vector_alloc(1);
+ if(aQueryVectorPtr == null) {
+ System.out.println("Assert failed: for vector_alloc in learnStructure()");
+ System.exit(0);
+ }
+
+ Vector_t bQueryVectorPtr = Vector_t.vector_alloc(1);
+ if(bQueryVectorPtr == null) {
+ System.out.println("Assert failed: for vector_alloc in learnStructure()");
+ System.exit(0);
+ }
+
+
+ FindBestTaskArg arg = new FindBestTaskArg();
arg.learnerPtr = learnerPtr;
arg.queries = queries;
arg.queryVectorPtr = queryVectorPtr;
arg.aQueryVectorPtr = aQueryVectorPtr;
arg.bQueryVectorPtr = bQueryVectorPtr;
- while (1) {
+ while (true) {
+
+ LearnerTask taskPtr;
+
+ atomic {
+ taskPtr = learnerPtr.TMpopTask(learnerPtr.taskListPtr);
+ }
- learner_task_t* taskPtr;
- TM_BEGIN();
- taskPtr = TMpopTask(TM_ARG taskListPtr);
- TM_END();
- if (taskPtr == NULL) {
+ if (taskPtr == null) {
break;
}
- operation_t op = taskPtr.op;
+ int op = taskPtr.op;
int fromId = taskPtr.fromId;
int toId = taskPtr.toId;
boolean isTaskValid;
- TM_BEGIN();
- */
+ atomic {
+ /*
+ * Check if task is still valid
+ */
+
+ isTaskValid = true;
+ if(op == OPERATION_INSERT) {
+ if(learnerPtr.netPtr.net_hasEdge(fromId, toId) ||
+ learnerPtr.netPtr.net_isPath(toId,
+ fromId,
+ visitedBitmapPtr,
+ workQueuePtr))
+ {
+ isTaskValid = false;
+ }
+ } else if (op == OPERATION_REMOVE) {
+ // Can never create cycle, so always valid
+ ;
+ } else if (op == OPERATION_REVERSE) {
+ // Temporarily remove edge for check
+ learnerPtr.netPtr.net_applyOperation(OPERATION_REMOVE, fromId, toId);
+ if(learnerPtr.netPtr.net_isPath(fromId,
+ toId,
+ visitedBitmapPtr,
+ workQueuePtr))
+ {
+ isTaskValid = false;
+ }
+ learnerPtr.netPtr.net_applyOperation(OPERATION_INSERT, fromId, toId);
+ } else {
+ System.out.println("Assert failed: We shouldn't get here in learnStructure()");
+ System.exit(0);
+ }
- /*
- * Check if task is still valid
- */
- /*
- isTaskValid = TRUE;
- switch (op) {
- case OPERATION_INSERT: {
- if (TMNET_HASEDGE(netPtr, fromId, toId) ||
- TMNET_ISPATH(netPtr,
- toId,
- fromId,
- visitedBitmapPtr,
- workQueuePtr))
- {
- isTaskValid = FALSE;
- }
- break;
- }
- case OPERATION_REMOVE: {
- // Can never create cycle, so always valid
- break;
- }
- case OPERATION_REVERSE: {
- // Temporarily remove edge for check
- TMNET_APPLYOPERATION(netPtr, OPERATION_REMOVE, fromId, toId);
- if (TMNET_ISPATH(netPtr,
- fromId,
- toId,
- visitedBitmapPtr,
- workQueuePtr))
- {
- isTaskValid = FALSE;
- }
- TMNET_APPLYOPERATION(netPtr, OPERATION_INSERT, fromId, toId);
- break;
- }
- default:
- assert(0);
- }
#ifdef TEST_LEARNER
- printf("[task] op=%i from=%li to=%li score=%lf valid=%s\n",
- taskPtr.op, taskPtr.fromId, taskPtr.toId, taskPtr.score,
- (isTaskValid ? "yes" : "no"));
- fflush(stdout);
+ System.out.println("[task] op= " + taskPtr.op + " from= " + taskPtr.fromId + " to= " +
+ taskPtr.toId + " score= " + taskPtr.score + " valid= " + (isTaskValid ? "yes" : "no"));
#endif
-*/
- /*
- * Perform task: update graph and probabilities
- */
+ /*
+ * Perform task: update graph and probabilities
+ */
- /*
- if (isTaskValid) {
- TMNET_APPLYOPERATION(netPtr, op, fromId, toId);
- }
+ if (isTaskValid) {
+ learnerPtr.netPtr.net_applyOperation(op, fromId, toId);
+ }
- TM_END();
+ }
- float deltaLogLikelihood = 0.0;
+ float deltaLogLikelihood = 0.0f;
if (isTaskValid) {
+ float newBaseLogLikelihood;
+ if(op == OPERATION_INSERT) {
+ atomic {
+ learnerPtr.TMpopulateQueryVectors(learnerPtr.netPtr,
+ toId,
+ queries,
+ queryVectorPtr,
+ parentQueryVectorPtr);
+ newBaseLogLikelihood =
+ learnerPtr.computeLocalLogLikelihood(toId,
+ learnerPtr.adtreePtr,
+ learnerPtr.netPtr,
+ queries,
+ queryVectorPtr,
+ parentQueryVectorPtr);
+ float toLocalBaseLogLikelihood = learnerPtr.localBaseLogLikelihoods[toId];
+ deltaLogLikelihood +=
+ toLocalBaseLogLikelihood - newBaseLogLikelihood;
+ learnerPtr.localBaseLogLikelihoods[toId] = newBaseLogLikelihood;
+ }
+
+ atomic {
+ int numTotalParent = learnerPtr.numTotalParent;
+ learnerPtr.numTotalParent = numTotalParent + 1;
+ }
- switch (op) {
- float newBaseLogLikelihood;
- case OPERATION_INSERT: {
- TM_BEGIN();
- TMpopulateQueryVectors(TM_ARG
- netPtr,
- toId,
- queries,
- queryVectorPtr,
- parentQueryVectorPtr);
- newBaseLogLikelihood =
- computeLocalLogLikelihood(toId,
- adtreePtr,
- netPtr,
- queries,
- queryVectorPtr,
- parentQueryVectorPtr);
- float toLocalBaseLogLikelihood =
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]);
- deltaLogLikelihood +=
- toLocalBaseLogLikelihood - newBaseLogLikelihood;
- TM_SHARED_WRITE_F(localBaseLogLikelihoods[toId],
- newBaseLogLikelihood);
- TM_END();
- TM_BEGIN();
- int numTotalParent = (int)TM_SHARED_READ(learnerPtr.numTotalParent);
- TM_SHARED_WRITE(learnerPtr.numTotalParent, (numTotalParent + 1));
- TM_END();
- break;
- }
#ifdef LEARNER_TRY_REMOVE
- case OPERATION_REMOVE: {
- TM_BEGIN();
- TMpopulateQueryVectors(TM_ARG
- netPtr,
- fromId,
- queries,
- queryVectorPtr,
- parentQueryVectorPtr);
- newBaseLogLikelihood =
- computeLocalLogLikelihood(fromId,
- adtreePtr,
- netPtr,
- queries,
- queryVectorPtr,
- parentQueryVectorPtr);
- float fromLocalBaseLogLikelihood =
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[fromId]);
- deltaLogLikelihood +=
- fromLocalBaseLogLikelihood - newBaseLogLikelihood;
- TM_SHARED_WRITE_F(localBaseLogLikelihoods[fromId],
- newBaseLogLikelihood);
- TM_END();
- TM_BEGIN();
- int numTotalParent = (int)TM_SHARED_READ(learnerPtr.numTotalParent);
- TM_SHARED_WRITE(learnerPtr.numTotalParent, (numTotalParent - 1));
- TM_END();
- break;
- }
-#endif // LEARNER_TRY_REMOVE
+ } else if(op == OPERATION_REMOVE) {
+ atomic {
+ learnerPtr.TMpopulateQueryVectors(learnerPtr.netPtr,
+ fromId,
+ queries,
+ queryVectorPtr,
+ parentQueryVectorPtr);
+ newBaseLogLikelihood =
+ learnerPtr. computeLocalLogLikelihood(fromId,
+ learnerPtr.adtreePtr,
+ learnerPtr.netPtr,
+ queries,
+ queryVectorPtr,
+ parentQueryVectorPtr);
+ float fromLocalBaseLogLikelihood =
+ learnerPtr.localBaseLogLikelihoods[fromId];
+ deltaLogLikelihood +=
+ fromLocalBaseLogLikelihood - newBaseLogLikelihood;
+ learnerPtr.localBaseLogLikelihoods[fromId] = newBaseLogLikelihood;
+ }
+
+ atomic{
+ int numTotalParent = learnerPtr.numTotalParent;
+ learnerPtr.numTotalParent = numTotalParent - 1;
+ }
+
+#endif // LEARNER_TRY_REMOVE
#ifdef LEARNER_TRY_REVERSE
- case OPERATION_REVERSE: {
- TM_BEGIN();
- TMpopulateQueryVectors(TM_ARG
- netPtr,
- fromId,
- queries,
- queryVectorPtr,
- parentQueryVectorPtr);
- newBaseLogLikelihood =
- computeLocalLogLikelihood(fromId,
- adtreePtr,
- netPtr,
- queries,
- queryVectorPtr,
- parentQueryVectorPtr);
- float fromLocalBaseLogLikelihood =
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[fromId]);
- deltaLogLikelihood +=
- fromLocalBaseLogLikelihood - newBaseLogLikelihood;
- TM_SHARED_WRITE_F(localBaseLogLikelihoods[fromId],
- newBaseLogLikelihood);
- TM_END();
-
- TM_BEGIN();
- TMpopulateQueryVectors(TM_ARG
- netPtr,
- toId,
- queries,
- queryVectorPtr,
- parentQueryVectorPtr);
- newBaseLogLikelihood =
- computeLocalLogLikelihood(toId,
- adtreePtr,
- netPtr,
- queries,
- queryVectorPtr,
- parentQueryVectorPtr);
- float toLocalBaseLogLikelihood =
- (float)TM_SHARED_READ_F(localBaseLogLikelihoods[toId]);
- deltaLogLikelihood +=
- toLocalBaseLogLikelihood - newBaseLogLikelihood;
- TM_SHARED_WRITE_F(localBaseLogLikelihoods[toId],
- newBaseLogLikelihood);
- TM_END();
- break;
- }
+ } else if(op == OPERATION_REVERSE) {
+ atomic {
+ learnerPtr.TMpopulateQueryVectors(learnerPtr.netPtr,
+ fromId,
+ queries,
+ queryVectorPtr,
+ parentQueryVectorPtr);
+ newBaseLogLikelihood =
+ learnerPtr.computeLocalLogLikelihood(fromId,
+ learnerPtr.adtreePtr,
+ learnerPtr.netPtr,
+ queries,
+ queryVectorPtr,
+ parentQueryVectorPtr);
+ float fromLocalBaseLogLikelihood =
+ learnerPtr.localBaseLogLikelihoods[fromId];
+ deltaLogLikelihood +=
+ fromLocalBaseLogLikelihood - newBaseLogLikelihood;
+ learnerPtr.localBaseLogLikelihoods[fromId] = newBaseLogLikelihood;
+ }
+
+ atomic {
+ learnerPtr.TMpopulateQueryVectors(learnerPtr.netPtr,
+ toId,
+ queries,
+ queryVectorPtr,
+ parentQueryVectorPtr);
+ newBaseLogLikelihood =
+ learnerPtr.computeLocalLogLikelihood(toId,
+ learnerPtr.adtreePtr,
+ learnerPtr.netPtr,
+ queries,
+ queryVectorPtr,
+ parentQueryVectorPtr);
+ float toLocalBaseLogLikelihood =
+ learnerPtr.localBaseLogLikelihoods[toId];
+ deltaLogLikelihood +=
+ toLocalBaseLogLikelihood - newBaseLogLikelihood;
+ learnerPtr.localBaseLogLikelihoods[toId] = newBaseLogLikelihood;
+ }
+
#endif // LEARNER_TRY_REVERSE
- default:
- assert(0);
- } // switch op
+ } else {
+ System.out.println("Assert failed: We should not reach here in learnerPtr()");
+ System.exit(0);
+ } //switch op
- } // if isTaskValid
-*/
+ } //if isTaskValid
/*
* Update/read globals
*/
- /*
+
float baseLogLikelihood;
int numTotalParent;
- TM_BEGIN();
- float oldBaseLogLikelihood =
- (float)TM_SHARED_READ_F(learnerPtr.baseLogLikelihood);
- float newBaseLogLikelihood = oldBaseLogLikelihood + deltaLogLikelihood;
- TM_SHARED_WRITE_F(learnerPtr.baseLogLikelihood, newBaseLogLikelihood);
- baseLogLikelihood = newBaseLogLikelihood;
- numTotalParent = (int)TM_SHARED_READ(learnerPtr.numTotalParent);
- TM_END();
- */
+ atomic {
+ float oldBaseLogLikelihood = learnerPtr.baseLogLikelihood;
+ float newBaseLogLikelihood = oldBaseLogLikelihood + deltaLogLikelihood;
+ learnerPtr.baseLogLikelihood = newBaseLogLikelihood;
+ baseLogLikelihood = newBaseLogLikelihood;
+ numTotalParent = learnerPtr.numTotalParent;
+ }
/*
* Find next task
*/
- /*
+
+
float baseScore = ((float)numTotalParent * basePenalty)
+ (numRecord * baseLogLikelihood);
- learner_task_t bestTask;
+ LearnerTask bestTask = new LearnerTask();
bestTask.op = NUM_OPERATION;
bestTask.toId = -1;
bestTask.fromId = -1;
bestTask.score = baseScore;
- learner_task_t newTask;
+ LearnerTask newTask = new LearnerTask();
arg.toId = toId;
arg.numTotalParent = numTotalParent;
arg.basePenalty = basePenalty;
arg.baseLogLikelihood = baseLogLikelihood;
- TM_BEGIN();
- newTask = TMfindBestInsertTask(TM_ARG &arg);
- TM_END();
+ atomic {
+ newTask = learnerPtr.TMfindBestInsertTask(arg);
+ }
if ((newTask.fromId != newTask.toId) &&
(newTask.score > (bestTask.score / operationQualityFactor)))
}
#ifdef LEARNER_TRY_REMOVE
- TM_BEGIN();
- newTask = TMfindBestRemoveTask(TM_ARG &arg);
- TM_END();
+ atomic {
+ newTask = learnerPtr.TMfindBestRemoveTask(arg);
+ }
if ((newTask.fromId != newTask.toId) &&
(newTask.score > (bestTask.score / operationQualityFactor)))
#endif // LEARNER_TRY_REMOVE
#ifdef LEARNER_TRY_REVERSE
- TM_BEGIN();
- newTask = TMfindBestReverseTask(TM_ARG &arg);
- TM_END();
+ atomic {
+ newTask = learnerPtr.TMfindBestReverseTask(arg);
+ }
if ((newTask.fromId != newTask.toId) &&
(newTask.score > (bestTask.score / operationQualityFactor)))
#endif // LEARNER_TRY_REVERSE
if (bestTask.toId != -1) {
- learner_task_t* tasks = learnerPtr.tasks;
+ LearnerTask[] tasks = learnerPtr.tasks;
tasks[toId] = bestTask;
- TM_BEGIN();
- TMLIST_INSERT(taskListPtr, (void*)&tasks[toId]);
- TM_END();
+ atomic {
+ learnerPtr.taskListPtr.list_insert(tasks[toId]);
+ }
#ifdef TEST_LEARNER
- printf("[new] op=%i from=%li to=%li score=%lf\n",
- bestTask.op, bestTask.fromId, bestTask.toId, bestTask.score);
- fflush(stdout);
+ System.out.println("[new] op= " + bestTask.op + " from= "+ bestTask.fromId + " to= "+ bestTask.toId +
+ " score= " + bestTask.score);
#endif
}
} // while (tasks)
- PBITMAP_FREE(visitedBitmapPtr);
- PQUEUE_FREE(workQueuePtr);
- PVECTOR_FREE(bQueryVectorPtr);
- PVECTOR_FREE(aQueryVectorPtr);
- PVECTOR_FREE(queryVectorPtr);
- PVECTOR_FREE(parentQueryVectorPtr);
- P_FREE(queries);
-
- TM_THREAD_EXIT();
+ visitedBitmapPtr.bitmap_free();
+ workQueuePtr.queue_free();
+ bQueryVectorPtr.vector_free();
+ aQueryVectorPtr.vector_free();
+ queryVectorPtr.vector_free();
+ parentQueryVectorPtr.vector_free();
+ queries = null;
}
-*/
/* =============================================================================
* -- Call adtree_make before this
* =============================================================================
*/
- /*
+ //Is not called anywhere now parallel code
public void
learner_run (int myId, int numThread, Learner learnerPtr)
- //learner_run (learner_t* learnerPtr)
{
-#ifdef OTM
-#pragma omp parallel
{
- createTaskList((void*)learnerPtr);
+ createTaskList(myId, numThread, learnerPtr);
}
-#pragma omp parallel
{
- learnStructure((void*)learnerPtr);
+ learnStructure(myId, numThread, learnerPtr);
}
-#else
- thread_start(&createTaskList, (void*)learnerPtr);
- thread_start(&learnStructure, (void*)learnerPtr);
-#endif
}
-*/
/* =============================================================================
* learner_score
}
- /* =============================================================================
- * TMinsertEdge
- * =============================================================================
- */
- /*
- static void
- TMinsertEdge (TM_ARGDECL net_t* netPtr, int fromId, int toId)
- {
- vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
- boolean status;
-
- net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId);
- list_t* parentIdListPtr = childNodePtr.parentIdListPtr;
- status = TMLIST_INSERT(parentIdListPtr, (void*)fromId);
- assert(status);
-
- net_node_t* parentNodePtr = (net_node_t*)vector_at(nodeVectorPtr, fromId);
- list_t* childIdListPtr = parentNodePtr.childIdListPtr;
- status = TMLIST_INSERT(childIdListPtr, (void*)toId);
- assert(status);
- }
- */
-
-
/* =============================================================================
* removeEdge
* =============================================================================
}
}
- /* =============================================================================
- * TMremoveEdge
- * =============================================================================
- */
- /*
- static void
- TMremoveEdge (TM_ARGDECL net_t* netPtr, int fromId, int toId)
- {
- vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
- boolean status;
-
- net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId);
- list_t* parentIdListPtr = childNodePtr.parentIdListPtr;
- status = TMLIST_REMOVE(parentIdListPtr, (void*)fromId);
- assert(status);
-
- net_node_t* parentNodePtr = (net_node_t*)vector_at(nodeVectorPtr, fromId);
- list_t* childIdListPtr = parentNodePtr.childIdListPtr;
- status = TMLIST_REMOVE(childIdListPtr, (void*)toId);
- assert(status);
- }
- */
-
/* =============================================================================
* reverseEdge
* =============================================================================
}
- /* =============================================================================
- * TMreverseEdge
- * =============================================================================
- */
- /*
- static void
- TMreverseEdge (TM_ARGDECL net_t* netPtr, int fromId, int toId)
- {
- TMremoveEdge(TM_ARG netPtr, fromId, toId);
- TMinsertEdge(TM_ARG netPtr, toId, fromId);
- }
- */
-
-
/* =============================================================================
* net_applyOperation
* =============================================================================
} else if(op == OPERATION_REVERSE) {
reverseEdge(fromId, toId);
} else {
- System.out.println("Operation failed");
+ System.out.println("Assert failed: We shouldn't get here in net_applyOperation()");
System.exit(0);
}
}
- /* =============================================================================
- * TMnet_applyOperation
- * =============================================================================
- */
- /*
- void
- TMnet_applyOperation (TM_ARGDECL
- net_t* netPtr, operation_t op, int fromId, int toId)
- {
- switch (op) {
- case OPERATION_INSERT: TMinsertEdge(TM_ARG netPtr, fromId, toId); break;
- case OPERATION_REMOVE: TMremoveEdge(TM_ARG netPtr, fromId, toId); break;
- case OPERATION_REVERSE: TMreverseEdge(TM_ARG netPtr, fromId, toId); break;
- default:
- assert(0);
- }
- }
-
- */
-
-
/* =============================================================================
* net_hasEdge
* =============================================================================
* TMnet_hasEdge
* =============================================================================
*/
- /*
- boolean
- TMnet_hasEdge (TM_ARGDECL net_t* netPtr, int fromId, int toId)
+ public boolean
+ TMnet_hasEdge (int fromId, int toId)
{
- vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
- net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId);
- list_t* parentIdListPtr = childNodePtr.parentIdListPtr;
-
- list_iter_t it;
- TMLIST_ITER_RESET(&it, parentIdListPtr);
- while (TMLIST_ITER_HASNEXT(&it, parentIdListPtr)) {
- int parentId = (int)TMLIST_ITER_NEXT(&it, parentIdListPtr);
+ NetNode childNodePtr = (NetNode)(nodeVectorPtr.vector_at(toId));
+
+ IntList parentIdListPtr = childNodePtr.parentIdListPtr;
+ IntListNode it = parentIdListPtr.head;//initialize iterator
+ parentIdListPtr.list_iter_reset(it);
+
+ while (parentIdListPtr.list_iter_hasNext(it)) {
+ it = it.nextPtr;
+ int parentId = parentIdListPtr.list_iter_next(it);
if (parentId == fromId) {
return true;
}
return false;
}
- */
/* =============================================================================
}
- /* =============================================================================
- * TMnet_isPath
- * =============================================================================
- */
- /*
- boolean
- TMnet_isPath (TM_ARGDECL
- net_t* netPtr,
- int fromId,
- int toId,
- Bitmap* visitedBitmapPtr,
- Queue* workQueuePtr)
- {
- boolean status;
-
- vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
- assert(visitedBitmapPtr.numBit == vector_getSize(nodeVectorPtr));
-
- PBITMAP_CLEARALL(visitedBitmapPtr);
- PQUEUE_CLEAR(workQueuePtr);
-
- status = PQUEUE_PUSH(workQueuePtr, (void*)fromId);
- assert(status);
-
- while (!PQUEUE_ISEMPTY(workQueuePtr)) {
- int id = (int)queue_pop(workQueuePtr);
- if (id == toId) {
- queue_clear(workQueuePtr);
- return true;
- }
- status = PBITMAP_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;
- TMLIST_ITER_RESET(&it, childIdListPtr);
- while (TMLIST_ITER_HASNEXT(&it, childIdListPtr)) {
- int childId = (int)TMLIST_ITER_NEXT(&it, childIdListPtr);
- if (!PBITMAP_ISSET(visitedBitmapPtr, childId)) {
- status = PQUEUE_PUSH(workQueuePtr, (void*)childId);
- assert(status);
- }
- }
- }
-
- return false;
- }
- */
-
-
/* =============================================================================
* isCycle
* =============================================================================
}
- /* =============================================================================
- * TMnet_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)
- * =============================================================================
- */
- /*
- boolean
- TMnet_findAncestors (TM_ARGDECL
- net_t* netPtr,
- int id,
- Bitmap* ancestorBitmapPtr,
- Queue* workQueuePtr)
- {
- boolean status;
-
- vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
- assert(ancestorBitmapPtr.numBit == vector_getSize(nodeVectorPtr));
-
- PBITMAP_CLEARALL(ancestorBitmapPtr);
- PQUEUE_CLEAR(workQueuePtr);
-
- {
- net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id);
- list_t* parentIdListPtr = nodePtr.parentIdListPtr;
- list_iter_t it;
- TMLIST_ITER_RESET(&it, parentIdListPtr);
- while (TMLIST_ITER_HASNEXT(&it, parentIdListPtr)) {
- int parentId = (int)TMLIST_ITER_NEXT(&it, parentIdListPtr);
- status = PBITMAP_SET(ancestorBitmapPtr, parentId);
- assert(status);
- status = PQUEUE_PUSH(workQueuePtr, (void*)parentId);
- assert(status);
- }
- }
-
- while (!PQUEUE_ISEMPTY(workQueuePtr)) {
- int parentId = (int)PQUEUE_POP(workQueuePtr);
- if (parentId == id) {
- PQUEUE_CLEAR(workQueuePtr);
- return false;
- }
- net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, parentId);
- list_t* grandParentIdListPtr = nodePtr.parentIdListPtr;
- list_iter_t it;
- TMLIST_ITER_RESET(&it, grandParentIdListPtr);
- while (TMLIST_ITER_HASNEXT(&it, grandParentIdListPtr)) {
- int grandParentId = (int)TMLIST_ITER_NEXT(&it, grandParentIdListPtr);
- if (!PBITMAP_ISSET(ancestorBitmapPtr, grandParentId)) {
- status = PBITMAP_SET(ancestorBitmapPtr, grandParentId);
- assert(status);
- status = PQUEUE_PUSH(workQueuePtr, (void*)grandParentId);
- assert(status);
- }
- }
- }
-
- return true;
- }
- */
-
-
/* =============================================================================
* net_findDescendants
* -- Contents of bitmapPtr set to 1 if descendants, else 0
{
boolean status;
- //vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
- if(descendantBitmapPtr.numBit == nodeVectorPtr.vector_getSize()) {
+ if(descendantBitmapPtr.numBit != nodeVectorPtr.vector_getSize()) {
System.out.println("Assert failed: for descendantBitmapPtr.numbit in net_findDescendants()");
System.exit(0);
}
- //assert(descendantBitmapPtr.numBit == vector_getSize(nodeVectorPtr));
descendantBitmapPtr.bitmap_clearAll();
workQueuePtr.queue_clear();
return true;
}
-
- /* =============================================================================
- * TMnet_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)
- * =============================================================================
- */
- /*
- boolean
- TMnet_findDescendants (TM_ARGDECL
- net_t* netPtr,
- int id,
- Bitmap* descendantBitmapPtr,
- Queue* workQueuePtr)
- {
- boolean status;
-
- vector_t* nodeVectorPtr = netPtr.nodeVectorPtr;
- assert(descendantBitmapPtr.numBit == vector_getSize(nodeVectorPtr));
-
- PBITMAP_CLEARALL(descendantBitmapPtr);
- PQUEUE_CLEAR(workQueuePtr);
-
- {
- net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id);
- list_t* childIdListPtr = nodePtr.childIdListPtr;
- list_iter_t it;
- TMLIST_ITER_RESET(&it, childIdListPtr);
- while (TMLIST_ITER_HASNEXT(&it, childIdListPtr)) {
- int childId = (int)TMLIST_ITER_NEXT(&it, childIdListPtr);
- status = PBITMAP_SET(descendantBitmapPtr, childId);
- assert(status);
- status = PQUEUE_PUSH(workQueuePtr, (void*)childId);
- assert(status);
- }
- }
-
- while (!PQUEUE_ISEMPTY(workQueuePtr)) {
- int childId = (int)PQUEUE_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;
- TMLIST_ITER_RESET(&it, grandChildIdListPtr);
- while (TMLIST_ITER_HASNEXT(&it, grandChildIdListPtr)) {
- int grandChildId = (int)TMLIST_ITER_NEXT(&it, grandChildIdListPtr);
- if (!PBITMAP_ISSET(descendantBitmapPtr, grandChildId)) {
- status = PBITMAP_SET(descendantBitmapPtr, grandChildId);
- assert(status);
- status = PQUEUE_PUSH(workQueuePtr, (void*)grandChildId);
- assert(status);
- }
- }
- }
-
- return true;
- }
- */
-
-
/* =============================================================================
* net_generateRandomEdges
* =============================================================================