📄 lpmex.c
字号:
/* lpmex('print_lp', lp_handle) */ else if (strcmp("print_lp",cmd)==0) { if (nrhs != 2) mexErrMsgTxt("print_lp requires 1 argument."); print_lp(lp[h]); } /* lpmex('print_scales', lp_handle) */ else if (strcmp("print_scales",cmd)==0) { if (nrhs != 2) mexErrMsgTxt("print_scales requires 1 argument."); print_scales(lp[h]); } /* lpmex('print_solution', lp_handle) */ else if (strcmp("print_solution",cmd)==0) { if (nrhs != 2) mexErrMsgTxt("print_solution requires 1 argument."); print_solution(lp[h]); } /* lpmex('reset_basis', lp_handle) */ else if (strcmp("reset_basis",cmd)==0) { if (nrhs != 2) mexErrMsgTxt("reset_basis requires 1 argument."); reset_basis(lp[h]); } /* lpmex('set_maxim', lp_handle) */ else if (strcmp("set_maxim",cmd)==0) { if (nrhs != 2) mexErrMsgTxt("set_maxim requires 1 argument."); set_maxim(lp[h]); } /* lpmex('set_minim', lp_handle) */ else if (strcmp("set_minim",cmd)==0) { if (nrhs != 2) mexErrMsgTxt("set_minim requires 1 argument."); set_minim(lp[h]); } /* [result] = lpmex('solve', lp_handle) */ else if (strcmp("solve",cmd)==0) { if (nrhs != 2) mexErrMsgTxt("solve requires 1 argument."); result = solve(lp[h]); plhs[0] = mxCreateDoubleMatrix(1,1,0); pr = mxGetPr(plhs[0]); pr[0] = result; if (result == OPTIMAL) { if (lp[h]->verbose == TRUE) { print_solution(lp[h]); printf("Branch & Bound depth: %d\n",lp[h]->max_level); printf("Nodes processed: %d\n",lp[h]->total_nodes); printf("Simplex pivots: %d\n",lp[h]->total_iter); } } if (result == INFEASIBLE) printf("This problem is infeasible\n"); if (result == UNBOUNDED) printf("This problem is unbounded\n"); if (result == FAILURE) printf("lp_solve failed\n"); } /* lpmex('unscale', lp_handle) */ else if (strcmp("unscale",cmd)==0) { if (nrhs != 2) mexErrMsgTxt("unscale requires 1 argument."); unscale(lp[h]); }/* functions with at least two arguments */ else if (nrhs < 3 ) { strcpy(errmsg,cmd); strncat(errmsg,": Unimplemented or requires at least 2 arguments.",180); mexErrMsgTxt(errmsg); } /* lpmex('add_column', lp_handle, col_vec) */ else if (strcmp("add_column",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("add_column requires 2 arguments."); } vec = mxCalloc(1+lp[h]->rows,sizeof(REAL)); /* GetRealVector can handle both full and sparse matrix */ GetRealVector(prhs[2],vec,lp[h]->rows); add_column(lp[h],vec); mxFree(vec); } /* [ans]=lpmex('column_in_lp', lp_handle, col_vec) */ /* returns TRUE if col_vec is already present as a column in lp. */ /*(Does not look at bounds and types, only looks at matrix values) */ else if (strcmp("column_in_lp",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("column_in_lp needs 2 arguments."); } vec = mxCalloc(2+lp[h]->rows,sizeof(REAL)); /* GetRealVector can handle both full and sparse matrix */ GetRealVector(prhs[2],vec,1+lp[h]->rows); plhs[0] = mxCreateDoubleMatrix(1,1,0); pr = mxGetPr(plhs[0]); pr[0] = column_in_lp(lp[h],vec+1); mxFree(vec); } /* lpmex('del_column', lp_handle, col) */ else if (strcmp("del_column",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("del_column requires 2 arguments."); } col = GetRealScalar(prhs[2]); if ((col >= 1) && (col <= lp[h]->columns)) { del_column(lp[h],col); } else { mexErrMsgTxt("column number out of bounds."); } } /* lpmex('del_constraint', lp_handle, row) */ else if (strcmp("del_constraint",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("del_constraint requires 2 arguments."); } row = GetRealScalar(prhs[2]); if ((row >= 1) && (row <= lp[h]->rows)) { del_constraint(lp[h],row); } else { mexErrMsgTxt("constraint number out of bounds."); } } /* [col_vec] = lpmex('get_column', lp_handle, col) */ else if (strcmp("get_column",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("get_column requires 2 arguments."); } col = GetRealScalar(prhs[2]); if ((col < 1) || (col > lp[h]->columns)) { mexErrMsgTxt("column out of range."); } vec = mxCalloc(1+lp[h]->rows,sizeof(REAL)); get_column(lp[h],col,vec); plhs[0] = mxCreateDoubleMatrix(lp[h]->rows,1,0); pr = mxGetPr(plhs[0]); for (i=0; i<lp[h]->rows; i++) { pr[i] = vec[i+1]; } mxFree(vec); } /* [row_vec] = lpmex('get_row', lp_handle, row) */ else if (strcmp("get_row",cmd)==0) { if (nrhs != 3) mexErrMsgTxt("get_row requires 2 arguments."); row = GetRealScalar(prhs[2]); if ((row < 1) || (row > lp[h]->rows)) { mexErrMsgTxt("row out of range."); } vec = mxCalloc(1+lp[h]->columns,sizeof(REAL)); get_row(lp[h],row,vec); plhs[0] = mxCreateDoubleMatrix(1,lp[h]->columns,0); pr = mxGetPr(plhs[0]); for (i=0; i<lp[h]->columns; i++) { pr[i] = vec[i+1]; } mxFree(vec); } /* [ans]=lpmex('is_feasible', lp_handle, sol_vec) */ /* returns TRUE if the vector in values is a feasible solution to the lp */ else if (strcmp("is_feasible",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("is_feasible needs 2 arguments."); } vec = mxCalloc(1+lp[h]->rows,sizeof(REAL)); /* GetRealVector can handle both full and sparse matrix */ GetRealVector(prhs[2],vec,lp[h]->rows); plhs[0] = mxCreateDoubleMatrix(1,1,0); pr = mxGetPr(plhs[0]); pr[0] = is_feasible(lp[h],vec); mxFree(vec); } /* lpmex('lp_options', lp_handle, opt_str) */ /* Set options for the lp. See README_MEX for details*/ else if (strcmp("lp_options",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("lp_options needs 2 arguments."); } str = mxCalloc(73,sizeof(char)); mxGetString(prhs[2], str, 73); lp[h]->verbose = (strstr(str,"-v")) ? TRUE:FALSE; lp[h]->debug = (strstr(str,"-d")) ? TRUE:FALSE; lp[h]->print_duals = (strstr(str,"-p")) ? TRUE:FALSE; lp[h]->print_sol = (strstr(str,"-i")) ? TRUE:FALSE; lp[h]->floor_first = (strstr(str,"-c")) ? FALSE:TRUE; lp[h]->print_at_invert = (strstr(str,"-I")) ? TRUE:FALSE; lp[h]->trace = (strstr(str,"-t")) ? TRUE:FALSE; lp[h]->anti_degen = (strstr(str,"-degen")) ? TRUE:FALSE; if (strstr(str,"-s")) auto_scale(lp[h]); else unscale(lp[h]); if (str1=strstr(str,"-b")) lp[h]->obj_bound = atof(str1 + 3); else lp[h]->obj_bound = (REAL)DEF_INFINITE; if (str1=strstr(str,"-e")) lp[h]->epsilon = atof(str1 + 3); else lp[h]->epsilon = (REAL)DEF_EPSILON; mxFree(str); } /* lpmex('set_mat', lp_handle, a) Full matrix argument */ /* lpmex('set_mat', lp_handle, a) Sparse matrix argument */ /* lpmex('set_mat', lp_handle, row, col, value) */ else if (strcmp("set_mat",cmd)==0) { if ( (nrhs!=3) && (nrhs!=5) ) mexErrMsgTxt("incorrect number of arguments."); if (nrhs == 5) { row = GetRealScalar(prhs[2]); if ((lp[h]->rows < row)||(row < 0)) { mexErrMsgTxt("set_mat invalid row."); } col = GetRealScalar(prhs[3]); if ((lp[h]->columns < col)||(col < 0)) { mexErrMsgTxt("set_mat invalid column."); } value = GetRealScalar(prhs[4]); set_mat(lp[h],row,col,value); } else if (nrhs == 3) { /* Called with a matrix argument */ m = mxGetM(prhs[2]); n = mxGetN(prhs[2]); if ((lp[h]->rows != m) || (lp[h]->columns != n) || !mxIsNumeric(prhs[2]) || mxIsComplex(prhs[2])) { mexErrMsgTxt("invalid argument."); } pr = mxGetPr(prhs[2]); if (!mxIsSparse(prhs[2])) { for (j=0; j<n; j++) { for (l=0; l<m; l++) { set_mat(lp[h],l+1,j+1,pr[l+j*m]); } } } if (mxIsSparse(prhs[2])) { jc = mxGetJc(prhs[2]); ir = mxGetIr(prhs[2]); for (j=0; j<n; j++) { for (k = jc[j]; k<jc[j+1]; k++) { i = ir[k]; set_mat(lp[h],i+1,j+1,pr[k]); } } } } } /* lpmex('set_obj_fn', lp_handle, vec) Sparse or Full arguments */ else if (strcmp("set_obj_fn",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("set_obj_fn requires 2 arguments."); } vec = mxCalloc(1+lp[h]->columns,sizeof(REAL)); /* GetRealVector can handle both full and sparse matrix */ GetRealVector(prhs[2],vec,lp[h]->columns); set_obj_fn(lp[h],vec); mxFree(vec); } /* lpmex('set_rh', lp_handle, row, value) */ else if (strcmp("set_rh",cmd)==0) { if (nrhs != 4) { mexErrMsgTxt("set_rh requires 3 arguments."); } row = GetRealScalar(prhs[2]); value = GetRealScalar(prhs[3]); set_rh(lp[h],row,value); } /* lpmex('set_rh_vec', lp_handle, vec) Sparse of Full arguments */ else if (strcmp("set_rh_vec",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("set_rh_vec requires 2 arguments."); } vec = mxCalloc(1+lp[h]->rows,sizeof(REAL)); /* GetRealVector can handle both full and sparse matrix */ GetRealVector(prhs[2],vec,lp[h]->rows); set_rh_vec(lp[h],vec); mxFree(vec); } /* lpmex('write_LP', lp_handle, filename) */ /* Write lp to a LP file */ else if (strcmp("write_LP",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("write_LP requires 2 arguments."); } mxGetString(prhs[2], filename, NAMELEN); if ((fp = fopen(filename, "w")) == NULL) { mexErrMsgTxt("write_LP can't write to the file."); } write_LP(lp[h], fp); fclose(fp); } /* lpmex('write_MPS', lp_handle, filename) */ /* Write lp to a MPS file */ else if (strcmp("write_MPS",cmd)==0) { if (nrhs != 3) { mexErrMsgTxt("write_MPS requires 2 arguments."); } mxGetString(prhs[2], filename, NAMELEN); if ((fp = fopen(filename, "w")) == NULL) { mexErrMsgTxt("write_MPS can't write to the file."); } write_MPS(lp[h], fp); fclose(fp); }/* functions with at least three arguments */ else if (nrhs < 4 ) { strcpy(errmsg,cmd); strncat(errmsg,": Unimplemented or requires at least 3 arguments.",180); mexErrMsgTxt(errmsg); } /* [value] = lpmex('mat_elm',lp_handle,row,col) get a single element from the matrix.*/ else if (strcmp("mat_elm",cmd)==0) { if (nrhs != 4) { mexErrMsgTxt("mat_elm needs 3 arguments."); } row = GetRealScalar(prhs[2]); if ((row < 1) || (row > lp[h]->rows)) { mexErrMsgTxt("row number out of bounds."); } col = GetRealScalar(prhs[3]); if ((col < 1) && (col > lp[h]->columns)) { mexErrMsgTxt("column number out of bounds."); } plhs[0] = mxCreateDoubleMatrix(1,1,0); pr = mxGetPr(plhs[0]); pr[0] = mat_elm(lp[h],row,col); } /* lpmex('set_constr_type', lp_handle, row, type) */ else if (strcmp("set_constr_type",cmd)==0) { if (nrhs != 4) { mexErrMsgTxt("set_constr_type needs 3 arguments."); } row = GetRealScalar(prhs[2]); if ((row < 1) || (row > lp[h]->rows)) { mexErrMsgTxt("row number out of bounds."); } type = GetRealScalar(prhs[3]); if ((type!=EQ) && (type!=LE) && (type!=GE)) { mexErrMsgTxt("invalid constraint type."); } set_constr_type(lp[h],row,type); } /* lpmex('set_int', lp_handle, col, type) */ else if (strcmp("set_int",cmd)==0) { if (nrhs != 4) { mexErrMsgTxt("set_int requires 3 arguments."); } col = GetRealScalar(prhs[2]); type = GetRealScalar(prhs[3]); if (type == 0) set_int(lp[h],col,FALSE); else set_int(lp[h],col,TRUE); } /* lpmex('set_lowbo', lp_handle, col,value) */ else if (strcmp("set_lowbo",cmd)==0) { if (nrhs != 4) { mexErrMsgTxt("set_lowbo requires 3 arguments."); } col = GetRealScalar(prhs[2]); value = GetRealScalar(prhs[3]); set_lowbo(lp[h],col,value); } /* lpmex('set_upbo', lp_handle, col, value) */ else if (strcmp("set_upbo",cmd)==0) { if (nrhs != 4) { mexErrMsgTxt("set_upbo requires 3 arguments."); } col = GetRealScalar(prhs[2]); value = GetRealScalar(prhs[3]); set_upbo(lp[h],col,value); }/* functions with more than three arguments */ else if (nrhs < 5 ) { strcpy(errmsg,cmd); strncat(errmsg,": Unimplemented or requires at least 4 arguments.",180); mexErrMsgTxt(errmsg); } /* lpmex('add_constraint', lp_handle, vec, type, rh) Sparse or Full arguments */ else if (strcmp("add_constraint", cmd)==0) { if (nrhs != 5) { mexErrMsgTxt("add_constraint requires 4 arguments."); } type = GetRealScalar(prhs[3]); if ((type!=LE) && (type!=EQ) && (type!=GE)) { mexErrMsgTxt("invalid constraint type."); } value = GetRealScalar(prhs[4]); vec = mxCalloc(1+lp[h]->columns,sizeof(REAL)); /* GetRealVector can handle both full and sparse matrix */ GetRealVector(prhs[2],vec,lp[h]->columns); add_constraint(lp[h],vec,type,value); mxFree(vec); } else { strcpy(errmsg,cmd); strncat(errmsg,": Unimplemented.",180); mexErrMsgTxt(errmsg); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -