int main() { printf("PERFECTION OUTPUT\n"); for(;;) { scanf("%d", &n); if(n == 0) break; int d = pda(n); printf("%5d %s\n", n, d == 0? "PERFECT" : d > 0? "ABUNDANT" : "DEFICIENT"); } printf("END OF OUTPUT\n"); }
// Attempt G-H grid? http://dbarajassolano.wordpress.com/2012/01/26/on-sparse-grid-quadratures/ void ba81RefreshQuadrature(omxExpectation* oo) { BA81Expect *state = (BA81Expect *) oo->argStruct; ba81NormalQuad &quad = state->getQuad(); Eigen::VectorXd mean; Eigen::MatrixXd fullCov; state->getLatentDistribution(NULL, mean, fullCov); if (state->verbose >= 1) { mxLog("%s: refresh quadrature", oo->name); if (state->verbose >= 2) { int dim = mean.rows(); pda(mean.data(), 1, dim); pda(fullCov.data(), dim, dim); } } quad.refresh(mean, fullCov); }
int main() { char play; printf("Do you want to enter a string? (y/n): "); scanf("%c", &play); while(play == 'y') { char* input_string; input_string = (char *) malloc(1000); printf("Enter Sting: "); scanf("%s", input_string); printf("%s\n", input_string); int current_state = 0; int length = strlen(input_string); // get length for loop paras = 0; paras_bool = 1; // 1 is true, 0 false stack_symbol = 0; int i = 0; printf("Starting State: q0\n"); while(i<length) // loops until end of string { current_state = pda(input_string[i], current_state); i++; if(current_state == 6) // if trap state/crash breaks break; } //printf("%d | %d | %d", current_state, paras, stack_symbol); if(current_state==5 && paras==0 && stack_symbol==0 && paras_bool == 1) printf("ACCEPTED\n"); else printf("REJECTED\n"); printf("\n"); printf("Do you want to enter a string? (y/n): "); scanf(" %c", &play); } return 0; }
static double ba81ComputeEMFit(omxFitFunction* oo, int want, FitContext *fc) { const double Scale = Global->llScale; BA81FitState *state = (BA81FitState*) oo->argStruct; BA81Expect *estate = (BA81Expect*) oo->expectation->argStruct; omxMatrix *itemParam = estate->itemParam; std::vector<const double*> &itemSpec = estate->grp.spec; std::vector<int> &cumItemOutcomes = estate->grp.cumItemOutcomes; ba81NormalQuad &quad = estate->getQuad(); const int maxDims = quad.maxDims; const size_t numItems = itemSpec.size(); const int do_fit = want & FF_COMPUTE_FIT; const int do_deriv = want & (FF_COMPUTE_GRADIENT | FF_COMPUTE_HESSIAN | FF_COMPUTE_IHESSIAN); if (do_deriv && !state->freeItemParams) { omxRaiseErrorf("%s: no free parameters", oo->name()); return NA_REAL; } if (state->returnRowLikelihoods) { omxRaiseErrorf("%s: vector=TRUE not implemented", oo->name()); return NA_REAL; } if (estate->verbose >= 3) mxLog("%s: complete data fit(want fit=%d deriv=%d)", oo->name(), do_fit, do_deriv); if (do_fit) estate->grp.ba81OutcomeProb(itemParam->data, TRUE); const int thrDerivSize = itemParam->cols * state->itemDerivPadSize; std::vector<double> thrDeriv(thrDerivSize * Global->numThreads); double *wherePrep = quad.wherePrep.data(); double ll = 0; #pragma omp parallel for num_threads(Global->numThreads) reduction(+:ll) for (size_t ix=0; ix < numItems; ix++) { const int thrId = omx_absolute_thread_num(); const double *spec = itemSpec[ix]; const int id = spec[RPF_ISpecID]; const int dims = spec[RPF_ISpecDims]; Eigen::VectorXd ptheta(dims); const rpf_dLL1_t dLL1 = Glibrpf_model[id].dLL1; const int iOutcomes = estate->grp.itemOutcomes[ix]; const int outcomeBase = cumItemOutcomes[ix] * quad.totalQuadPoints; const double *weight = estate->expected + outcomeBase; const double *oProb = estate->grp.outcomeProb + outcomeBase; const double *iparam = omxMatrixColumn(itemParam, ix); double *myDeriv = thrDeriv.data() + thrDerivSize * thrId + ix * state->itemDerivPadSize; for (int qx=0; qx < quad.totalQuadPoints; qx++) { if (do_fit) { for (int ox=0; ox < iOutcomes; ox++) { ll += weight[ox] * oProb[ox]; } } if (do_deriv) { double *where = wherePrep + qx * maxDims; for (int dx=0; dx < dims; dx++) { ptheta[dx] = where[std::min(dx, maxDims-1)]; } (*dLL1)(spec, iparam, ptheta.data(), weight, myDeriv); } weight += iOutcomes; oProb += iOutcomes; } } size_t excluded = 0; if (do_deriv) { double *deriv0 = thrDeriv.data(); int perThread = itemParam->cols * state->itemDerivPadSize; for (int th=1; th < Global->numThreads; th++) { double *thrD = thrDeriv.data() + th * perThread; for (int ox=0; ox < perThread; ox++) deriv0[ox] += thrD[ox]; } int numFreeParams = int(state->numFreeParam); int ox=-1; for (size_t ix=0; ix < numItems; ix++) { const double *spec = itemSpec[ix]; int id = spec[RPF_ISpecID]; double *iparam = omxMatrixColumn(itemParam, ix); double *pad = deriv0 + ix * state->itemDerivPadSize; (*Glibrpf_model[id].dLL2)(spec, iparam, pad); HessianBlock *hb = state->hBlocks[ix].clone(); hb->mat.triangularView<Eigen::Upper>().setZero(); for (int dx=0; dx < state->itemDerivPadSize; ++dx) { int to = state->paramMap[++ox]; if (to == -1) continue; // Need to check because this can happen if // lbounds/ubounds are not set appropriately. if (0 && !std::isfinite(deriv0[ox])) { int item = ox / itemParam->rows; mxLog("item parameters:\n"); const double *spec = itemSpec[item]; int id = spec[RPF_ISpecID]; int numParam = (*Glibrpf_model[id].numParam)(spec); double *iparam = omxMatrixColumn(itemParam, item); pda(iparam, numParam, 1); // Perhaps bounds can be pulled in from librpf? TODO Rf_error("Deriv %d for item %d is %f; are you missing a lbound/ubound?", ox, item, deriv0[ox]); } if (to < numFreeParams) { if (want & FF_COMPUTE_GRADIENT) { fc->grad(to) -= Scale * deriv0[ox]; } } else { if (want & (FF_COMPUTE_HESSIAN | FF_COMPUTE_IHESSIAN)) { int Hto = state->hbMap[ox]; if (Hto >= 0) hb->mat.data()[Hto] -= Scale * deriv0[ox]; } } } fc->queue(hb); } } if (excluded && estate->verbose >= 1) { mxLog("%s: Hessian not positive definite for %d/%d items", oo->name(), (int) excluded, (int) numItems); } if (excluded == numItems) { omxRaiseErrorf("Hessian not positive definite for %d/%d items", (int) excluded, (int) numItems); } return Scale * ll; }
void optimize3(int *fn, int *fp, int *groups, double *fvals, int *groupraw,int *gname, int *ngroup, int *method, double *cooling, double *temp_start, int *projdim, double *val, double *proj,int *r,double *lambda) { int n,p,g,projn,projp,maxproj,i,j,m,kt,count,*group; double *proj_best, *projdata, *index_best,*proj_temp,*index_temp; double temp_end,temp,cool,*sum,diff,radius; double *index_work,*proj_work; n = *fn; p = *fp; g = *groups; projn = p; projp = *projdim; temp = *temp_start; cool = *cooling; temp_end = 0.001; maxproj=1000; group = Calloc(n,int); zero_int(group, n); if(gname[(g-1)] !=g) { for(i=0; i<n; i++) for(j=0; j<g; j++) if(groupraw[i] == gname[j]) group[i] = j+1; } else memcpy(group,groupraw,n*sizeof(int)); /********************************/ proj_best = Calloc(projp*projn,double);zero(proj_best,projp*projn); proj_work = Calloc(projp*projn,double);zero(proj_work,projp*projn); proj_temp = Calloc(projp*projn,double);zero(proj_temp,projp*projn); projdata = Calloc(projp*n,double); zero(projdata,projp*n); index_best = Calloc(1,double); index_work = Calloc(1,double); index_temp = Calloc(1,double); sum = Calloc(1,double); /* initrandom(start); */ GetRNGstate(); iszero(fp,projdim,proj_best,sum); if (!(*sum)) { normal_fill1 (fp,projdim,proj_best, 1.0,proj_best); orthonormal (proj_best,fp,projdim); } for(i=0; i<projp; i++) { for (j=0;j<n; j++) { projdata[i*n+j] = 0; for (m=0; m<p; m++) projdata[i*n+j] += proj_best[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_best); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_best); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_best,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_best); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_best); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_work,lambda); break; } kt = 1; for(i=0; i<projp; i++) for (m=0; m<projn; m++) proj_work[i*p+m] = proj_best[i*p+m]; temp = 1.5/sqrt(projn+10); count = 0; radius = 89; while (radius>0.6) { /* Step 1 */ normal_fill1 (fp,projdim,proj_temp,(0.2/log(projn+5)),proj_best); orthonormal (proj_temp,fp,projdim); zero(projdata,projp*n); for(i=0; i<projp; i++) { for (j=0;j<n; j++) { for (m=0; m<p; m++) projdata[i*n+j] += proj_temp[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_temp); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_temp); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_temp,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_temp); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_temp); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_work,lambda); break; } diff=*index_temp-*index_best; if(diff <=0) count++; else { *index_work=*index_temp; for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj_work[i*projn+j] =proj_temp[i*projn+j]; } /* Step 2 */ normal_fill1 (fp,projdim,proj_temp,temp,proj_best); orthonormal (proj_temp,fp,projdim); zero(projdata,projp*n); for(i=0; i<projp; i++) { for (j=0;j<n; j++) { for (m=0; m<p; m++) projdata[i*n+j] += proj_temp[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_temp); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_temp); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_temp,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_temp); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_temp); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_work,lambda); break; } diff=*index_temp-*index_best; if(diff <=0) count++; else if(*index_temp > *index_work) { *index_work=*index_temp; for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj_work[i*projn+j] =proj_temp[i*projn+j]; } /* Step 3 */ normal_fill1 (fp,projdim,proj_temp,temp,proj_best); orthonormal (proj_temp,fp,projdim); zero(projdata,projp*n); for(i=0; i<projp; i++) { for (j=0;j<n; j++) { for (m=0; m<p; m++) projdata[i*n+j] += proj_temp[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_temp); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_temp); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_temp,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_temp); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_temp); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_work,lambda); break; } diff=*index_temp-*index_best; if(diff <=0) count++; else if(*index_temp > *index_work) { *index_work=*index_temp; for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj_work[i*projn+j] =proj_temp[i*projn+j]; } /* Step 4 */ normal_fill1 (fp,projdim,proj_temp,temp,proj_best); orthonormal (proj_temp,fp,projdim); zero(projdata,projp*n); for(i=0; i<projp; i++) { for (j=0;j<n; j++) { for (m=0; m<p; m++) projdata[i*n+j] += proj_temp[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_temp); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_temp); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_temp,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_temp); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_temp); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_work,lambda); break; } diff=*index_temp-*index_best; if(diff <=0) count++; else if(*index_temp > *index_work) { *index_work=*index_temp; for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj_work[i*projn+j] =proj_temp[i*projn+j]; } /* final step */ if(count > projn*8) { radius = radius/2; temp= temp/2; count = 0; } for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj_best[i*projn+j] =proj_work[i*projn+j]; *index_best = *index_work; kt++; } for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj[i*projn+j] =proj_best[i*projn+j]; *val = *index_best; /* printf("%d\n",kt);*/ PutRNGstate(); Free(proj_best); Free(proj_work); Free(proj_temp); Free(projdata); Free(index_work); Free(index_best); Free(index_temp); Free(sum); Free(group); }
void optimize2(int *fn, int *fp, int *groups, double *fvals, int *groupraw, int *gname, int *ngroup, int *method, double *cooling, double *temp_start, int *projdim, double *val, double *proj, double *energy,int *r,double *lambda) { int n,p,g,projn,projp,maxproj,i,j,m,kt,*group; double *proj_best,*proj_work, *projdata,*index_work, *index_best; double temp_end,temp,cool,tempp,*sum,prob,e,diff; n = *fn; p = *fp; g = *groups; projn = p; projp = *projdim; temp = *temp_start; cool = *cooling; temp_end = 0.001; maxproj=1000; group = Calloc(n,int); zero_int(group, n); if(gname[(g-1)] !=g) { for(i=0; i<n; i++) for(j=0; j<g; j++) if(groupraw[i] == gname[j]) group[i] = j+1; } else memcpy(group,groupraw,n*sizeof(int)); /* printf("in Optimize %f\n",*var);*/ /********************************/ proj_best = Calloc(projp*projn,double);zero(proj_best,projp*projn); proj_work = Calloc(projp*projn,double);zero(proj_work,projp*projn); projdata = Calloc(projp*n,double); zero(projdata,projp*n); index_best = Calloc(1,double); index_work = Calloc(1,double); sum = Calloc(1,double); /* initrandom(start); */ GetRNGstate(); iszero(fp,projdim,proj_best,sum); if (!(*sum)) { normal_fill1 (fp,projdim,proj_best, 1.0,proj_best); orthonormal (proj_best,fp,projdim); } for(i=0; i<projp; i++) { for (j=0;j<n; j++) { projdata[i*n+j] = 0; for (m=0; m<p; m++) projdata[i*n+j] += proj_best[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_best); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_best); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_best,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_best); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_best); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_best,lambda); break; } kt = 1; for(i=0; i<projp; i++) for (m=0; m<projn; m++) proj_work[i*p+m] = proj_best[i*p+m]; diff=100; while ((temp >0.001 || fabs(diff)>(*energy/1000000)) && kt <=500000) { tempp = *energy/log(kt+1)/10000; temp = temp*cool; normal_fill1 (fp,projdim,proj_work,temp,proj_best); orthonormal (proj_work,fp,projdim); zero(projdata,projp*n); for(i=0; i<projp; i++) { for (j=0;j<n; j++) { for (m=0; m<p; m++) projdata[i*n+j] += proj_work[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_work); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_work); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_work,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_work); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_work); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_work,lambda); break; } prob=uniformrandom(); diff=*index_work-*index_best; e=exp(diff/tempp); if( prob<e) { for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj_best[i*projn+j] =proj_work[i*projn+j]; *index_best = *index_work; } kt++; } for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj[i*projn+j] =proj_best[i*projn+j]; *val = *index_best; /* printf("iteration = %d\n",kt);*/ PutRNGstate(); Free(proj_best); Free(proj_work); Free(projdata); Free(index_work); Free(index_best); Free(sum); Free(group); }
void optimize1(int *fn, int *fp, int *groups, double *fvals, int *groupraw,int *gname, int *ngroup, int *method, double *cooling, double *temp_start, int *projdim, double *val, double *proj,int *r,double *lambda) { int n,p,g,projn,projp,maxproj,i,j,m,kt,*group; double *proj_best,*proj_work, *projdata,*index_work, *index_best; double temp_end,temp,cool,*sum; n = *fn; p = *fp; g = *groups; projn = p; projp = *projdim; temp = *temp_start; cool = *cooling; temp_end = 0.001; maxproj=1000; group = Calloc(n,int); zero_int(group, n); if(gname[(g-1)] !=g) { for(i=0; i<n; i++) for(j=0; j<g; j++) if(groupraw[i] == gname[j]) group[i] = j+1; } else memcpy(group,groupraw,n*sizeof(int)); /********************************/ proj_best = Calloc(projp*projn,double);zero(proj_best,projp*projn); proj_work = Calloc(projp*projn,double);zero(proj_work,projp*projn); projdata = Calloc(projp*n,double); zero(projdata,projp*n); index_best = Calloc(1,double); index_work = Calloc(1,double); sum = Calloc(1,double); /* initrandom(start); */ GetRNGstate(); iszero(fp,projdim,proj_best,sum); if (!(*sum)) { normal_fill1 (fp,projdim,proj_best, 1.0,proj_best); orthonormal (proj_best,fp,projdim); } for(i=0; i<projp; i++) { for (j=0;j<n; j++) { projdata[i*n+j] = 0; for (m=0; m<p; m++) projdata[i*n+j] += proj_best[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_best); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_best); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_best,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_best); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_best); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_best,lambda); break; } kt = 0; for(i=0; i<projp; i++) for (m=0; m<projn; m++) proj_work[i*p+m] = proj_best[i*p+m]; while (temp> temp_end && kt < maxproj) { normal_fill1 (fp,projdim,proj_work,temp,proj_best); orthonormal (proj_work,fp,projdim); temp *= cool; zero(projdata,projp*n); for(i=0; i<projp; i++) { for (j=0;j<n; j++) { for (m=0; m<p; m++) projdata[i*n+j] += proj_work[i*p+m]*fvals[m*n+j]; } } switch(*method) { case 1: discriminant1(fn,projdim,groups,projdata,group,gname,ngroup,index_work); break; case 2: discriminant2(fn,projdim,groups,projdata,group,gname,ngroup,index_work); break; case 3: discriminant3(fn,projdim,groups,projdata,group,gname,ngroup,index_work,r); break; case 4: cartgini(fn,projdim,groups,projdata,group,ngroup,index_work); break; case 5: cartentropy(fn,projdim,groups,projdata,group,ngroup,index_work); break; case 6: pda(fn,projdim,groups,projdata,group,gname,ngroup,index_best,lambda); break; } if(*index_work > *index_best) { for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj_best[i*projn+j] =proj_work[i*projn+j]; *index_best = *index_work; } kt++; } /* printf("kt = %d\n",kt);*/ for(i=0;i<projp; i++) for(j=0; j<projn; j++) proj[i*projn+j] =proj_best[i*projn+j]; *val = *index_best; PutRNGstate(); Free(proj_best); Free(proj_work); Free(projdata); Free(index_work); Free(index_best); Free(sum); Free(group); }