/* Makes a stack profile on file stackFile. */ void MakeStackProfile(void) { int i, no, sampleNo; TickList *newTick; int *sampleNoTab; float sampleTime; char idStr[100]; GraphReset(); if ((outfp = fopen((char *) &stackName, "w")) == NULL) { printf("Can not open stackfile: %s.\n", stackName); exit(-1); } sprintf(idStr, "%s - Stack profiling", name); jobstring = MallocString(idStr); sprintf(idStr, "%s", timeStr); datestring = MallocString(idStr); sampleNoTab = xmalloc(noOfSamples * sizeof(int)); no = sortSamples(sortOpt, sampleNoTab); i = 0; sampleNo = 0; for (newTick=firstTick;newTick!=NULL;newTick=newTick->nTick,i++) { if ((sampleNo < no) && (sampleNoTab[sampleNo] == i)) { if (useTickNo) { sampleTime = (float)i; } else { sampleTime = (float)newTick->time/(float)CLOCKS_PER_SEC; } /*printf("sampleNo %3d SampleTime %5.2f\n", sampleNo, sampleTime);*/ allocNewSample(sampleNo, sampleTime); storeSampleEntry(sampleNo, sampleTime, "stack", ((float)(newTick->stackUse))*4.0); storeSampleEntry(sampleNo, sampleTime, "rDesc", ((float)(newTick->regionDescUse))*4.0); sampleNo++; } } if (sampleNo != nsamples) /* These two variables have to follow each other. */ Disaster("sampleNo <> nsamples"); if ((noOfSamples >= SampleMax) && (SampleMax != nsamples)) /* If we have more than SampleMax samples, */ Disaster("noOfSamples >= SampleMax and SampleMax <> nsamples."); /* then we keep exactly SampleMax samples. */ showMax = 0; /* Do not show a maximum line. */ yLab = MallocString("bytes"); PutFile(); return; }
size_t Ransac::runRANSAC(const size_t maxIterationCount, const size_t minInlierCount, const double alarmRatio, const bool isProsacSampling, const double threshold) { const size_t availableSampleSetSize = usedSampleSize_ > 0 ? std::max(usedSampleSize_, minimalSampleSize_) : minimalSampleSize_; if (totalSampleSize_ < availableSampleSetSize) return -1; if (isProsacSampling) sortSamples(); size_t maxIteration = maxIterationCount; size_t inlierCount = 0; inlierFlags_.resize(totalSampleSize_, false); std::vector<bool> currInlierFlags(totalSampleSize_, false); std::vector<size_t> indices(availableSampleSetSize, -1); // TODO [check] >> //size_t prosacSampleCount = 10; size_t prosacSampleCount = availableSampleSetSize + 10; iteration_ = 0; while (iteration_ < maxIteration && inlierCount < minInlierCount) { // Draw a sample. if (isProsacSampling) { drawRandomSample(prosacSampleCount, availableSampleSetSize, true, indices); // This incrementing strategy is naive and simple but works just fine most of the time. if (prosacSampleCount < totalSampleSize_) ++prosacSampleCount; } else drawRandomSample(totalSampleSize_, availableSampleSetSize, false, indices); // Estimate a model. if (estimateModel(indices) && verifyModel()) { // Evaluate a model. const size_t currInlierCount = lookForInliers(currInlierFlags, threshold); if (currInlierCount > inlierCount) { const double inlierRatio = double(currInlierCount) / totalSampleSize_; const size_t newMaxIteration = (size_t)std::floor(std::log(alarmRatio) / std::log(1.0 - std::pow(inlierRatio, (double)availableSampleSetSize))); if (newMaxIteration < maxIteration) maxIteration = newMaxIteration; inlierCount = currInlierCount; inlierFlags_.swap(currInlierFlags); } } ++iteration_; } // Re-estimate with all inliers and loop until the number of inliers does not increase anymore. if (inlierCount >= minimalSampleSize_) { size_t oldInlierCount = inlierCount; do { if (!estimateModelFromInliers()) return -1; oldInlierCount = inlierCount; inlierCount = lookForInliers(inlierFlags_, threshold); } while (inlierCount > oldInlierCount); inlierCount = lookForInliers(inlierFlags_, threshold); } return inlierCount; }
size_t Ransac::runMLESAC(const size_t maxIterationCount, const size_t minInlierCount, const double alarmRatio, const bool isProsacSampling, const double inlierSquaredStandardDeviation, const double inlierThresholdProbability, const size_t maxEMIterationCount) { const size_t availableSampleSetSize = usedSampleSize_ > 0 ? std::max(usedSampleSize_, minimalSampleSize_) : minimalSampleSize_; if (totalSampleSize_ < availableSampleSetSize) return -1; if (isProsacSampling) sortSamples(); size_t maxIteration = maxIterationCount; size_t inlierCount = 0; inlierFlags_.resize(totalSampleSize_, false); std::vector<double> inlierProbs(totalSampleSize_, 0.0); double minNegativeLogLikelihood = std::numeric_limits<double>::max(); std::vector<size_t> indices(availableSampleSetSize, -1); // TODO [check] >> //size_t prosacSampleCount = 10; size_t prosacSampleCount = availableSampleSetSize + 10; iteration_ = 0; const double& eps = swl::MathConstant::EPS; while (iteration_ < maxIteration && inlierCount < minInlierCount) { // Draw a sample. if (isProsacSampling) { drawRandomSample(prosacSampleCount, availableSampleSetSize, true, indices); // This incrementing strategy is naive and simple but works just fine most of the time. if (prosacSampleCount < totalSampleSize_) ++prosacSampleCount; } else drawRandomSample(totalSampleSize_, availableSampleSetSize, false, indices); // Estimate a model. if (estimateModel(indices) && verifyModel()) { // Compute inliers' probabilities. computeInlierProbabilities(inlierProbs, inlierSquaredStandardDeviation); // EM algorithm. const double tol = swl::MathConstant::TOL_5; double gamma = 0.5, prevGamma; for (size_t i = 0; i < maxEMIterationCount; ++i) { const double outlierProb = (1.0 - gamma) * inlierThresholdProbability; double sumInlierProb = 0.0; for (size_t k = 0; k < totalSampleSize_; ++k) { const double inlierProb = gamma * inlierProbs[k]; sumInlierProb += inlierProb / (inlierProb + outlierProb); } prevGamma = gamma; gamma = sumInlierProb / totalSampleSize_; if (std::abs(gamma - prevGamma) < tol) break; } // Evaluate a model. const double outlierProb = (1.0 - gamma) * inlierThresholdProbability; double negativeLogLikelihood = 0.0; for (size_t k = 0; k < totalSampleSize_; ++k) negativeLogLikelihood -= std::log(gamma * inlierProbs[k] + outlierProb); // Negative log likelihood. if (negativeLogLikelihood < minNegativeLogLikelihood) { const double denom = std::log(1.0 - std::pow(gamma, (double)availableSampleSetSize)); if (std::abs(denom) > eps) { const size_t newMaxIteration = (size_t)std::floor(std::log(alarmRatio) / denom); if (newMaxIteration < maxIteration) maxIteration = newMaxIteration; } inlierCount = lookForInliers(inlierFlags_, inlierProbs, inlierThresholdProbability); minNegativeLogLikelihood = negativeLogLikelihood; } } ++iteration_; } // Re-estimate with all inliers and loop until the number of inliers does not increase anymore. if (inlierCount >= minimalSampleSize_) { size_t oldInlierCount = 0; do { if (!estimateModelFromInliers()) return inlierCount; // Compute inliers' probabilities. computeInlierProbabilities(inlierProbs, inlierSquaredStandardDeviation); oldInlierCount = inlierCount; inlierCount = lookForInliers(inlierFlags_, inlierProbs, inlierThresholdProbability); } while (inlierCount > oldInlierCount); // Compute inliers' probabilities. computeInlierProbabilities(inlierProbs, inlierSquaredStandardDeviation); inlierCount = lookForInliers(inlierFlags_, inlierProbs, inlierThresholdProbability); } return inlierCount; }
/* Makes a region profile. */ void MakeObjectProfile(int region) { int i, sampleNo, no; TickList *newTick; ObjectList *newObj; RegionList *newRegion; int *sampleNoTab; float sampleTime; char idStr[100]; int success = 0; GraphReset(); if ((outfp = fopen((char *) &objName, "w")) == NULL) { printf("Cannot open output file %s.\n", objName); exit(-1); } sprintf(idStr, "%s - Object profiling on region %d", name, region); jobstring = MallocString(idStr); sprintf(idStr, "%s", timeStr); datestring = MallocString(idStr); sampleNoTab = xmalloc(noOfSamples * sizeof(int)); no = sortSamples(sortOpt, sampleNoTab); i = 0; sampleNo = 0; for (newTick=firstTick;newTick!=NULL;newTick=newTick->nTick,i++) { if ((sampleNo < no) && (sampleNoTab[sampleNo] == i)) { if (useTickNo) { sampleTime = (float)i; } else { sampleTime = (float)newTick->time/(float)CLOCKS_PER_SEC; } /*printf("sampleNo %3d SampleTime %5.2f\n", sampleNo, sampleTime);*/ allocNewSample(sampleNo, sampleTime); for (newRegion=newTick->fRegion;newRegion!=NULL;newRegion=newRegion->nRegion) { if (newRegion->regionId == region) for (newObj=newRegion->fObj;newObj!=NULL;newObj=newObj->nObj) { success = 1; sprintf(idStr, "pp%d", newObj->atId); storeSampleEntry(sampleNo, sampleTime, idStr, ((float)(newObj->size))*4.0); } } sampleNo++; } } if (success == 0) { printf("There is no profiling information for region r%d, so I could not \ncreate a PostScript file for you.\n", region); exit(-1); } if (sampleNo != nsamples) /* These two variables have to follow each other. */ Disaster("sampleNo <> nsamples"); if ((noOfSamples >= SampleMax) && (SampleMax != nsamples)) /* If we have more than SampleMax samples, */ Disaster("noOfSamples >= SampleMax and SampleMax <> nsamples."); /* then we keep exactly SampleMax samples. */ showMax = 1; maxValue = profTabGetMaxAlloc(region)*4; sprintf(maxValueStr, "Maximum allocated bytes in this region: %2.0f.", maxValue); yLab = MallocString("bytes"); PutFile(); return; }
/* Makes a region profile. */ void MakeRegionProfile(void) { int i, sampleNo, no; TickList *newTick; RegionList *newRegion; int *sampleNoTab; float sampleTime; float maxStack = 0.0; char idStr[100]; GraphReset(); if ((outfp = fopen((char *) &rpName, "w")) == NULL) { printf("Can not open output file %s.\n", rpName); exit(-1); } sprintf(idStr, "%s - Region profiling", name); jobstring = MallocString(idStr); sprintf(idStr, "%s", timeStr); datestring = MallocString(idStr); sampleNoTab = xmalloc(noOfSamples * sizeof(int)); no = sortSamples(sortOpt, sampleNoTab); i = 0; sampleNo = 0; for (newTick=firstTick;newTick!=NULL;newTick=newTick->nTick,i++) { if ((sampleNo < no) && (sampleNoTab[sampleNo] == i)) { if (useTickNo) { sampleTime = (float)i; } else { sampleTime = (float)newTick->time/(float)CLOCKS_PER_SEC; } /*printf("sampleNo %3d SampleTime %5.2f\n", sampleNo, sampleTime);*/ allocNewSample(sampleNo, sampleTime); storeSampleEntry(sampleNo, sampleTime, "stack", ((float)(newTick->stackUse))*4.0); storeSampleEntry(sampleNo, sampleTime, "rDesc", ((float)(newTick->regionDescUse))*4.0); if (((((float)(newTick->stackUse))+((float)(newTick->regionDescUse)))*4.0) > maxStack) /* To ajust the max. */ maxStack = (((float)(newTick->stackUse))+((float)(newTick->regionDescUse)))*4.0; /* allocation line. */ for (newRegion=newTick->fRegion;newRegion!=NULL;newRegion=newRegion->nRegion) { if (newRegion->infinite) sprintf(idStr, "r%dinf", newRegion->regionId); else sprintf(idStr, "r%dfin", newRegion->regionId); storeSampleEntry(sampleNo, sampleTime, idStr, ((float)(newRegion->used))*4.0); } sampleNo++; } } if (sampleNo != nsamples) /* These two variables have to follow each other. */ Disaster("sampleNo <> nsamples"); if ((noOfSamples >= SampleMax) && (SampleMax != nsamples)) /* If we have more than SampleMax samples, */ Disaster("noOfSamples >= SampleMax and SampleMax <> nsamples."); /* then we keep exactly SampleMax samples. */ showMax = 1; maxValue = maxRegions*4; /* The total memory used is often lower than what the line suggests * because the time the stack is maximal may not be the same time * that allocation in regions is maximal! mael 2001-05-22 */ sprintf(maxValueStr, "Maximum allocated bytes in regions (%2.0f) and on stack (%2.0f)", maxValue, maxStack); maxValue += maxStack; /* Ajusting the max. allocation line. */ yLab = MallocString("bytes"); PutFile(); return; }