//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); }
int DSDPComputeX(DSDP dsdp){ int i,info; double pobj=0,ppobj2=0,ddobj,tracexs=0,tracexs2=0,rpinfeas=0,rpinfeas2=0,rpobjerr=0; double err1,cc,rrr,bigM,ymax,pfeastol=dsdp->pinfeastol; DSDPTerminationReason reason; DSDPVec AX=dsdp->ytemp; DSDPFunctionBegin; info=DSDPStopReason(dsdp,&reason);DSDPCHKERR(info); info=DSDPGetDDObjective(dsdp,&ddobj);DSDPCHKERR(info); info=DSDPGetMaxYElement(dsdp,&ymax);DSDPCHKERR(info); info=DSDPGetR(dsdp,&rrr); DSDPCHKERR(info); info=DSDPGetPenalty(dsdp,&bigM);DSDPCHKERR(info); info=DSDPGetScale(dsdp,&cc);DSDPCHKERR(info); dsdp->pdfeasible=DSDP_PDFEASIBLE; for (i=0;i<MAX_XMAKERS;i++){ if (i>0 && dsdp->xmaker[i].pstep<1) continue; info=DSDPComputeXVariables(dsdp,dsdp->xmaker[i].mu,dsdp->xmaker[i].y,dsdp->xmaker[i].dy,AX,&tracexs);DSDPCHKERR(info); info=DSDPVecGetC(AX,&pobj); DSDPCHKERR(info); info=DSDPVecGetR(AX,&dsdp->tracex); DSDPCHKERR(info); info=DSDPVecSetC(AX,0);DSDPCHKERR(info); info=DSDPVecSetR(AX,0);DSDPCHKERR(info); info=DSDPVecNormInfinity(AX,&rpinfeas);DSDPCHKERR(info); rpinfeas=rpinfeas/(dsdp->tracex+1); DSDPLogInfo(0,2,"POBJ: %4.4e, DOBJ: %4.4e\n",pobj,ddobj/cc); info=DSDPVecNorm2(AX,&err1);DSDPCHKERR(info); dsdp->tracexs=tracexs; dsdp->perror=err1; dsdp->pobj=cc*pobj; info=DSDPInspectXY(dsdp,dsdp->xmaker[i].mu,dsdp->xmaker[i].y,dsdp->xmaker[i].dy,AX,&tracexs2,&ppobj2,&rpinfeas2);DSDPCHKERR(info); rpinfeas2=rpinfeas2/(dsdp->tracex+1); /* rpinfeas is infeasibility of (P) while rpinfeas2 is infeasibility of (PP) */ DSDPLogInfo(0,2,"X P Infeas: %4.2e , PObj: %4.8e\n",rpinfeas,pobj*(cc)); DSDPLogInfo(0,2,"TOTAL P Infeas: %4.2e PObj: %4.8e\n",rpinfeas2,ppobj2*(cc)); rpobjerr= fabs(pobj-dsdp->ppobj)/(1+fabs(dsdp->ppobj)); if (rpinfeas2 < pfeastol){ /* (PP) must be close to feasible */ if (dsdp->rgap<0.1){ if (rpinfeas>pfeastol/100 && fabs(rrr)>dsdp->dinfeastol){ dsdp->pdfeasible=DSDP_PDUNKNOWN; DSDPLogInfo(0,2,"Warning: Try Increasing penalty parameter\n"); } else if (rpinfeas>pfeastol && ddobj>0 && ppobj2<0 && fabs(rrr)<dsdp->dinfeastol){ dsdp->pdfeasible=DSDP_UNBOUNDED; DSDPLogInfo(0,2,"Warning: D probably unbounded\n"); } else if (/* fabs(bigM)-dsdp->tracex < fabs(rrr) && rpinfeas<pfeastol */ fabs(rrr)>dsdp->dinfeastol){ dsdp->pdfeasible=DSDP_INFEASIBLE; DSDPLogInfo(0,2,"Warning: D probably infeasible \n"); } } i=i+10; break; } else { /* Step direction was not accurate enough to compute X from Schur complement */ DSDPLogInfo(0,2,"Try backup X\n"); info=DSDPSetConvergenceFlag(dsdp,DSDP_NUMERICAL_ERROR); DSDPCHKERR(info); } } DSDPFunctionReturn(0); }
void DSDPInternal::evaluate(int nfdir, int nadir) { int info; // Copy b vector for (int i=0; i<m_; ++i) { info = DSDPSetDualObjective(dsdp_, i+1, -input(SDP_B).at(i)); } // Get Ai from supplied A mapping_.setInput(input(SDP_C),0); mapping_.setInput(input(SDP_A),1); // Negate because the standard form in PSDP is different std::transform(mapping_.input(0).begin(), mapping_.input(0).end(), mapping_.input(0).begin(), std::negate<double>()); std::transform(mapping_.input(1).begin(), mapping_.input(1).end(), mapping_.input(1).begin(), std::negate<double>()); mapping_.evaluate(); for (int i=0; i<m_+1; ++i) { for (int j=0; j<nb_; ++j) { mapping_.output(i*nb_+j).get(values_[i][j],SPARSESYM); info = SDPConeSetASparseVecMat(sdpcone_, j, i, block_sizes_[j], 1, 0, &pattern_[i][j][0], &values_[i][j][0], pattern_[i][j].size() ); } } info = DSDPSetup(dsdp_); info = DSDPSolve(dsdp_); casadi_assert_message(info==0,"DSDPSolver failed"); DSDPTerminationReason reason; DSDPStopReason(dsdp_, &reason); std::cout << "Termination reason: " << (*terminationReason_.find(reason)).second << std::endl; DSDPSolutionType pdfeasible; DSDPGetSolutionType(dsdp_,&pdfeasible); std::cout << "Solution type: " << (*solutionType_.find(pdfeasible)).second << std::endl; info = DSDPGetY(dsdp_,&output(SDP_PRIMAL).at(0),m_); double temp; DSDPGetDDObjective(dsdp_, &temp); output(SDP_PRIMAL_COST).set(-temp); DSDPGetPPObjective(dsdp_, &temp); output(SDP_DUAL_COST).set(-temp); if (calc_dual_) { for (int j=0; j<nb_; ++j) { info = SDPConeComputeX(sdpcone_, j, block_sizes_[j], &store_X_[j][0], store_X_[j].size()); Pmapper_.input(j).set(store_X_[j],SPARSESYM); } Pmapper_.evaluate(); std::copy(Pmapper_.output().data().begin(),Pmapper_.output().data().end(),output(SDP_DUAL).data().begin()); } if (calc_p_) { for (int j=0; j<nb_; ++j) { info = SDPConeComputeS(sdpcone_, j, 1.0, &output(SDP_PRIMAL).at(0), m_, 0, block_sizes_[j] , &store_P_[j][0], store_P_[j].size()); Pmapper_.input(j).set(store_P_[j],SPARSESYM); } Pmapper_.evaluate(); std::copy(Pmapper_.output().data().begin(),Pmapper_.output().data().end(),output(SDP_PRIMAL_P).data().begin()); } }