HashTable *HashTableNew(int size) { HashTable* table = NULL; int i = 0; table = MNewPtr(sizeof(HashTable)); table->size = size; table->array = MNewPtr(sizeof(VoidListPtr) * size); for(i = 0; i < size; i++) { table->array[i] = VLNew(); DebugError(table->array[i] == NULL, "Failed allocate in HashTableNew"); } return table; }
int main(int argc, char *argv[]) { int i,j; VoidListPtr activeSearches, inactiveSearches, pausedSearches; int allDone, changes; struct tms starttime; struct tms endtime; long learnTime; long seenTotal, exampleNumber; char dataFileName[255]; FILE *exampleIn; ExamplePtr e; FILE *netOut; /* init the data */ _InitializeGlobals(argc, argv); times(&starttime); /* set up the many searches */ activeSearches = VLNew(); inactiveSearches = VLNew(); pausedSearches = VLNew(); for(i = 0 ; i < BNGetNumNodes(gCurrentNet) ; i++) { VLAppend(inactiveSearches, MSNew(gCurrentNet, i)); } _ActivateSearches(activeSearches, inactiveSearches, pausedSearches); sprintf(dataFileName, "%s/%s.data", gSourceDirectory, gFileStem); if(gStdin) { exampleIn = stdin; } else { exampleIn = fopen(dataFileName, "r"); DebugError(exampleIn == 0, "Unable to open the data file"); } seenTotal = exampleNumber = 0; allDone = 0; while(!allDone) { e = ExampleRead(exampleIn, gEs); if(e != 0) { exampleNumber++; } else { /* if example file is empty reset it. if stdin, then stop */ if(gStdin) { allDone = 1; } else { fclose(exampleIn); exampleIn = fopen(dataFileName, "r"); DebugError(exampleIn == 0, "Unable to open the data file"); e = ExampleRead(exampleIn, gEs); exampleNumber = 1; } } seenTotal++; changes = 0; if(!allDone) { /* give the eg+num to each search */ for(i = 0 ; i < VLLength(activeSearches) ; i++) { MSAddExample(VLIndex(activeSearches, i), e, exampleNumber); } /* HERE maybe add to the deactivated searches to est params */ /* if chunk time then tell them all to check for winners */ if(seenTotal % gChunk == 0) { DebugMessage(1, 1, "===== check there are %d active %d inactive =====\n", VLLength(activeSearches), VLLength(inactiveSearches)); /* see if there are any changes to structure */ for(i = VLLength(activeSearches) - 1 ; i >= 0 ; i--) { gNumCheckPointBoundsUsed += MSNumActiveAlternatives(VLIndex(activeSearches, i)); if(MSIsDone(VLIndex(activeSearches, i))) { /* several things could make it done, conflicts eg */ MSFree(VLRemove(activeSearches, i)); changes = 1; } else if(MSCheckForWinner(VLIndex(activeSearches, i))) { changes = 1; /* Go through all the others and check for conflicts */ for(j = VLLength(activeSearches) - 1 ; j >= 0 ; j--) { if(j != i) { MSModelChangedHandleConflicts( VLIndex(activeSearches, j)); /* note this may force a winner or a no-decision */ } } if(MSIsDone(VLIndex(activeSearches, i))) { MSFree(VLRemove(activeSearches, i)); } else { VLAppend(pausedSearches, VLRemove(activeSearches, i)); } } } /* do another pass to remove done and inactive searches */ for(i = VLLength(activeSearches) - 1 ; i >= 0 ; i--) { if(MSIsDone(VLIndex(activeSearches, i))) { changes = 1; /* several things could make it done, conflicts eg */ MSFree(VLRemove(activeSearches, i)); } else if(!MSIsActive(VLIndex(activeSearches, i))) { changes = 1; VLAppend(pausedSearches, VLRemove(activeSearches, i)); } } if(changes) { _ActivateSearches(activeSearches, inactiveSearches, pausedSearches); changes = 0; } /* if everyone is done then stop */ if(VLLength(activeSearches) == 0) { allDone = 1; } if(_IsTimeExpired(starttime)) { allDone = 1; gFinishedByTime = 1; } } } else { /* will be allDone if reading from stdin and got no eg */ /* or if time expired */ /* tell every search to call a tie and finish up */ for(i = 0 ; i < VLLength(activeSearches) ; i++) { MSTerminateSearch(VLIndex(activeSearches, i)); } /* NOTE: Ignore the non-active searches, they won't have anything to add to the results and can just quit */ } ExampleFree(e); } /* !allDone */ /* print out final things */ /* HERE need to get: maybe p0? */ times(&endtime); learnTime = endtime.tms_utime - starttime.tms_utime; DebugMessage(1, 1, "done learning...\n"); DebugMessage(1, 1, "time %.2lfs\n", ((double)learnTime) / 100); DebugMessage(1, 1, "Total Samples: %ld\n", seenTotal); if(gOutputNet) { netOut = fopen(gNetOutput, "w"); BNWriteBIF(gCurrentNet, netOut); fclose(netOut); } else { BNWriteBIF(gCurrentNet, stdout); } BNPrintStats(gCurrentNet); DebugMessage(1, 1, " Num decisions %ld by tie %ld by win %ld\n", gNumBoundsUsed, gNumByTie, gNumByWin); DebugMessage(1, 1, " Samples in step min: %ld max: %ld avg: %ld zero# %ld\n", gMinSamplesInDecision, gMaxSamplesInDecision, gTotalSamplesInDecision / (gNumRemoved + gNumAdded), gNumZeroSamplesInDecision); DebugMessage(1, 1, " Num removed %ld, Num added %ld\n", gNumRemoved, gNumAdded); DebugMessage(1, 1, " Num used whole DB %ld\n", gNumUsedWholeDB); DebugMessage(1, 1, " Search end by win %ld in tie %ld default %ld\n", gNumCurrentByWin, gNumCurrentInTie, gNumCurrentByDefault); DebugMessage(1, 1, " Elim by conflict: cycle %ld, parameter %ld\n", gNumByCycleConflict, gNumByParameterConflict); DebugMessage(1, 1, " Elim by parent limit %ld\n", gNumByParentLimit); DebugMessage(1, 1, " Elim by parameter limit %ld\n", gNumByParameterLimit); DebugMessage(1, 1, " Elim by memory limit %ld\n", gNumByMemoryLimit); DebugMessage(1, 1, " Elim by change limit %ld\n", gNumByChangeLimit); DebugMessage(1, 1, " Num single searches too big for memory %ld\n", gNumExceededMemory); DebugMessage(1, 1, " Current effective Delta is %lf\n", gNumBoundsUsed * gDelta); DebugMessage(1, 1, " Num in checkpoint %ld adjusted delta %lf\n", gNumCheckPointBoundsUsed, (gNumBoundsUsed + gNumCheckPointBoundsUsed) * gDelta); DebugMessage(gFinishedByTime, 1, " ****** Time Expired ******\n"); DebugMessage(1, 1, " allocation %ld\n", MGetTotalAllocation()); return 0; }
OSErr EditProfilesInit(DialogPtr dialog, VOIDPTR data) { Rect r = GetDialogItemBox(dialog, EPLIST); float startdepth; CProfilesList *tlist; DepthValuesSet dvals; long i,n; OSErr err = 0; short IBMoffset; //RegisterPopTable(prefPopTable, 3); //RegisterPopUpDialog(EDIT_PROFILES_DLGID, dialog); //sharedEPDialogNonPtrFields = GetEPDialogNonPtrFields(sgWindMover);// JLM 11/25/98 memset(&sgObjects,0,sizeof(sgObjects));// JLM 12/10/98 { DepthValuesSetH dvalsh = sgDepthValuesH; sgDepthVals = new CProfilesList(sizeof(DepthValuesSet)); if(!sgDepthVals)return -1; if(sgDepthVals->IList())return -1; if(dvalsh) { // copy list to temp list n = _GetHandleSize((Handle)dvalsh)/sizeof(DepthValuesSet); for(i=0; i<n; i++) { dvals=(*dvalsh)[i]; err=sgDepthVals->AppendItem((Ptr)&dvals); if(err)return err; } } else n=0; n++; // Always have blank row at bottom err = VLNew(dialog, EPLIST, &r,n, DrawProfilesList, &sgObjects); if(err) return err; } SetDialogItemHandle(dialog,EPFRAMEINPUT,(Handle)FrameEmbossed); //SetDialogItemHandle(dialog,EPBUTTONFRAME,(Handle)FrameEmbossed); SetDialogItemHandle(dialog,EPLIST,(Handle)ProfilesListUpdate); //ShowHideDialogItem(dialog,EPBUTTONFRAME,false);//JLM, hide this frame, we have a different button arrangement r = GetDialogItemBox(dialog,EPLIST); #ifdef IBM IBMoffset = r.left; #else IBMoffset = 0; #endif r = GetDialogItemBox(dialog, EPDEPTH_LIST_LABEL); DEPTH_COL=(r.left+r.right)/2-IBMoffset; r = GetDialogItemBox(dialog, EPU_LIST_LABEL); U_COL=(r.left+r.right)/2-IBMoffset; r = GetDialogItemBox(dialog, EPV_LIST_LABEL); V_COL=(r.left+r.right)/2-IBMoffset; r = GetDialogItemBox(dialog, EPW_LIST_LABEL); W_COL=(r.left+r.right)/2-IBMoffset; r = GetDialogItemBox(dialog, EPTEMP_LIST_LABEL); TEMP_COL=(r.left+r.right)/2-IBMoffset; r = GetDialogItemBox(dialog, EPSAL_LIST_LABEL); SAL_COL=(r.left+r.right)/2-IBMoffset; // might want to use values of first set if there are any Float2EditText(dialog, EPINCREMENT,sIncrementInMeters, 0); Float2EditText(dialog, EPDEPTH,0.0, 0); Float2EditText(dialog, EPU,0.0, 0); Float2EditText(dialog, EPV,0.0, 0); //Float2EditText(dialog, EPW,0.0, 0); Float2EditText(dialog, EPTEMP,0.0, 0); Float2EditText(dialog, EPSAL,0.0, 0); //Float2EditText(dialog, EPDXDY,sCellLength, 0); //Float2EditText(dialog, EPNUMCELLS,sNumCells, 0); //SetPopSelection (dialog, EPSPEEDPOPUP, sgSpeedUnits); //MyDisplayTime(dialog,EPMONTHSPOPUP,sgStartTime); ////////// SetDialogItemHandle(dialog, EPHILITEDDEFAULT, (Handle)FrameDefault); ShowHideDialogItem(dialog,EPHILITEDDEFAULT,false);//JLM, hide this item, this dialog has no default UpdateDisplayWithCurSelection(dialog); MySelectDialogItemText(dialog, EPDEPTH, 0, 100);//JLM return 0; }
int oldMain(int argc, char *argv[]) { FILE *exampleIn; FILE *netOut; ExamplePtr e; while(!allDone) { searchStep++; DebugMessage(1, 2, "============== Search step: %d ==============\n", searchStep); DebugMessage(1, 2, " Total samples: %ld Last round: %ld\n", seenTotal, seenRound); DebugMessage(1, 2, " allocation before choices %ld\n", MGetTotalAllocation()); DebugMessage(1, 2, " best with score %f:\n", _ScoreBN(bn)); if(DebugGetMessageLevel() >= 2) { BNPrintStats(bn); } if(DebugGetMessageLevel() >= 3) { BNWriteBIF(bn, DebugGetTarget()); } netChoices = VLNew(); _GetOneStepChoicesForBN(bn, netChoices); DebugMessage(1, 2, " allocation after choices %ld there are %d\n", MGetTotalAllocation(), VLLength(netChoices)); seenRound = 0; stepDone = 0; e = ExampleRead(exampleIn, es); while(!stepDone && e != 0) { seenTotal++; seenRound++; /* put the eg in every net choice */ _OptimizedAddSample(bn, netChoices, e); /* if not batch and have seen chunk then check for winner */ if(!gDoBatch && (seenRound % gChunk == 0)) { _CompareNetsBoundFreeLoosers(bn, netChoices); if(VLLength(netChoices) == 1) { stepDone = 1; } else { DebugMessage(1, 2, " done with check seen in round %ld, %d left\n", seenRound, VLLength(netChoices)); } } ExampleFree(e); e = ExampleRead(exampleIn, es); } /* !stepDone && e != 0 */ ILAppend(samplesPerRound, seenRound); /* if examples ran out before bound kicked in pick a winner */ if(VLLength(netChoices) > 1) { DebugMessage(!gDoBatch, 1, "Ran out of data before finding winner, using current best of the remaining %d.\n", VLLength(netChoices)); _CompareNetsFreeLoosers(bn, netChoices); } /* if the winner is the current one then we are all done */ if(BNStructureEqual(bn, (BeliefNet)VLIndex(netChoices, 0))) { /* make sure to free all loosing choices and the netChoices list */ allDone = 1; } else if(gMaxSearchSteps != -1 && searchStep >= gMaxSearchSteps) { DebugMessage(1, 1, "Stopped because of search step limit\n"); allDone = 1; } /* copy all the CPTs that are only in bn into the new winner */ /* only really needed for final output but I do it for debugging too */ _UpdateCPTsForFrom((BeliefNet)VLIndex(netChoices, 0), bn); if(gOnlyEstimateParameters) { allDone = 1; } /* now check all previous winners */ /* if we detect a cycle, pick the best that happens in the period of the cycle */ if(!allDone) { for(i = 0 ; i < VLLength(previousWinners) ; i++) { if(BNStructureEqual(bn, (BeliefNet)VLIndex(previousWinners, i))) { allDone = 1; } if(allDone) { if(_ScoreBN(bn) < _ScoreBN((BeliefNet)VLIndex(previousWinners, i))) { bn = VLIndex(previousWinners, i); } } } } if(!allDone) { VLAppend(previousWinners, BNClone(bn)); _FreeUserData(bn); BNFree(bn); bn = (BeliefNet)VLRemove(netChoices, 0); _FreeUserData(bn); VLFree(netChoices); } DebugMessage(1, 2, "Current effective Delta is %lf\n", gNumBoundsUsed * gDelta); DebugMessage(1, 2, "Current observed p0 is %lf\n", gObservedP0); DebugMessage(1, 2, " allocation after all free %ld\n", MGetTotalAllocation()); /* reset file if not in stdin mode */ if(!gStdin) { fclose(exampleIn); exampleIn = fopen(fileName, "r"); DebugError(exampleIn == 0, "Unable to open the data file"); } } /* while !allDone */ DebugMessage(1, 1, "Total Samples: %ld\n", seenTotal); if(DebugGetMessageLevel() >= 1) { DebugMessage(1, 1, "Samples per round:\n"); for(i = 0 ; i < ILLength(samplesPerRound) ; i++) { DebugMessage(1, 1, "%d %d\n", i + 1, ILIndex(samplesPerRound, i)); } } allocation = MGetTotalAllocation(); printf("Final score: %f\n", _ScoreBN(bn)); printf("Final net:\n"); BNWriteBIF(bn, stdout); if(gOutputNet) { netOut = fopen(gNetOutput, "w"); BNWriteBIF(bn, netOut); fclose(netOut); } BNFree(bn); ExampleSpecFree(es); DebugMessage(1, 1, " allocation %ld\n", MGetTotalAllocation()); return 0; }