double iterfcn(int n, int s, int iter, int gbest, struct swarm *pop) { bool stop = false; int stat; double ret = 1.0; double evaltime; //Get Execution Time end = clock(); evaltime = ((double)(end-start))/CLOCKS_PER_SEC; //Iteration Printing if(printLevel > 1 && iter > 0) { if(iter == 10 || !(iter%100)) mexPrintf(" iter feval time[s] leader objective\n"); mexPrintf("%5d %5d %5.2f %5d %12.5g\n",iter,noFeval,evaltime,gbest,pop->fy[gbest]); mexEvalString("drawnow;"); //flush draw buffer } //Check for Ctrl-C if (utIsInterruptPending()) { utSetInterruptPending(false); /* clear Ctrl-C status */ mexPrintf("\nCtrl-C Detected. Exiting PSwarm...\n\n"); ret = -1.0; ctrlCExit = true; } //Iteration Callback if(iterF.enabled && iter > 0) { iterF.plhs[0] = NULL; memcpy(mxGetData(iterF.prhs[1]), &iter, sizeof(int)); memcpy(mxGetPr(iterF.prhs[2]), &pop->fy[gbest], sizeof(double)); memcpy(mxGetPr(iterF.prhs[3]), &pop->y[gbest*n], n * sizeof(double)); stat = mexCallMATLAB(1, iterF.plhs, 4, iterF.prhs, iterF.f); if(stat) mexErrMsgTxt("Error calling Callback Function!"); //Collect return argument stop = *(bool*)mxGetData(iterF.plhs[0]); if(stop) { ctrlCExit = true; mexPrintf("\nIterFun Called Stop. Exiting PSwarm...\n\n"); ret = -1; } // Clean up Ptr mxDestroyArray(iterF.plhs[0]); } //Check for maxtime expiry if(evaltime > maxtime) { mexPrintf("\nMaximum Solver Time Exceeded. Exiting PSwarm...\n\n"); ret = -1.0; } return ret; }
int train_callback(mkl_network *net){ learning_info info; char in_message[1024]; info.MSE = net->MSE; info.grad = 0; info.time = net->elpased_time; info.epoch = net->epoch; net_send_info(&info); mexPrintf("MSE: %e\tTime: %d\n", net->MSE, net->elpased_time); MSE_log[net->epoch - 1] = net->MSE; time_log[net->epoch - 1] = net->elpased_time; mexEvalString("drawnow;"); if (utIsInterruptPending()){ mexPrintf("Cought Ctrl+C. Wait a 5 sec.\n"); mexEvalString("drawnow;"); utSetInterruptPending(0); return -1; } if (net_receive_info(in_message)){ if(!strcmp("{command:stop}", in_message)){ return -1 } } return 0; }
//Solver Iteration Callback Monitor int user_exit(int n, int k, struct blockmatrix C, double *a, double dobj, double pobj, double constant_offset, struct constraintmatrix *con, struct blockmatrix X, double *y, struct blockmatrix Z, struct paramstruc params) { double evaltime; //Get Execution Time end = clock(); evaltime = ((double)(end-start))/CLOCKS_PER_SEC; //Check max time if(evaltime > maxtime) return CSDP_MAX_TIME; //Check ctrl-c if (utIsInterruptPending()) { utSetInterruptPending(false); /* clear Ctrl-C status */ mexPrintf("\nCtrl-C Detected. Exiting CSDP...\n\n"); return CSDP_USER_TERMINATION; //terminate } if(printLevel>1 && citer) { //Display heading if % 20 iters if(citer==1 || citer % 20 == 0) mexPrintf("Iter Time PP Objective DD Objective\n"); //Display parameters mexPrintf("%-3d %6.2f %16.8e %16.8e\n",citer,evaltime,pobj,dobj); mexEvalString("drawnow;"); //flush draw buffer } citer++; //Return ok return(0); }
//Solver Iteration Callback Monitor static int DSDPMonitor(DSDP dsdp, void* dummy) { int iter; double pobj,dobj,pstp=0,dstp,mu,res,pnorm,pinfeas; double evaltime; //Get Execution Time end = clock(); evaltime = ((double)(end-start))/CLOCKS_PER_SEC; //Check max time if(evaltime > maxtime) return DSDP_MAX_TIME; //Check ctrl-c if (utIsInterruptPending()) { utSetInterruptPending(false); /* clear Ctrl-C status */ mexPrintf("\nCtrl-C Detected. Exiting DSDP...\n\n"); return DSDP_USER_TERMINATION; //terminate } if(printLevel>1) { DSDPGetIts(dsdp,&iter); DSDPGetDDObjective(dsdp,&dobj); DSDPGetPPObjective(dsdp,&pobj); DSDPGetR(dsdp,&res); DSDPGetPInfeasibility(dsdp,&pinfeas); DSDPGetStepLengths(dsdp,&pstp,&dstp); DSDPGetBarrierParameter(dsdp,&mu); DSDPGetPnorm(dsdp,&pnorm); //Display heading if % 20 iters if(iter==0 || iter % 20 == 0) mexPrintf("Iter Time[s] PP Objective DD Objective PInfeas DInfeas Nu StepLength Pnrm\n"); //Display parameters mexPrintf("%-3d %6.2f %16.8e %16.8e %9.1e %9.1e %9.1e",iter,evaltime,pobj,dobj,pinfeas,res,mu); mexPrintf(" %4.2f %4.2f",pstp,dstp); if (pnorm>1.0e3) mexPrintf(" %1.0e \n",pnorm); else mexPrintf(" %5.2f \n",pnorm); mexEvalString("drawnow;"); //flush draw buffer } //Return ok return(0); }
bool lbfgsb_program::iterCallback (int t, double etime, double* x, double f) { bool stop = false; //Check for Ctrl-C if (utIsInterruptPending()) { utSetInterruptPending(false); /* clear Ctrl-C status */ mexPrintf("\nCtrl-C Detected. Exiting L-BFGS-B...\n\n"); return true; //terminate asap } //Iteration Printing if(printLevel > 1) { if(t==1 || !(t%10)) mexPrintf(" iter feval time[s] objective\n"); mexPrintf("%5d %5d %5.2f %12.5g\n",t,noFevals,etime,f); mexEvalString("drawnow;"); //flush draw buffer } //Iteration Callback if(iterF->enabled) { iterF->plhs[0] = NULL; memcpy(mxGetData(iterF->prhs[1]), &t, sizeof(int)); memcpy(mxGetPr(iterF->prhs[2]), &f, sizeof(double)); memcpy(mxGetPr(iterF->prhs[3]), x, this->ndec * sizeof(double)); try { mexCallMATLAB(1, iterF->plhs, 4, iterF->prhs, iterF->f); } catch(...) { mexWarnMsgTxt("Unrecoverable Error from Iteration Callback, Exiting LBFGSB...\n"); //Force exit return true; } //Collect return argument stop = *(bool*)mxGetData(iterF->plhs[0]); // Clean up Ptr mxDestroyArray(iterF->plhs[0]); } return stop; }
void Net::Forward(Mat &pred, int passnum) { if (first_layer_ == 0) { //mexPrintMsg("Forward pass for layer", layers_[0]->type_); layers_[0]->Forward(NULL, passnum); layers_[0]->CalcWeights(NULL, passnum); } for (size_t i = first_layer_; i < layers_.size(); ++i) { if (layers_[i]->type_ == "j") first_layer_ = i; //mexPrintMsg("Forward pass for layer", layers_[i]->type_); layers_[i]->Forward(layers_[i-1], passnum); layers_[i]->Nonlinear(passnum); if (utIsInterruptPending()) { mexAssert(false, "Ctrl-C Detected. END"); } } pred.attach(layers_.back()->activ_mat_); //mexPrintMsg("Forward pass finished"); }
void Net::Forward(Mat &pred, int passnum) { //mexPrintMsg("Start forward pass..."); //mexPrintMsg("Forward pass for layer", layers_[0]->type_); layers_[0]->Forward(NULL, passnum); for (size_t i = 1; i < layers_.size(); ++i) { //mexPrintMsg("Forward pass for layer", layers_[]->type_); Mat activ_mat_prev; layers_[i]->Forward(layers_[i-1], passnum); if (layers_[i]->type_ == "c" || layers_[i]->type_ == "f") { layers_[i]->Nonlinear(passnum); } if (passnum == 0) layers_[i-1]->activ_mat_.clear(); if (utIsInterruptPending()) { Clear(); mexAssert(false, "Ctrl-C Detected. END"); } /* for (int j = 0; j < 5; ++j) { //mexPrintMsg("activ_mat_", layers_[i]->activ_mat_(0, j)); }*/ } pred.attach(layers_.back()->activ_mat_); //("Forward pass finished"); }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ ALLOCATES(); CreateTicTacToc( CallMatlab ); CreateTicTacToc( callSort ); int I, J, K, ii, jj, kk; int IJ, IJK, IJK_1; int DI, DJ, DK, DIJK, DIJK_1; int CDI, CDJ, CDK; int result, fevals = 0; int NVOLS, NVOLS_1, n, s, s_start, s_end, v_init; real *volumes, *V, x, y, *DIST, *order, last_distance; int *VV=NULL, nV, v, vv; char skip; triplet *TS=NULL, *DTS=NULL, T; mxArray *INPUT[2]={NULL,NULL}, *OUTPUT[3]={NULL,NULL,NULL}; double *MAXs, LAST_MAX; double thisMINx, thisMINy; double *idxs; double *vols; double *ijk; char callSort; mwSize toVec[2]={1,1}; char VERBOSE = 0; char STR[1024]; if( nlhs > 1 ){ mxErrMsgTxt("too much outputs"); } if( mxIsChar( prhs[nrhs-1] ) ){ mxGetString( prhs[nrhs-1], STR, 100 ); if( ! myStrcmpi(STR,"verbose") ){ VERBOSE = 1; } else { mxErrMsgTxt("only 'verbose' option allowed."); } nrhs = nrhs-1; } if( nrhs != 3 ){ mxErrMsgTxt("sintax error. max_min_multiples_erodes( V , F , volumes )"); } if( mxGetClassID( prhs[1] ) != mxFUNCTION_CLASS ){ mxErrMsgTxt("F have to be a function_handle."); } if( myNDims( prhs[0] ) > 3 ){ mxErrMsgTxt("bigger than 3d arrays is not allowed."); } NVOLS = myNumel( prhs[2] ); NVOLS_1 = NVOLS - 1; volumes = myGetPr( prhs[2] ); I = mySize( prhs[0] , 0 ); J = mySize( prhs[0] , 1 ); K = mySize( prhs[0] , 2 ); IJ = I*J; IJK = IJ*K; VV = (int *) mxMalloc( IJK*sizeof( int ) ); TS = (triplet *) mxMalloc( IJK*sizeof( triplet ) ); V = myGetPr( prhs[0] ); v = 0; nV = 0; for( kk = 0 ; kk < K ; kk++ ){ for( jj = 0 ; jj < J ; jj++ ){ for( ii = 0 ; ii < I ; ii++ ){ x = V[ v ]; if( x == x ){ VV[ nV ] = v; TS[ v ].isnan = 0; TS[ v ].i = ii; TS[ v ].j = jj; TS[ v ].k = kk; nV++; } else { TS[ v ].isnan = 1; } v++; }}} INPUT[0] = prhs[1]; INPUT[1] = mxCreateNumericMatrix( 1 , 3 , mxDOUBLE_CLASS , mxREAL ); ijk = (double *) mxGetData( INPUT[1] ); ijk[0] = TS[ VV[ nV/2] ].i + 1; ijk[1] = TS[ VV[ nV/2] ].j + 1; ijk[2] = TS[ VV[ nV/2] ].k + 1; OUTPUT[2] = mexCallMATLABWithTrap( 2 , OUTPUT , 2 , INPUT , "feval" ); if( OUTPUT[2] == NULL ){ callSort = 0; if( mxGetClassID( OUTPUT[0] ) != mxDOUBLE_CLASS ){ if( INPUT[1] != NULL ){ mxDestroyArray( INPUT[1] ); INPUT[1]=NULL; } if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; } if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; } if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; } mxErrMsgTxt("F debe retornar un double en el primer output."); } if( mxGetClassID( OUTPUT[1] ) != mxDOUBLE_CLASS ){ if( INPUT[1] != NULL ){ mxDestroyArray( INPUT[1] ); INPUT[1]=NULL; } if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; } if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; } if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; } mxErrMsgTxt("F debe retornar un double en el segundo output."); } } else { callSort = 1; if( VERBOSE ){ mexPrintf("sort has to be called\n"); } mxDestroyArray( OUTPUT[2] ); OUTPUT[2] = NULL; result = mexCallMATLAB( 1 , OUTPUT , 2 , INPUT , "feval" ); if( result ){ mxErrMsgTxt("error computing la funcion."); } if( mxGetClassID( OUTPUT[0] ) != mxDOUBLE_CLASS ){ if( INPUT[1] != NULL ){ mxDestroyArray( INPUT[1] ); INPUT[1]=NULL; } if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; } if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; } if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; } mxErrMsgTxt("F debe retornar un double en el primer output."); } } DI = mySize( OUTPUT[0] , 0 ); DJ = mySize( OUTPUT[0] , 1 ); DK = mySize( OUTPUT[0] , 2 ); DTS = (triplet *) mxMalloc( 2*DI*DJ*DK*sizeof( triplet ) ); plhs[0] = mxCreateNumericMatrix( NVOLS , 1 , mxREAL_CLASS , mxREAL ); MAXs = (real *) mxGetData( plhs[0] ); for( n = 0 ; n < NVOLS ; n++ ){ MAXs[n] = -10000; } LAST_MAX = MAXs[ NVOLS_1 ]; for( v_init = 0 ; v_init < EVERY ; v_init++ ){ if( utIsInterruptPending() ){ if( INPUT[1] != NULL ){ mxDestroyArray( INPUT[1] ); INPUT[1]=NULL; } if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; } if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; } if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; } mexPrintf("USER INTERRUP!!!\n"); mxErrMsgTxt("USER INTERRUP!!!"); } if( VERBOSE ){ mexPrintf("v_init: %d (%g) of %d\n", v_init , LAST_MAX , EVERY ); } for( v = v_init ; v < nV ; v += EVERY ){ vv = VV[ v ]; thisMINx = V[ vv ]; thisMINy = -thisMINx; if( ( thisMINx < LAST_MAX ) && ( thisMINy < LAST_MAX ) ){ continue; } T = TS[ vv ]; ijk[0] = T.i + 1; ijk[1] = T.j + 1; ijk[2] = T.k + 1; if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; } if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; } if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; } if( !callSort ){ tic( CallMatlab ); result = mexCallMATLAB( 2 , OUTPUT , 2 , INPUT , "feval" ); fevals++; tac( CallMatlab ); } else { tic( CallMatlab ); result = mexCallMATLAB( 1 , OUTPUT , 2 , INPUT , "feval" ); fevals++; tac( CallMatlab ); } DI = mySize( OUTPUT[0] , 0 ); DJ = mySize( OUTPUT[0] , 1 ); DK = mySize( OUTPUT[0] , 2 ); DIJK = DI*DJ*DK; if( volumes[ NVOLS_1 ] > DIJK ){ if( INPUT[1] != NULL ){ mxDestroyArray( INPUT[1] ); INPUT[1]=NULL; } if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; } if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; } if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; } mxErrMsgTxt("el maximo volumen debe ser menor que numel(DIST)"); } DIJK_1 = DIJK - 1; DIST = (double *) mxGetData( OUTPUT[0] ); DTS = (triplet *) mxRealloc( DTS , DIJK*sizeof( triplet ) ); s = 0; for( kk = 0 ; kk < DK ; kk++ ){ for( jj = 0 ; jj < DJ ; jj++ ){ for( ii = 0 ; ii < DI ; ii++ ){ DTS[ s ].i = ii; DTS[ s ].j = jj; DTS[ s ].k = kk; s++; }}} if( !callSort ){ order = (double *) mxGetData( OUTPUT[1] ); } else { toVec[0] = mxGetNumberOfElements( OUTPUT[0] ); mxSetDimensions( OUTPUT[0] , toVec , 2 ); tic( callSort ); result = mexCallMATLAB( 2 , OUTPUT+1 , 1 , OUTPUT , "sort" ); tac( callSort ); order = (double *) mxGetData( OUTPUT[2] ); } CDI = DTS[ (int) ( order[0] - 1 ) ].i; CDJ = DTS[ (int) ( order[0] - 1 ) ].j; CDK = DTS[ (int) ( order[0] - 1 ) ].k; skip = 0; s = 0; for( n = 0 ; n < NVOLS ; n++ ){ s_end = (int) ( volumes[n] - 1 ); last_distance = DIST[ (int) order[ s_end ] - 1 ]; while( s_end < DIJK_1 && DIST[ (int) ( order[ s_end + 1 ] - 1 ) ] == last_distance ){ s_end++; } s_end++; for( ; s < s_end ; s++ ){ vv = (int) ( order[ s ] - 1 ); ii = T.i + DTS[ vv ].i - CDI; if( ii < 0 || ii > I ){ skip = 1; break; } jj = T.j + DTS[ vv ].j - CDJ; if( jj < 0 || jj > J ){ skip = 1; break; } kk = T.k + DTS[ vv ].k - CDK; if( kk < 0 || kk > K ){ skip = 1; break; } vv = ii + jj*I + kk*IJ; if( TS[ vv ].isnan ){ skip = 1; break; } x = V[ vv ]; if( x < thisMINx ){ thisMINx = x; } y = -x; if( y < thisMINy ){ thisMINy = y; } if( ( thisMINx < LAST_MAX ) && ( thisMINy < LAST_MAX ) ){ skip = 1; break; } } if( skip ){ break; } if( thisMINx > MAXs[n] ){ MAXs[n] = thisMINx; } if( thisMINy > MAXs[n] ){ MAXs[n] = thisMINy; } } LAST_MAX = MAXs[ NVOLS_1 ]; } } if( INPUT[1] != NULL ){ mxDestroyArray( INPUT[1] ); INPUT[1] =NULL; } if( OUTPUT[0] != NULL ){ mxDestroyArray( OUTPUT[0] ); OUTPUT[0]=NULL; } if( OUTPUT[1] != NULL ){ mxDestroyArray( OUTPUT[1] ); OUTPUT[1]=NULL; } if( OUTPUT[2] != NULL ){ mxDestroyArray( OUTPUT[2] ); OUTPUT[2]=NULL; } if( VERBOSE ){ mexPrintf( "\nfevals: %d en tiempo: CallMatlab: %20.30g sorting: %20.30g\n" , fevals , toc( CallMatlab ) , toc( callSort ) ); } if( VV != NULL ){ mxFree( VV ); } if( TS != NULL ){ mxFree( TS ); } if( DTS != NULL ){ mxFree( DTS ); } myFreeALLOCATES(); }
int check_ctrlc(void) { return utIsInterruptPending(); }
//MATLAB Callback static void SIMUL(int *indic, int *n, double *x, double *f, double *g, int *izs, float *rzs, double *dzs) { int stat; double *grad; bool stop = false; double evaltime; //Get Execution Time end = clock(); evaltime = ((double)(end-start))/CLOCKS_PER_SEC; //Check for Ctrl-C if (utIsInterruptPending()) { utSetInterruptPending(false); /* clear Ctrl-C status */ mexPrintf("\nCtrl-C Detected. Exiting M1QN3...\n\n"); *indic = 0; //terminate return; } //Check for maxtime expiry if(evaltime > maxtime) { mexPrintf("\nMaximum Solver Time Exceeded. Exiting M1QN3...\n\n"); *indic = 0; //terminate return; } //Only compute f and g if requested if(*indic == 4) { fun.plhs[0] = NULL; memcpy(mxGetPr(fun.prhs[fun.xrhs]), x, *n * sizeof(double)); stat = mexCallMATLAB(1, fun.plhs, fun.nrhs, fun.prhs, fun.f); if(stat) mexErrMsgTxt("Error calling Objective Function!"); //Get Objective *f = *mxGetPr(fun.plhs[0]); // Clean up Ptr mxDestroyArray(fun.plhs[0]); //Check for inf, nan if(mxIsInf(*f) || mxIsNaN(*f)) *indic = -1; //indicate smaller step size //Get Gradient fun.plhs[0] = NULL; memcpy(mxGetPr(fun.prhs_g[fun.xrhs_g]), x, *n * sizeof(double)); stat = mexCallMATLAB(1, fun.plhs, fun.nrhs_g, fun.prhs_g, fun.g); if(stat) mexErrMsgTxt("Error calling Gradient Function!"); //Get Gradient grad = mxGetPr(fun.plhs[0]); //Assign Gradient memcpy(g,grad,*n*sizeof(double)); // Clean up Ptr mxDestroyArray(fun.plhs[0]); //Iteration Printing if(izs[1] > 1) { if(izs[0] == 1 || !(izs[0]%10)) mexPrintf(" feval time fval\n"); mexPrintf("%5d %5.2f %12.5g\n",izs[0],evaltime,*f); mexEvalString("drawnow;"); //flush draw buffer } //Iteration Callback if(iterF.enabled) { iterF.plhs[0] = NULL; memcpy(mxGetData(iterF.prhs[1]), izs, sizeof(int)); memcpy(mxGetPr(iterF.prhs[2]), f, sizeof(double)); memcpy(mxGetPr(iterF.prhs[3]), x, *n * sizeof(double)); stat = mexCallMATLAB(1, iterF.plhs, 4, iterF.prhs, iterF.f); if(stat) mexErrMsgTxt("Error calling Callback Function!"); //Collect return argument stop = *(bool*)mxGetData(iterF.plhs[0]); // Clean up Ptr mxDestroyArray(iterF.plhs[0]); if(stop) *indic = 0; //force exit } //Increment feval counter izs[0]++; } }
//LM_DER Callback static void jacfunc(int *m, int *n, double *x, double *fvec, double *fjac, int *ldfjac, int *iflag) { bool havrnorm = false, stop = false; int i, stat; double *fval, rnorm=0, evaltime; //Get Execution Time end = clock(); evaltime = ((double)(end-start))/CLOCKS_PER_SEC; //Function Eval if(*iflag == 1) { fun.plhs[0] = NULL; memcpy(mxGetPr(fun.prhs[fun.xrhs]), x, *n * sizeof(double)); stat = mexCallMATLAB(1, fun.plhs, fun.nrhs, fun.prhs, fun.f); if(stat) mexErrMsgTxt("Error calling Objective Function!"); //Get Objective fval = mxGetPr(fun.plhs[0]); //Assign Objective + subtract ydata for(i=0;i<*m;i++) fvec[i] = fval[i]-ydata[i]; // Clean up Ptr mxDestroyArray(fun.plhs[0]); //Iteration Printing if(printLevel > 1) { //Calculate Residual Norm rnorm = 0; havrnorm = true; for(i=0;i<*m;i++) rnorm += (fval[i]-ydata[i])*(fval[i]-ydata[i]); if(citer == 1 || !(citer%10)) mexPrintf(" feval time[s] sse\n"); mexPrintf("%5d %5.2f %12.5g\n",citer,evaltime,rnorm); mexEvalString("drawnow;"); //flush draw buffer } //Iteration Callback if(iterF.enabled) { //Calculate sse if we don't have it if(!havrnorm) for(i=0;i<*m;i++) rnorm += (fval[i]-ydata[i])*(fval[i]-ydata[i]); iterF.plhs[0] = NULL; memcpy(mxGetData(iterF.prhs[1]), &citer, sizeof(int)); memcpy(mxGetPr(iterF.prhs[2]), &rnorm, sizeof(double)); memcpy(mxGetPr(iterF.prhs[3]), x, *n * sizeof(double)); stat = mexCallMATLAB(1, iterF.plhs, 4, iterF.prhs, iterF.f); if(stat) mexErrMsgTxt("Error calling Callback Function!"); //Collect return argument stop = *(bool*)mxGetData(iterF.plhs[0]); // Clean up Ptr mxDestroyArray(iterF.plhs[0]); } //Update feval counter citer++; } //Jacobian Eval else if(*iflag == 2) { grad.plhs[0] = NULL; memcpy(mxGetPr(grad.prhs[grad.xrhs]), x, *n * sizeof(double)); stat = mexCallMATLAB(1, grad.plhs, grad.nrhs, grad.prhs, grad.f); if(stat) mexErrMsgTxt("Error calling Gradient Function!"); //Get Objective fval = mxGetPr(grad.plhs[0]); //Assign Gradient memcpy(fjac,fval,*m**n*sizeof(double)); // Clean up Ptr mxDestroyArray(grad.plhs[0]); } //Check for Ctrl-C if (utIsInterruptPending()) { utSetInterruptPending(false); /* clear Ctrl-C status */ mexPrintf("\nCtrl-C Detected. Exiting LM_DER...\n\n"); *iflag = -1; //terminate } //Check for iterfun terminate if (stop) { mexPrintf("\nIterFun called Stop. Exiting LM_DER...\n\n"); *iflag = -1; //terminate } //Check for maxtime expiry if(evaltime > maxtime) { mexPrintf("\nMaximum Solver Time Exceeded. Exiting LM_DER...\n\n"); *iflag = -1; //terminate } }