void AssignmentManager::outputRunLMs(FILE* outfile){ fprintf(outfile, "\nOptimal MPE found: %s\n\n", foundOptimalThisRun() ? "yes" : "no"); if(M_MPE){ int i; /* build heap for sorting solutions in output. */ int* order = new int[M]; struct fheap* tmp; tmp = fh_alloc(M); for(i=0; i<fh_BestMLogProbs->n; i++){ fh_insert(tmp, i+1, -runBestLogProbs[i]); } for(i=0; i<fh_BestMLogProbs->n; i++){ order[i] = fh_delete_min(tmp)-1; } // fh_free(tmp); crashes !! fprintf(outfile, " Best M Scores:\n"); for(i=0; i<fh_BestMLogProbs->n; i++){ fprintf(outfile, "%.6lf ", runBestLogProbs[order[i]]); } fprintf(outfile, "\n"); delete[] order; } else { if(outputBestMPE) outputAssignment(outfile, runBestAssignments[0]); if(fabs(get_log_score(runBestAssignments[0]) - runBestLogProb) > EPS){ fprintf(outfile, "Log probability %lf of best run assignment does not match %lf\n", get_log_score(runBestAssignments[0]), runBestLogProb); fprintf(stderr, "Log probability %lf of best run assignment does not match %lf\n", get_log_score(runBestAssignments[0]), runBestLogProb); assert(false); } } }
//#include <iostream> bool AssignmentManager::updateIfNewBest(double log_prob){ // printf("updateIfNewBest(%lf)\n", log_prob); bool gotBetter = false; // printf("LOG %.5lf\n",log_prob); if(log_prob > runBestLogProb + EPS){ // new better. gotBetter = true; runBestLogProb = log_prob; if(!M_MPE){ copyAssignment(runBestAssignments[0]); } bool debug = false; if(debug){ assert(fabs(get_log_score() - get_log_score(runBestAssignments[0]))<EPS); if(fabs(get_log_score(runBestAssignments[0]) - runBestLogProb) > EPS){ fprintf(stdout, "Log probability %lf of best run assignment does not match %lf\n", get_log_score(runBestAssignments[0]), runBestLogProb); fprintf(stderr, "Log probability %lf of best run assignment does not match %lf\n", get_log_score(runBestAssignments[0]), runBestLogProb); assert(false); } } } if(gotBetter && log_prob > overallBestLogProb + EPS){ // new global best. int solVec[num_vars]; for (int i=0; i<num_vars; ++i) solVec[i] = variables[i]->value; slsWrapper->reportSolution(log_prob, num_vars, solVec); } if(M_MPE){ // additional work. int numSolutionsInHeap = fh_BestMLogProbs->n; if( numSolutionsInHeap < M && newGoodAssignment()){ // put in until M. printf("Putting in %dth assignment with prob %lf\n", numSolutionsInHeap, log_prob); copyAssignment(runBestAssignments[numSolutionsInHeap]); runBestLogProbs[numSolutionsInHeap] = log_prob; fh_insert(fh_BestMLogProbs, numSolutionsInHeap+1, log_prob); gotBetter = true; } else { // printf("log_prob=%lf, index = %d, %lf\n", log_prob, fh_inspect(fh_BestMLogProbs), runBestLogProbs[fh_inspect(fh_BestMLogProbs)] + EPS); // printf("newGood %d\n", newGoodAssignment()?1:0); if( (log_prob > runBestLogProbs[fh_inspect(fh_BestMLogProbs)-1] + EPS) && newGoodAssignment() ){ // replace worst if strictly larger int index = fh_delete_min(fh_BestMLogProbs); printf("Removed index %d with prob %lf\n", index, runBestLogProbs[index-1]); copyAssignment(runBestAssignments[index-1]); printf("Put in index %d with prob %lf\n", index, log_prob); fh_insert(fh_BestMLogProbs, index, log_prob); runBestLogProbs[index-1] = log_prob; gotBetter = true; } } } // printf("done with updateIfNewBest\n"); return gotBetter; }
void fh_remove(fheap_t *h, int vertex_no){ // printf("heap:remove %d\n", vertex_no); fheap_node_t *cut_node = h->nodes[vertex_no]; if(cut_node == 0){ printf("FATAL ERROR IN FH_REMOVE WITHIN FIBONACCI HEAP!\n"); printf("Key %d has index 0\n", vertex_no); exit(-1); } fh_decrease_key(h, vertex_no, -1e30); if( fh_delete_min(h) != vertex_no ){ printf("FATAL ERROR IN FH_REMOVE WITHIN FIBONACCI HEAP!\n"); printf("After decreasing its key to -e30, %d is not the smallest element!\nEXITING\n", vertex_no); exit(-1); }; }
long _fh_delete_min(void *h) { return fh_delete_min((fheap_t *)h); }