int main(int argc, char *argv[]) { /* The original LP data m x n matrix = | b -A | | c0 c^T |, where the LP to be solved is to maximize c^T x + c0 subj. to A x <= b. */ dd_ErrorType err=dd_NoError; dd_LPSolverType solver=dd_DualSimplex; /* either DualSimplex or CrissCross */ dd_LPPtr lp,lp1; /* pointer to LP data structure that is not visible by user. */ dd_LPSolutionPtr lps,lps1; /* pointer to LP solution data that is visible by user. */ dd_MatrixPtr M; dd_colrange j; dd_DataFileType inputfile; dd_set_global_constants(); printf("\n--- Solving an LP with dd_LPSolve, and Finding an Interior Point ---\n"); /* Input an LP using the cdd library */ dd_SetInputFile(&reading,inputfile,&err); if (err!=dd_NoError) goto _L99; M=dd_PolyFile2Matrix(reading, &err); if (err!=dd_NoError) goto _L99; /* dd_WriteMatrix(stdout, M); */ lp=dd_Matrix2LP(M, &err); if (err!=dd_NoError) goto _L99; /* Solve the LP by cdd LP solver. */ printf("\n--- Running dd_LPSolve ---\n"); solver=dd_DualSimplex; dd_LPSolve(lp, solver, &err); /* Solve the LP */ if (err!=dd_NoError) goto _L99; /* Write the LP solutions by cdd LP reporter. */ /* dd_WriteLPResult(stdout, lp, err); */ /* dd_WriteLPResult(writing, lp, err); */ /* One can access the solutions by loading them. See dd_WriteLPResult for outputing the results correctly. */ lps=dd_CopyLPSolution(lp); if (lps->LPS==dd_Optimal){ printf("Optimal solution found:\n"); printf(" primal_solution\n"); for (j=1; j<lps->d; j++) { printf(" %3ld : ",j); dd_WriteNumber(stdout,lps->sol[j]); printf("\n"); } printf(" dual_solution\n"); for (j=1; j<lps->d; j++){ if (lps->nbindex[j+1]>0) { printf(" %3ld : ",lps->nbindex[j+1]); dd_WriteNumber(stdout,lps->dsol[j]); printf("\n"); } } printf(" optimal_value : "); dd_WriteNumber(stdout,lps->optvalue); printf("\n"); } /* Find an interior point with cdd LP library. */ printf("\n--- Running dd_FindInteriorPoint ---\n"); lp1=dd_MakeLPforInteriorFinding(lp); printf("The LP to be solved for finding an interior point:\n"); dd_WriteLP(stdout,lp1); dd_LPSolve(lp1,solver,&err); if (err!=dd_NoError) goto _L99; /* Write an interior point. */ lps1=dd_CopyLPSolution(lp1); if (dd_Positive(lps1->optvalue)){ printf("\nAn interior point found: ("); for (j=1; j <(lps1->d)-1; j++) { dd_WriteNumber(stdout,lps1->sol[j]); } printf(")\n"); } if (dd_Negative(lps1->optvalue)) printf("\nThe feasible region is empty.\n"); if (dd_EqualToZero(lps1->optvalue)) printf("\nThe feasible region is nonempty but has no interior point.\n"); /* Free allocated spaces. */ dd_FreeLPSolution(lps); dd_FreeLPData(lp); dd_FreeLPSolution(lps1); dd_FreeLPData(lp1); dd_FreeMatrix(M); _L99:; if (err!=dd_NoError) dd_WriteErrorMessages(stdout, err); dd_free_global_constants(); /* At the end, this should be called. */ return 0; }
static dd_ErrorType FaceEnumHelper(dd_MatrixPtr M, dd_rowset R, dd_rowset S) { dd_ErrorType err; dd_rowset LL, ImL, RR, SS, Lbasis; dd_rowrange iprev = 0; dd_colrange dim; dd_LPSolutionPtr lps = NULL; set_initialize(&LL, M->rowsize); set_initialize(&RR, M->rowsize); set_initialize(&SS, M->rowsize); set_copy(LL, M->linset); set_copy(RR, R); set_copy(SS, S); /* note actual type of "value" is mpq_t (defined in cddmp.h) */ mytype value; dd_init(value); err = dd_NoError; dd_boolean foo = dd_ExistsRestrictedFace(M, R, S, &err); if (err != dd_NoError) { #ifdef MOO fprintf(stderr, "err from dd_ExistsRestrictedFace\n"); fprintf(stderr, "err = %d\n", err); #endif /* MOO */ set_free(LL); set_free(RR); set_free(SS); dd_clear(value); return err; } if (foo) { set_uni(M->linset, M->linset, R); err = dd_NoError; dd_FindRelativeInterior(M, &ImL, &Lbasis, &lps, &err); if (err != dd_NoError) { #ifdef MOO fprintf(stderr, "err from dd_FindRelativeInterior\n"); fprintf(stderr, "err = %d\n", err); #endif /* MOO */ dd_FreeLPSolution(lps); set_free(ImL); set_free(Lbasis); set_free(LL); set_free(RR); set_free(SS); dd_clear(value); return err; } dim = M->colsize - set_card(Lbasis) - 1; set_uni(M->linset, M->linset, ImL); SEXP mydim, myactive, myrip; PROTECT(mydim = ScalarInteger(dim)); PROTECT(myactive = rr_set_fwrite(M->linset)); int myd = (lps->d) - 2; PROTECT(myrip = allocVector(STRSXP, myd)); for (int j = 1; j <= myd; j++) { dd_set(value, lps->sol[j]); char *zstr = NULL; zstr = mpq_get_str(zstr, 10, value); SET_STRING_ELT(myrip, j - 1, mkChar(zstr)); free(zstr); } REPROTECT(dimlist = CONS(mydim, dimlist), dimidx); REPROTECT(riplist = CONS(myrip, riplist), ripidx); REPROTECT(activelist = CONS(myactive, activelist), activeidx); UNPROTECT(3); dd_FreeLPSolution(lps); set_free(ImL); set_free(Lbasis); if (dim > 0) { for (int i = 1; i <= M->rowsize; i++) { if ((! set_member(i, M->linset)) && (! set_member(i, S))) { set_addelem(RR, i); if (iprev) { set_delelem(RR, iprev); set_delelem(M->linset, iprev); set_addelem(SS, iprev); } iprev = i; err = FaceEnumHelper(M, RR, SS); if (err != dd_NoError) { #ifdef MOO fprintf(stderr, "err from FaceEnumHelper\n"); fprintf(stderr, "err = %d\n", err); #endif /* MOO */ set_copy(M->linset, LL); set_free(LL); set_free(RR); set_free(SS); dd_clear(value); return err; } } } } } set_copy(M->linset, LL); set_free(LL); set_free(RR); set_free(SS); dd_clear(value); return dd_NoError; }