void solve_lp(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { /* 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 error=dd_NoError; dd_LPSolverType solver=dd_CrissCross; /* either DualSimplex or CrissCross */ dd_LPPtr lp; /* pointer to LP data structure that is not visible by user. */ dd_set_global_constants(); /* First, this must be called once to use cddlib. */ /* Input an LP using the cdd library */ lp = MB_get_LP_MatrixPtr(prhs[0]); /* Solve the LP by cdd LP solver. */ dd_LPSolve(lp,solver,&error); if (error!=dd_NoError) dd_WriteErrorMessages(stdout, error); /* Take the solution. */ plhs[0] = MB_set_LPsol_MatrixPtr(lp); /* Free allocated spaces. */ dd_FreeLPData(lp); }
void find_interior_DS(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { /* uses Dual Simplex method */ /* We would like to find an iterior point for a polyhedron in H representation A x <= b. */ dd_ErrorType error=dd_NoError; dd_LPSolverType solver=dd_DualSimplex; dd_LPPtr lp, lp1; /* pointer to LP data structure that is not visible by user. */ dd_MatrixPtr A; int j; dd_set_global_constants(); /* First, this must be called once to use cddlib. */ /* Input an LP using the cdd library */ /* lp = MB_get_LP_MatrixPtr(prhs[0]); */ if (A=FT_get_H_MatrixPtr(prhs[0])) { /* set objective */ A->objective = dd_LPmin; for (j = 0; j < A->colsize; j++) dd_set_d(A->rowvec[j],0.0); lp=dd_Matrix2LP(A, &error); dd_FreeMatrix(A); }else{ mexErrMsgTxt("Error in the setting of LP matrix."); } lp1=dd_MakeLPforInteriorFinding(lp); dd_LPSolve(lp1,solver,&error); if (error!=dd_NoError) dd_WriteErrorMessages(stdout, error); /* Take the solution. */ plhs[0] = MB_set_LPsol_MatrixPtr(lp1); /* Free allocated spaces. */ dd_FreeLPData(lp); dd_FreeLPData(lp1); }
int main(int argc, char *argv[]) { dd_PolyhedraPtr poly; dd_LPPtr lp; dd_MatrixPtr M,A; dd_ErrorType err=dd_NoError; dd_DataFileType inputfile,outputfile; FILE *reading=NULL, *writing; dd_set_global_constants(); /* First, this must be called. */ if (argc>1) strcpy(inputfile,argv[1]); if (argc<=1 || !SetInputFile(&reading,argv[1])){ dd_WriteProgramDescription(stdout); dd_SetInputFile(&reading,inputfile, &err); } if (err==dd_NoError) { M=dd_PolyFile2Matrix(reading, &err); } else { printf("Input file not found\n"); goto _L99; } if (err!=dd_NoError) goto _L99; if (M->objective==dd_LPnone){ /* do representation conversion */ poly=dd_DDMatrix2Poly2(M, dd_LexMin, &err); /* equivalent to poly=dd_DDMatrix2Poly2(M, &err) when the second argument is set to dd_LexMin. */ if (err!=dd_NoError) goto _L99; dd_SetWriteFileName(inputfile, outputfile, 'o', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteProgramDescription(writing); dd_WriteRunningMode(writing, poly); switch (poly->representation) { case dd_Inequality: fprintf(writing, "ext_file: Generators\n"); A=dd_CopyGenerators(poly); dd_WriteMatrix(writing,A); dd_FreeMatrix(A); break; case dd_Generator: fprintf(writing, "ine_file: Inequalities\n"); A=dd_CopyInequalities(poly); dd_WriteMatrix(writing,A); dd_FreeMatrix(A); break; default: break; } dd_WriteDDTimes(writing,poly); fclose(writing); dd_SetWriteFileName(inputfile, outputfile, 'a', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteAdjacency(writing,poly); fclose(writing); dd_SetWriteFileName(inputfile, outputfile, 'j', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteInputAdjacency(writing,poly); fclose(writing); dd_SetWriteFileName(inputfile, outputfile, 'i', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteIncidence(writing,poly); fclose(writing); dd_SetWriteFileName(inputfile, outputfile, 'n', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteInputIncidence(writing,poly); fclose(writing); dd_FreeMatrix(M); dd_FreePolyhedra(poly); } else { /* solve the LP */ lp=dd_Matrix2LP(M, &err); if (err!=dd_NoError) goto _L99; dd_LPSolve(lp,dd_DualSimplex,&err); if (err!=dd_NoError) goto _L99; dd_SetWriteFileName(inputfile, outputfile, 's', M->representation); SetWriteFile(&writing, outputfile); dd_WriteLPResult(writing, lp, err); fclose(writing); dd_FreeMatrix(M); dd_FreeLPData(lp); } _L99: if (err!=dd_NoError) dd_WriteErrorMessages(stdout,err); return 0; }
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; }