int GetHWInfo(const char *confile, char *info) { FILE *fp = NULL; if (NULL == confile || NULL == info) { return -1; } if ((fp = fopen(confile, "r")) == NULL) { mxLog("Can not open confile!"); return -1; } fgets(info, MAXHWINFOLEN, fp); //clean up string for(unsigned int i = 0; i < strlen(info); i++) { if( '\n' == info[i]) { info[i] = '\0'; } } if (fclose(fp) < 0) { mxLog("ERROR: Close confile failed!"); return -1; } return 0; }
omxState::~omxState() { if(OMX_DEBUG) { mxLog("Freeing %d Constraints.", (int) conListX.size());} for(int k = 0; k < (int) conListX.size(); k++) { delete conListX[k]; } for(size_t ax = 0; ax < algebraList.size(); ax++) { // free argument tree omxFreeMatrix(algebraList[ax]); } for(size_t ax = 0; ax < algebraList.size(); ax++) { algebraList[ax]->hasMatrixNumber = false; omxFreeMatrix(algebraList[ax]); } if(OMX_DEBUG) { mxLog("Freeing %d Matrices.", (int) matrixList.size());} for(size_t mk = 0; mk < matrixList.size(); mk++) { matrixList[mk]->hasMatrixNumber = false; omxFreeMatrix(matrixList[mk]); } if(OMX_DEBUG) { mxLog("Freeing %d Model Expectations.", (int) expectationList.size());} for(size_t ex = 0; ex < expectationList.size(); ex++) { omxFreeExpectationArgs(expectationList[ex]); } }
void omxInitNormalExpectation(omxExpectation* ox) { SEXP rObj = ox->rObj; omxState* currentState = ox->currentState; if(OMX_DEBUG) { mxLog("Initializing Normal expectation."); } omxNormalExpectation *one = (omxNormalExpectation*) R_alloc(1, sizeof(omxNormalExpectation)); /* Set Expectation Calls and Structures */ ox->computeFun = omxComputeNormalExpectation; ox->destructFun = omxDestroyNormalExpectation; ox->componentFun = omxGetNormalExpectationComponent; ox->populateAttrFun = omxPopulateNormalAttributes; ox->argStruct = (void*) one; /* Set up expectation structures */ if(OMX_DEBUG) { mxLog("Processing cov."); } one->cov = omxNewMatrixFromSlot(rObj, currentState, "covariance"); if(OMX_DEBUG) { mxLog("Processing Means."); } one->means = omxNewMatrixFromSlot(rObj, currentState, "means"); one->thresholds = omxNewMatrixFromSlot(rObj, currentState, "thresholds"); }
/*********************************** *Name: CheckUpdate *Usage: *Input: bfUpdateJson: Json file in mem *Output: N/A *Return: 0: need update. -1: no update *Comment: **************************************/ int DownloadResourceFile(const char *url, const char* pathname) { char md5[33] = {0}; char command[MAXURL] = {0}; if (NULL == url || NULL == pathname) { mxLog("url or pathname is NULL"); return -1; } strcat(command, "wget "); strcat(command, (const char *)url); strcat(command, " -O "); strcat(command, (const char *)pathname); mxLog("DOWNLOAD command: %s", command); //download if ( 0 != system(command)) { mxLog("Download Resource File Failed"); } //verify GetMD5FromJsonPart(pathname, md5); return CheckMD5(pathname, md5); }
void omxRaiseErrorf(const char* msg, ...) { std::string str; va_list ap; va_start(ap, msg); string_vsnprintf(msg, ap, str); va_end(ap); _omxRaiseError(); if(OMX_DEBUG) { mxLog("Error raised: %s", str.c_str()); } bool overflow = false; #pragma omp critical(bads) { if (Global->bads.size() > 100) { overflow = true; } else { Global->bads.push_back(str); } } // mxLog takes a lock too, so call it outside of critical section if (overflow) mxLog("Too many errors: %s", str.c_str()); }
int ReportUpdateFirewareFailed(mx_Data * pD) { char *jsonFormat = "{\"type\":\"0x000c\",\"cfv\":\"%s\"}\0"; char *json = (char *) malloc (strlen(jsonFormat) - 2 + strlen(pD->ngxData.localVersion) + 1); if(NULL == json) { return -1; mxLog("malloc failed"); } sprintf(json, jsonFormat, pD->ngxData.localVersion); mxLog("Report to bf's json is:%s", json); //send to bf if(0 != ReportJson(atoi(pD->reportPort), pD->reportDomain, pD->reportPath, pD->encryptMAC, json)) { mxLog("Report type=0x000c to bf failed"); return -1; } free(json); mxLog("Report type=0x000c to bf success"); return 0; }
void omxSadmvnWrapper(omxFitFunction *oo, omxMatrix *cov, omxMatrix *ordCov, double *corList, double *lThresh, double *uThresh, int *Infin, double *likelihood, int *inform) { // SADMVN calls Alan Genz's sadmvn.f--see appropriate file for licensing info. // TODO: Check with Genz: should we be using sadmvn or sadmvn? // Parameters are: // N int # of vars // Lower double* Array of lower bounds // Upper double* Array of upper bounds // Infin int* Array of flags: 0 = (-Inf, upper] 1 = [lower, Inf), 2 = [lower, upper] // Correl double* Array of correlation coeffs: in row-major lower triangular order // MaxPts int Maximum # of function values (use 1000*N or 1000*N*N) // Abseps double Absolute Rf_error tolerance. Yick. // Releps double Relative Rf_error tolerance. Use EPSILON. // Error &double On return: absolute real Rf_error, 99% confidence // Value &double On return: evaluated value // Inform &int On return: 0 = OK; 1 = Rerun, increase MaxPts; 2 = Bad input // TODO: Separate block diagonal covariance matrices into pieces for integration separately double Error; double absEps = Global->absEps; double relEps = Global->relEps; int MaxPts = Global->maxptsa + Global->maxptsb * cov->rows + Global->maxptsc * cov->rows * cov->rows; int numVars = ordCov->rows; int fortranThreadId = omx_absolute_thread_num() + 1; /* FOR DEBUGGING PURPOSES */ /* numVars = 2; lThresh[0] = -2; uThresh[0] = -1.636364; Infin[0] = 2; lThresh[1] = 0; uThresh[1] = 0; Infin[1] = 0; smallCor[0] = 1.0; smallCor[1] = 0; smallCor[2] = 1.0; */ F77_CALL(sadmvn)(&numVars, lThresh, uThresh, Infin, corList, &MaxPts, &absEps, &relEps, &Error, likelihood, inform, &fortranThreadId); if(OMX_DEBUG && !oo->matrix->currentState->currentRow) { char infinCodes[3][20]; strcpy(infinCodes[0], "(-INF, upper]"); strcpy(infinCodes[1], "[lower, INF)"); strcpy(infinCodes[2], "[lower, upper]"); mxLog("Input to sadmvn is (%d rows):", numVars); //:::DEBUG::: omxPrint(ordCov, "Ordinal Covariance Matrix"); //:::DEBUG::: for(int i = 0; i < numVars; i++) { mxLog("Row %d: %f, %f, %d(%s)", i, lThresh[i], uThresh[i], Infin[i], infinCodes[Infin[i]]); } mxLog("Cor: (Lower %d x %d):", cov->rows, cov->cols); //:::DEBUG::: for(int i = 0; i < cov->rows*(cov->rows-1)/2; i++) { // mxLog("Row %d of Cor: ", i); // for(int j = 0; j < i; j++) mxLog(" %f", corList[i]); // (i*(i-1)/2) + j]); // mxLog(""); } } if(OMX_DEBUG) { mxLog("Output of sadmvn is %f, %f, %d.", Error, *likelihood, *inform); } }
void omxExpectation::loadThresholds() { bool debug = false; CheckAST(thresholdsMat, 0); numOrdinal = 0; //omxPrint(thresholdsMat, "loadThr"); auto dc = base::getDataColumns(); thresholds.reserve(dc.size()); std::vector<bool> found(thresholdsMat->cols, false); for(int dx = 0; dx < int(dc.size()); dx++) { int index = dc[dx]; omxThresholdColumn col; col.dColumn = index; const char *colname = data->columnName(index); int tc = thresholdsMat->lookupColumnByName(colname); if (tc < 0 || (data->rawCols.size() && !omxDataColumnIsFactor(data, index))) { // Continuous variable if(debug || OMX_DEBUG) { mxLog("%s: column[%d] '%s' is continuous (tc=%d isFactor=%d)", name, index, colname, tc, omxDataColumnIsFactor(data, index)); } thresholds.push_back(col); } else { found[tc] = true; col.column = tc; if (data->rawCols.size()) { col.numThresholds = omxDataGetNumFactorLevels(data, index) - 1; } else { // See omxData::permute } thresholds.push_back(col); if(debug || OMX_DEBUG) { mxLog("%s: column[%d] '%s' is ordinal with %d thresholds in threshold column %d.", name, index, colname, col.numThresholds, tc); } numOrdinal++; } } if (numOrdinal != thresholdsMat->cols) { std::string buf; for (int cx=0; cx < thresholdsMat->cols; ++cx) { if (found[cx]) continue; buf += string_snprintf(" %d", 1+cx); } omxRaiseErrorf("%s: cannot find data for threshold columns:%s\n(Do appropriate threshold column names match data column names?)", name, buf.c_str()); } if(debug || OMX_DEBUG) { mxLog("%d threshold columns processed.", numOrdinal); } }
static void nloptEqualityFunction(unsigned m, double* result, unsigned n, const double* x, double* grad, void* f_data) { context &ctx = *(context *)f_data; GradientOptimizerContext &goc = ctx.goc; assert(n == goc.fc->numParam); Eigen::Map< Eigen::VectorXd > Epoint((double*)x, n); Eigen::VectorXd Eresult(ctx.origeq); Eigen::MatrixXd jacobian(n, ctx.origeq); equality_functional ff(goc); ff(Epoint, Eresult); if (grad) { fd_jacobian(goc.gradientAlgo, goc.gradientIterations, goc.gradientStepSize, ff, Eresult, Epoint, jacobian); if (ctx.eqmask.size() == 0) { ctx.eqmask.assign(m, false); for (int c1=0; c1 < int(m-1); ++c1) { for (int c2=c1+1; c2 < int(m); ++c2) { bool match = (Eresult[c1] == Eresult[c2] && (jacobian.col(c1) == jacobian.col(c2))); if (match && !ctx.eqmask[c2]) { ctx.eqmask[c2] = match; ++ctx.eqredundent; if (goc.verbose >= 2) { mxLog("nlopt: eq constraint %d is redundent with %d", c1, c2); } } } } if (ctx.eqredundent) { if (goc.verbose >= 1) { mxLog("nlopt: detected %d redundent equality constraints; retrying", ctx.eqredundent); } nlopt_opt opt = (nlopt_opt) goc.extraData; nlopt_force_stop(opt); } } } Eigen::Map< Eigen::VectorXd > Uresult(result, m); Eigen::Map< Eigen::MatrixXd > Ujacobian(grad, n, m); int dx=0; for (int cx=0; cx < int(m); ++cx) { if (ctx.eqmask[cx]) continue; Uresult[dx] = Eresult[cx]; if (grad) { Ujacobian.col(dx) = jacobian.col(cx); } ++dx; } if (goc.verbose >= 4 && grad) { mxPrintMat("eq result", Uresult); mxPrintMat("eq jacobian", Ujacobian); } }
/*********************************** *Name: EncryptMAC *Usage: *Input: *Output: *Return: 0: success. -1: fail *Comment: **************************************/ int EncryptMAC(char *mac, char *EncryptMAC) { unsigned char keys[13] = {0xcc, 0xcb, 0xa1, 0xfe, 0xaf, 0x9a, 0x8b, 0x33, 0xde, 0xee, 0x52, 0xff, 0x00}; unsigned char result[12] = {0}; unsigned char randValue[4]; unsigned char finalValue[16]; if (NULL == mac || NULL == EncryptMAC || 12 != strlen(mac)) { mxLog("char * is NULL"); return -1; } mxLog("keys=%s", keys); //step A; for (int i = 0; i < 12; i++) { result[i] = mac[i] ^ keys[i]; //mxLog("result=%X, mac=%X, key=%X, i=%X", result[i],mac[i], keys[i], i); } //Step B; // rand result max is 0x7FFF srand((unsigned int)time(NULL)); randValue[0] = (unsigned char)rand(); randValue[1] = (unsigned char)rand(); randValue[2] = (unsigned char)rand(); randValue[3] = (unsigned char)rand(); //Step C for (int i = 0; i < 3; i++) //12 /4 = 3 times { for(int j = 0; j < 4; j++) { result[i*4 + j] = randValue[j] ^ result[i*4 + j]; //mxLog("result=%X, randV=%X, index=%d", result[i*4 + j],randValue[j], i*4 + j); } } //Step D memcpy(finalValue, randValue, 4); memcpy(finalValue+4, result, 12); //Step E for(int i = 0; i < 16; i++) { OneByteToTwoChar(finalValue[i], EncryptMAC); EncryptMAC += 2; //mxLog("finalValue[i]=0x%X, EncryptMAC=%c and %c", finalValue[i], *EncryptMAC, *(EncryptMAC+1)); } *EncryptMAC = '\0'; return 0; }
int MoveFile(const char * srcNamePath, const char * targetNamePath) { char command[MAXURL] = {0}; char temppath[MAXURL] = {0}; if (NULL == srcNamePath || NULL == targetNamePath ) { mxLog("srcNamePath or targetNamePath is NULL"); return -1; } memset(temppath, 0, sizeof(temppath)); strcpy(temppath, (const char *)targetNamePath); // mxLog("temppath before: %s",temppath); for(int i=strlen(temppath); i>0;i--) { if(temppath[i-1] == '/') { temppath[i-1] = '\0'; break; } } // mxLog("temppath after: %s",temppath); // prepare dir memset(command, 0, sizeof(command)); strcat(command, "mkdir -p "); strcat(command, temppath); mxLog("Mkdir command is: %s", command); if ( 0 != system(command)) { mxLog("Make Directory Failed"); } //mkdir -p root/Folder/iso/bsldata/76/be/765564bb2f3787c6d00964697383e1be // move file memset(command, 0, sizeof(command)); strcat(command, "mv -f "); strcat(command, (const char *)srcNamePath); strcat(command, " "); strcat(command, (const char *)targetNamePath); mxLog("MoveFile command: %s", command); if ( 0 != system(command)) { mxLog("Move welcome.zip Failed"); } return 0; }
int ReportResourceDownloadFailed(mx_Data *pD) { char json[] = "{\"type\":\"0x0004\"}\0"; mxLog("Report to bf's json is:%s", json); //send to bf if(0 != ReportJson(atoi(pD->reportPort), pD->reportDomain, pD->reportPath, pD->encryptMAC, json)) { mxLog("Report type=0x0004 to bf failed"); return -1; } mxLog("Report type=0x0004 to bf success"); return 0; }
/*********************************** *Name: GetID *Usage: to get bapfeng net Version *Input: void *Output: mac string *Return: 0: success. -1: fail *Comment: **************************************/ int GetID(const char *IDFile, char *id) { FILE * fp = NULL; char * buf = NULL; long bufsize = 0; if (NULL == IDFile || NULL == id) { mxLog("IDFile or id is NULL"); return -1; } bufsize = getFileSize(IDFile) +1; buf = (char *)malloc(bufsize); memset(buf, 0, bufsize); if(0==bufsize || NULL ==buf) { mxLog("bufsize or buf is NULL"); return -1; } if ((fp = fopen(IDFile, "r")) == NULL) { free(buf); mxLog("open IDFile failed"); return -1; } fgets(buf, bufsize, fp); char *p = buf; while ('\0' != *p && *p != '\n') { *id++ = *p++; } mxLog("GetCurrentVersion : %s", buf); if (fclose(fp) < 0) { mxLog("ERROR: Close confile failed!"); free(buf); return -1; } free(buf); return 0; }
void omxPopulateFIMLAttributes(omxFitFunction *off, SEXP algebra) { if(OMX_DEBUG) { mxLog("Populating FIML Attributes."); } omxFIMLFitFunction *argStruct = ((omxFIMLFitFunction*)off->argStruct); SEXP expCovExt, expMeanExt, rowLikelihoodsExt; omxMatrix *expCovInt, *expMeanInt; expCovInt = argStruct->cov; expMeanInt = argStruct->means; Rf_protect(expCovExt = Rf_allocMatrix(REALSXP, expCovInt->rows, expCovInt->cols)); for(int row = 0; row < expCovInt->rows; row++) for(int col = 0; col < expCovInt->cols; col++) REAL(expCovExt)[col * expCovInt->rows + row] = omxMatrixElement(expCovInt, row, col); if (expMeanInt != NULL && expMeanInt->rows > 0 && expMeanInt->cols > 0) { Rf_protect(expMeanExt = Rf_allocMatrix(REALSXP, expMeanInt->rows, expMeanInt->cols)); for(int row = 0; row < expMeanInt->rows; row++) for(int col = 0; col < expMeanInt->cols; col++) REAL(expMeanExt)[col * expMeanInt->rows + row] = omxMatrixElement(expMeanInt, row, col); } else { Rf_protect(expMeanExt = Rf_allocMatrix(REALSXP, 0, 0)); } Rf_setAttrib(algebra, Rf_install("expCov"), expCovExt); Rf_setAttrib(algebra, Rf_install("expMean"), expMeanExt); if(argStruct->populateRowDiagnostics){ omxMatrix *rowLikelihoodsInt = argStruct->rowLikelihoods; Rf_protect(rowLikelihoodsExt = Rf_allocVector(REALSXP, rowLikelihoodsInt->rows)); for(int row = 0; row < rowLikelihoodsInt->rows; row++) REAL(rowLikelihoodsExt)[row] = omxMatrixElement(rowLikelihoodsInt, row, 0); Rf_setAttrib(algebra, Rf_install("likelihoods"), rowLikelihoodsExt); } }
void omxGlobal::reportProgress(const char *context, FitContext *fc) { if (omx_absolute_thread_num() != 0) { mxLog("omxGlobal::reportProgress called in a thread context (report this bug to developers)"); return; } R_CheckUserInterrupt(); time_t now = time(0); if (Global->maxSeconds > 0 && now > Global->startTime + Global->maxSeconds && !Global->timedOut) { Global->timedOut = true; Rf_warning("Time limit of %d minutes %d seconds exceeded", Global->maxSeconds/60, Global->maxSeconds % 60); } if (silent || now - lastProgressReport < 1 || fc->getGlobalComputeCount() == previousComputeCount) return; lastProgressReport = now; std::string str; if (previousReportFit == 0.0 || previousReportFit == fc->fit) { str = string_snprintf("%s %d %.6g", context, fc->getGlobalComputeCount(), fc->fit); } else { str = string_snprintf("%s %d %.6g %.4g", context, fc->getGlobalComputeCount(), fc->fit, fc->fit - previousReportFit); } reportProgressStr(str.c_str()); previousReportLength = str.size(); previousReportFit = fc->fit; previousComputeCount = fc->getGlobalComputeCount(); }
void omxCallLISRELExpectation(omxExpectation* oo, FitContext *fc, const char *, const char *) { if(OMX_DEBUG) { mxLog("LISREL Expectation Called."); } omxLISRELExpectation* oro = (omxLISRELExpectation*)(oo->argStruct); omxRecompute(oro->LX, fc); omxRecompute(oro->LY, fc); omxRecompute(oro->BE, fc); omxRecompute(oro->GA, fc); omxRecompute(oro->PH, fc); omxRecompute(oro->PS, fc); omxRecompute(oro->TD, fc); omxRecompute(oro->TE, fc); omxRecompute(oro->TH, fc); if(oro->TX != NULL) { // Update means? omxRecompute(oro->TX, fc); omxRecompute(oro->KA, fc); } if(oro->TY != NULL) { omxRecompute(oro->TY, fc); omxRecompute(oro->AL, fc); } omxCalculateLISRELCovarianceAndMeans(oro); }
static void nloptInequalityFunction(unsigned m, double *result, unsigned n, const double* x, double* grad, void* f_data) { GradientOptimizerContext *goc = (GradientOptimizerContext *) f_data; assert(n == goc->fc->numParam); Eigen::Map< Eigen::VectorXd > Epoint((double*)x, n); Eigen::Map< Eigen::VectorXd > Eresult(result, m); Eigen::Map< Eigen::MatrixXd > jacobian(grad, n, m); if (grad && goc->verbose >= 2) { if (m == 1) { mxLog("major iteration ineq=%.12f", Eresult[0]); } else { mxPrintMat("major iteration ineq", Eresult); } } inequality_functional ff(*goc); ff(Epoint, Eresult); if (grad) { fd_jacobian(goc->gradientAlgo, goc->gradientIterations, goc->gradientStepSize, ff, Eresult, Epoint, jacobian); if (!std::isfinite(Eresult.sum())) { // infeasible at start of major iteration nlopt_opt opt = (nlopt_opt) goc->extraData; nlopt_force_stop(opt); } } if (goc->verbose >= 3 && grad) { mxPrintMat("inequality jacobian", jacobian); } }
void omxExpectationPrint(omxExpectation* ox, char* d) { if(ox->printFun != NULL) { ox->printFun(ox); } else { mxLog("(Expectation, type %s) ", (ox->expType==NULL?"Untyped":ox->expType)); } }
static void setLatentStartingValues(omxFitFunction *oo, FitContext *fc) //remove? TODO { BA81FitState *state = (BA81FitState*) oo->argStruct; BA81Expect *estate = (BA81Expect*) oo->expectation->argStruct; std::vector<int> &latentMap = state->latentMap; ba81NormalQuad &quad = estate->getQuad(); int maxAbilities = quad.maxAbilities; omxMatrix *estMean = estate->estLatentMean; omxMatrix *estCov = estate->estLatentCov; for (int a1 = 0; a1 < maxAbilities; ++a1) { if (latentMap[a1] >= 0) { int to = latentMap[a1]; fc->est[to] = omxVectorElement(estMean, a1); } for (int a2 = 0; a2 <= a1; ++a2) { int to = latentMap[maxAbilities + triangleLoc1(a1) + a2]; if (to < 0) continue; fc->est[to] = omxMatrixElement(estCov, a1, a2); } } if (estate->verbose >= 1) { mxLog("%s: set latent parameters for version %d", oo->name(), estate->ElatentVersion); } }
/*********************************** *Name: OneByteToTwoChar *Usage: *Input: *Output: *Return: 0: success. -1: fail *Comment: **************************************/ int OneByteToTwoChar(unsigned char value, char *twoChar) { if (NULL == twoChar) { mxLog("towChar is NULL"); return -1; } //char 1 unsigned char ch = (unsigned char)((value & 0xF0) >> 4 ) & 0x0F; if( 9 >= ch ) // 0~9 { *(twoChar) = ch + '0'; } if( 0xA <= ch && ch <= 0xF ) // a~f { *(twoChar) = ch + 'a' - 10; } //get char 2 ch = (unsigned char)(value & 0x0F); if( 9 >= ch ) // 0~9 { *(twoChar+1) = ch + '0'; } if( 0xA <= ch && ch <= 0xF ) // a~f { *(twoChar+1) = ch + 'a' - 10; } //get 2 char return 0; }
static double nloptObjectiveFunction(unsigned n, const double *x, double *grad, void *f_data) { GradientOptimizerContext *goc = (GradientOptimizerContext *) f_data; nlopt_opt opt = (nlopt_opt) goc->extraData; FitContext *fc = goc->fc; assert(n == fc->numParam); int mode = 0; double fit = goc->solFun((double*) x, &mode); if (grad) { fc->iterations += 1; if (goc->maxMajorIterations != -1 && fc->iterations >= goc->maxMajorIterations) { nlopt_force_stop(opt); } } if (grad && goc->verbose >= 2) { mxLog("major iteration fit=%.12f", fit); } if (mode == -1) { if (!goc->feasible) { nlopt_force_stop(opt); } return nan("infeasible"); } if (!grad) return fit; Eigen::Map< Eigen::VectorXd > Epoint((double*) x, n); Eigen::Map< Eigen::VectorXd > Egrad(grad, n); if (fc->wanted & FF_COMPUTE_GRADIENT) { Egrad = fc->grad; } else if (fc->CI && fc->CI->varIndex >= 0) { Egrad.setZero(); Egrad[fc->CI->varIndex] = fc->lowerBound? 1 : -1; fc->grad = Egrad; } else { if (goc->verbose >= 3) mxLog("fd_gradient start"); fit_functional ff(*goc); gradient_with_ref(goc->gradientAlgo, goc->gradientIterations, goc->gradientStepSize, ff, fit, Epoint, Egrad); fc->grad = Egrad; } if (goc->verbose >= 3) { mxPrintMat("gradient", Egrad); } return fit; }
static void ba81Destroy(omxExpectation *oo) { if(OMX_DEBUG) { mxLog("Freeing %s function.", oo->name); } BA81Expect *state = (BA81Expect *) oo->argStruct; omxFreeMatrix(state->estLatentMean); omxFreeMatrix(state->estLatentCov); delete state; }
void FitMultigroup::compute(int want, FitContext *fc) { omxMatrix *fitMatrix = matrix; double fit = 0; double mac = 0; FitMultigroup *mg = (FitMultigroup*) this; for (size_t ex=0; ex < mg->fits.size(); ex++) { omxMatrix* f1 = mg->fits[ex]; if (f1->fitFunction) { omxFitFunctionCompute(f1->fitFunction, want, fc); if (want & FF_COMPUTE_MAXABSCHANGE) { mac = std::max(fc->mac, mac); } if (want & FF_COMPUTE_PREOPTIMIZE) { if (units == FIT_UNITS_UNINITIALIZED) { units = f1->fitFunction->units; } else if (units != f1->fitFunction->units) { mxThrow("%s: cannot combine units %s and %s (from %s)", matrix->name(), fitUnitsToName(units), fitUnitsToName(f1->fitFunction->units), f1->name()); } } } else { omxRecompute(f1, fc); } if (want & FF_COMPUTE_FIT) { if(f1->rows != 1 || f1->cols != 1) { omxRaiseErrorf("%s[%d]: %s of type %s does not evaluate to a 1x1 matrix", fitMatrix->name(), (int)ex, f1->name(), f1->fitFunction->fitType); } fit += f1->data[0]; if (mg->verbose >= 1) { mxLog("%s: %s fit=%f", fitMatrix->name(), f1->name(), f1->data[0]); } } } if (fc) fc->mac = mac; if (want & FF_COMPUTE_FIT) { fitMatrix->data[0] = fit; if (mg->verbose >= 1) { mxLog("%s: fit=%f", fitMatrix->name(), fit); } } }
static void buildLatentParamMap(omxFitFunction* oo, FitContext *fc) { FreeVarGroup *fvg = fc->varGroup; BA81FitState *state = (BA81FitState *) oo->argStruct; std::vector<int> &latentMap = state->latentMap; BA81Expect *estate = (BA81Expect*) oo->expectation->argStruct; int maxAbilities = estate->grp.maxAbilities; if (state->haveLatentMap == fc->varGroup->id[0]) return; if (estate->verbose >= 1) mxLog("%s: rebuild latent parameter map for var group %d", oo->name(), fc->varGroup->id[0]); state->freeLatents = false; int numLatents = maxAbilities + triangleLoc1(maxAbilities); latentMap.assign(numLatents, -1); int meanNum = 0; if (estate->_latentMeanOut) meanNum = ~estate->_latentMeanOut->matrixNumber; int covNum = 0; if (estate->_latentCovOut) covNum = ~estate->_latentCovOut->matrixNumber; int numParam = int(fvg->vars.size()); for (int px=0; px < numParam; px++) { omxFreeVar *fv = fvg->vars[px]; for (size_t lx=0; lx < fv->locations.size(); lx++) { omxFreeVarLocation *loc = &fv->locations[lx]; int matNum = loc->matrix; if (matNum == meanNum && estate->_latentMeanOut) { latentMap[loc->row + loc->col] = px; state->freeLatents = true; } else if (matNum == covNum && estate->_latentCovOut) { int a1 = loc->row; int a2 = loc->col; if (a1 < a2) std::swap(a1, a2); int cell = maxAbilities + triangleLoc1(a1) + a2; if (latentMap[cell] == -1) { latentMap[cell] = px; if (a1 == a2 && fv->lbound == NEG_INF) { fv->lbound = BA81_MIN_VARIANCE; // variance must be positive Global->boundsUpdated = true; if (fc->est[px] < fv->lbound) { Rf_error("Starting value for variance %s is not positive", fv->name); } } } else if (latentMap[cell] != px) { // doesn't detect similar problems in multigroup constraints TODO Rf_error("Covariance matrix must be constrained to preserve symmetry"); } state->freeLatents = true; } } } state->haveLatentMap = fc->varGroup->id[0]; }
void omxExpectation::loadFromR() { if (!rObj || !data) return; auto ox = this; int numCols=0; bool isRaw = strEQ(omxDataType(data), "raw"); if (isRaw || data->hasSummaryStats()) { ProtectedSEXP Rdcn(R_do_slot(rObj, Rf_install("dataColumnNames"))); loadCharVecFromR(name, Rdcn, dataColumnNames); ProtectedSEXP Rdc(R_do_slot(rObj, Rf_install("dataColumns"))); numCols = Rf_length(Rdc); ox->saveDataColumnsInfo(Rdc); if(OMX_DEBUG) mxPrintMat("Variable mapping", base::getDataColumns()); if (isRaw) { auto dc = base::getDataColumns(); for (int cx=0; cx < numCols; ++cx) { int var = dc[cx]; data->assertColumnIsData(var); } } } if (R_has_slot(rObj, Rf_install("thresholds"))) { if(OMX_DEBUG) { mxLog("Accessing Threshold matrix."); } ProtectedSEXP threshMatrix(R_do_slot(rObj, Rf_install("thresholds"))); if(INTEGER(threshMatrix)[0] != NA_INTEGER) { ox->thresholdsMat = omxMatrixLookupFromState1(threshMatrix, ox->currentState); ox->loadThresholds(); } else { if (OMX_DEBUG) { mxLog("No thresholds matrix; not processing thresholds."); } ox->numOrdinal = 0; } } }
void omxResizeMatrix(omxMatrix *om, int nrows, int ncols) { // Always Recompute() before you Resize(). if(OMX_DEBUG_MATRIX) { mxLog("Resizing matrix from (%d, %d) to (%d, %d)", om->rows, om->cols, nrows, ncols); } if( (om->rows != nrows || om->cols != ncols)) { omxFreeInternalMatrixData(om); om->data = (double*) Calloc(nrows * ncols, double); }
/*********************************** *Name: GetCurrentVersion *Usage: to get bapfeng net Version *Input: void *Output: mac string *Return: 0: success. -1: fail *Comment: **************************************/ int GetVersion(const char *VERFile, char *versionNum) { FILE * fp = NULL; char * buf = NULL; char * p = NULL; long bufsize = 0; if (NULL == VERFile || NULL == versionNum) { mxLog("VERFile or versionNum is NULL"); return -1; } bufsize = getFileSize(VERFile) +1; if(bufsize < 1) { mxLog("ERR! getFileSize"); return -1; } buf = (char *)malloc(bufsize); memset(buf, 0, bufsize); if(0==bufsize || NULL ==buf) { mxLog("bufsize or buf is NULL"); return -1; } if ((fp = fopen(VERFile, "r")) == NULL) { mxLog("Can not open macFile!"); free(buf); mxLog("open VERFile failed"); return -1; } fgets(buf, bufsize, fp); p = buf; while ('\0' != *p ) { if (*p >= '0' && *p <= '9') { *versionNum++ = *p; } p++; } if (fclose(fp) < 0) { mxLog( "ERROR: Close confile failed!"); free(buf); return -1; } free(buf); return 0; }
/*********************************** *Name: GetCurrentVersion *Usage: to get bapfeng net Version *Input: void *Output: mac string *Return: 0: success. -1: fail *Comment: **************************************/ int AssembleJsonURL(const char* urlhead, const char *id, char *JsonUrl) { if (NULL == JsonUrl || NULL == id) { mxLog("jsonUrl or id is NULL"); return -1; } memset(JsonUrl, 0, MAXURL*sizeof(char)); strcpy(JsonUrl, urlhead); strcat(JsonUrl, "?id="); strcat(JsonUrl, id); return 0; }
/*********************************** *Name: GetCurrentVersion *Usage: to get bapfeng net Version *Input: void *Output: mac string *Return: 0: success. -1: fail *Comment: **************************************/ int DownJson(const char *JsonUrlAddr, const char* jsonFileName) { char command[MAX_CHAR_ONE_LINE] = {0}; if (NULL == JsonUrlAddr) { mxLog("jsonUrlAddr is NULL"); return -1; } strcat(command, "wget "); strcat(command, JsonUrlAddr); strcat(command, " -O "); strcat(command, jsonFileName); mxLog("command is: %s", command); if ( 0 != system(command)) { mxLog("Download JSON Failed"); } return 0; }
int UpdateNginxVersion(char * versionFile) { FILE *fp = NULL; if(NULL == versionFile) { mxLog("ERROR: open Nginx File Fail!"); return -1; } if (NULL == (fp = fopen(versionFile, "w+"))) { mxLog("ERROR Can not open fileName!"); return -1; } fputs(pMXData->ngxData.jsonVersion, fp); if (fclose(fp) < 0) { mxLog("ERROR: Close NginxVersion File failed!"); } return 0; }