📄 vfbn2.c
字号:
netOut = fopen(gNetOutput, "w");
BNWriteBIF(bn, netOut);
fclose(netOut);
}
BNFree(bn);
ExampleSpecFree(es);
DebugMessage(1, 1, " allocation %ld\n", MGetTotalAllocation());
return 0;
}
#endif
void _AllocFailed(int allocationSize) {
printf("MEMORY ALLOCATION FAILED, size %d\n", allocationSize);
}
static void _InitializeGlobals(int argc, char *argv[]) {
char fileName[255];
_processArgs(argc, argv);
MSetAllocFailFunction(_AllocFailed);
if(gUseStartingNet) {
gPriorNet = BNReadBIF(gStartingNetFile);
if(gPriorNet == 0) {
DebugError(1, "couldn't read net specified by -startFrom\n");
}
gEs = BNGetExampleSpec(gPriorNet);
} else {
sprintf(fileName, "%s/%s.names", gSourceDirectory, gFileStem);
gEs = ExampleSpecRead(fileName);
DebugError(gEs == 0, "Unable to open the .names file");
gPriorNet = BNNewFromSpec(gEs);
}
gInitialParameterCount = BNGetNumParameters(gPriorNet);
gBranchFactor = BNGetNumNodes(gPriorNet) * BNGetNumNodes(gPriorNet);
if(gLimitBytes != -1) {
gMaxBytesPerModel = gLimitBytes / BNGetNumNodes(gPriorNet);
//gMaxBytesPerModel = gLimitBytes / gBranchFactor;
DebugMessage(1, 2, "Limit models to %.4lf megs\n",
gMaxBytesPerModel / (1024.0 * 1024.0));
}
gCurrentNet = BNClone(gPriorNet);
BNZeroCPTs(gCurrentNet);
RandomInit();
/* seed */
if(gSeed != -1) {
RandomSeed(gSeed);
} else {
gSeed = RandomRange(1, 30000);
RandomSeed(gSeed);
}
DebugMessage(1, 1, "running with seed %d\n", gSeed);
DebugMessage(1, 1, "allocation %ld\n", MGetTotalAllocation());
DebugMessage(1, 1, "initial parameters %ld\n", gInitialParameterCount);
}
static int _OverMemoryLimit(void) {
return (gLimitBytes != -1) && (MGetTotalAllocation() > gLimitBytes);
}
static void _ActivateSearches(VoidListPtr active, VoidListPtr deactive, VoidListPtr paused) {
/* HERE this will activate too many and not be efficient,
would be better if we could ask the searches how much
memory they think they are gonna take */
if(!gInStep) {
while(VLLength(paused) > 0) {
VLAppend(deactive, VLRemove(paused, 0));
}
}
DebugMessage(1, 3, "activate current allocation %ld\n",
MGetTotalAllocation());
while(!_OverMemoryLimit() && VLLength(deactive) > 0) {
MSActivate(VLIndex(deactive, 0));
VLAppend(active, VLRemove(deactive, 0));
DebugMessage(1, 3, " another active now allocation %ld\n",
MGetTotalAllocation());
}
while(_OverMemoryLimit() && VLLength(active) > 1) {
MSDeactivate(VLIndex(active, VLLength(active) - 1));
VLPush(deactive, VLRemove(active, VLLength(active) - 1));
DebugMessage(1, 3,
" deactivated last now allocation %ld\n",
MGetTotalAllocation());
}
if(_OverMemoryLimit() && VLLength(active) > 0) {
DebugMessage(1, 1, "*** Memory limit exceeded by search on node %d\n",
MSGetNodeID(VLIndex(active, 0)));
gNumExceededMemory++;
DebugMessage(1, 3, " single search too large allocation %ld\n",
MGetTotalAllocation());
}
if(VLLength(active) == 0 && VLLength(deactive) == 0 &&
VLLength(paused) > 0) {
while(VLLength(paused) > 0) {
VLAppend(deactive, VLRemove(paused, 0));
}
_ActivateSearches(active, deactive, paused);
}
}
static int _IsTimeExpired(struct tms starttime) {
struct tms endtime;
long seconds;
if(gLimitSeconds != -1) {
times(&endtime);
seconds = (double)(endtime.tms_utime - starttime.tms_utime) / 100.0;
return seconds >= gLimitSeconds;
}
return 0;
}
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -