Beispiel #1
0
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;
}
Beispiel #2
0
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);
}
Beispiel #5
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;
}
Beispiel #6
0
void lbfgsb_program::computeGradient (int n, double* x, double* g)
{
    double *gval;

    grad->plhs[0] = NULL;
    memcpy(mxGetPr(grad->prhs[grad->xrhs]), x, n * sizeof(double));

    try {
        mexCallMATLAB(1, grad->plhs, grad->nrhs, grad->prhs, grad->f);
        //Get Gradient
        gval = mxGetPr(grad->plhs[0]);
        memcpy(g,gval,n*sizeof(double));

        // Clean up Ptr
        mxDestroyArray(grad->plhs[0]);
    }
    catch(...)
    {
        mexWarnMsgTxt("Unrecoverable Error from Gradient Callback, Exiting LBFGSB...\n");
        //Force exit
        utSetInterruptPending(true);
    }
}
Beispiel #7
0
double lbfgsb_program::computeObjective (int n, double* x)
{
    double f = 0;

    fun->plhs[0] = NULL;
    memcpy(mxGetPr(fun->prhs[fun->xrhs]), x, n * sizeof(double));
    noFevals++;

    try {
        mexCallMATLAB(1, fun->plhs, fun->nrhs, fun->prhs, fun->f);
        //Get Objective
        f = mxGetScalar(fun->plhs[0]);
        // Clean up Ptr
        mxDestroyArray(fun->plhs[0]);
    }
    catch(...)
    {
        mexWarnMsgTxt("Unrecoverable Error from Objective Callback, Exiting LBFGSB...\n");
        //Force exit
        utSetInterruptPending(true);
    }
    return f;
}
Beispiel #8
0
//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]++;
    }
}
Beispiel #9
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
    }
}