📄 glpk02.tex
字号:
%* glpk02.tex *%\chapter{Basic API Routines}This chapter describes GLPK API routines intended for using inapplication programs.\subsubsection*{Library header}All GLPK API data types and routines are defined in the header file\verb|glpk.h|. It should be included in all source files which useGLPK API, either directly or indirectly through some other header fileas follows:\begin{verbatim} #include <glpk.h>\end{verbatim}\subsubsection*{Error handling}If some GLPK API routine detects erroneous or incorrect data passed bythe application program, it writes appropriate diagnostic messages tothe terminal and then abnormally terminates the application program.In most practical cases this allows to simplify programming by avoidingnumerous checks of return codes. Thus, in order to prevent crashing theapplication program should check all data, which are suspected to beincorrect, before calling GLPK API routines.Should note that this kind of error handling is used only in cases ofincorrect data passed by the application program. If, for example, theapplication program calls some GLPK API routine to read data from aninput file and these data are incorrect, the GLPK API routine reportsabout error in the usual way by means of the return code.\subsubsection*{Thread safety}Currently GLPK API routines are non-reentrant and therefore cannot beused in multi-threaded programs.\subsubsection*{Array indexing}Normally all GLPK API routines start array indexing from 1, not from 0(except the specially stipulated cases). This means, for example, thatif some vector $x$ of the length $n$ is passed as an array to some GLPKAPI routine, the latter expects vector components to be placed inlocations \verb|x[1]|, \verb|x[2]|, \dots, \verb|x[n]|, and the location\verb|x[0]| normally is not used.In order to avoid indexing errors it is most convenient and mostreliable to declare the array \verb|x| as follows:\begin{verbatim} double x[1+n];\end{verbatim}\noindentor to allocate it as follows:\begin{verbatim} double *x; . . . x = calloc(1+n, sizeof(double));\end{verbatim}\noindentIn both cases one extra location \verb|x[0]| is reserved that allowspassing the array to GLPK routines in a usual way.\section{Problem object}All GLPK API routines deal with so called {\it problem object}, whichis a program object of type \verb|glp_prob| and intended to representa particular LP or MIP instance.The type \verb|glp_prob| is a data structure declared in the headerfile \verb|glpk.h| as follows:\begin{verbatim} typedef struct { ... } glp_prob;\end{verbatim}Problem objects (i.e. program objects of the \verb|glp_prob| type) areallocated and managed internally by the GLPK API routines. Theapplication program should never use any members of the \verb|glp_prob|structure directly and should deal only with pointers to these objects(that is, \verb|glp_prob *| values).\pagebreakThe problem object consists of five segments, which are:$\bullet$ problem segment,$\bullet$ basis segment,$\bullet$ interior point segment,$\bullet$ MIP segment, and$\bullet$ control parameters and statistics segment.\subsubsection*{Problem segment}The {\it problem segment} contains original LP/MIP data, whichcorresponds to the problem formulation (1.1)---(1.3) (see Section\ref{seclp}, page \pageref{seclp}). It includes the followingcomponents:$\bullet$ rows (auxiliary variables),$\bullet$ columns (structural variables),$\bullet$ objective function, and$\bullet$ constraint matrix.Rows and columns have the same set of the following attributes:$\bullet$ ordinal number,$\bullet$ symbolic name (1 up to 255 arbitrary graphic characters),$\bullet$ type (free, lower bound, upper bound, double bound, fixed),$\bullet$ numerical values of lower and upper bounds,$\bullet$ scale factor.{\it Ordinal numbers} are intended for referencing rows and columns.Row ordinal numbers are integers $1, 2, \dots, m$, and column ordinalnumbers are integers $1, 2, \dots, n$, where $m$ and $n$ are,respectively, the current number of rows and columns in the problemobject.{\it Symbolic names} are intended for informational purposes. They alsocan be used for referencing rows and columns.{\it Types and bounds} of rows (auxiliary variables) and columns(structural variables) are explained above (see Section \ref{seclp},page \pageref{seclp}).{\it Scale factors} are used internally for scaling rows and columns ofthe constraint matrix.Information about the {\it objective function} includes numericalvalues of objective coefficients and a flag, which defines theoptimization direction (i.e. minimization or maximization).The {\it constraint matrix} is a $m \times n$ rectangular matrix builtof constraint coefficients $a_{ij}$, which defines the system of linearconstraints (1.2) (see Section \ref{seclp}, page \pageref{seclp}). Thismatrix is stored in the problem object in both row-wise and column-wisesparse formats.Once the problem object has been created, the application program canaccess and modify any components of the problem segment in arbitraryorder.\subsubsection*{Basis segment}The {\it basis segment} of the problem object keeps information relatedto the current basic solution. It includes:$\bullet$ row and column statuses,$\bullet$ basic solution statuses,$\bullet$ factorization of the current basis matrix, and$\bullet$ basic solution components.The {\it row and column statuses} define which rows and columns arebasic and which are non-basic. These statuses may be assigned either bythe application program of by some API routines. Note that thesestatuses are always defined independently on whether the correspondingbasis is valid or not.The {\it basic solution statuses} include the {\it primal status} andthe {\it dual status}, which are set by the simplex-based solver oncethe problem has been solved. The primal status shows whether a primalbasic solution is feasible, infeasible, or undefined. The dual statusshows the same for a dual basic solution.The {\it factorization of the basis matrix} is some factorized form(like LU-factorization) of the current basis matrix (defined by thecurrent row and column statuses). The factorization is used by thesimplex-based solver and kept when the solver terminates the search.This feature allows efficiently reoptimizing the problem after somemodifications (for example, after changing some bounds or objectivecoefficients). It also allows performing the post-optimal analysis (forexample, computing components of the simplex table, etc.).The {\it basic solution components} include primal and dual values ofall auxiliary and structural variables for the most recently obtainedbasic solution.\subsubsection*{Interior point segment}The {\it interior point segment} is automatically allocated after theproblem has been solved using the interior point solver. It containsinterior point solution components, which include the solution status,and primal and dual values of all auxiliary and structural variables.\subsubsection*{MIP segment}The {\it MIP segment} is used only for MIP problems. This segmentincludes:$\bullet$ column kinds,$\bullet$ MIP solution status, and$\bullet$ MIP solution components.The {\it column kinds} define which columns (i.e. structural variables)are integer and which are continuous.The {\it MIP solution status} is set by the MIP solver and shows whethera MIP solution is integer optimal, integer feasible (non-optimal), orundefined.The {\it MIP solution components} are computed by the MIP solver andinclude primal values of all auxiliary and structural variables for themost recently obtained MIP solution.Note that in case of MIP problem the basis segment corresponds tothe optimal solution of LP relaxation, which is also available to theapplication program.Currently the search tree is not kept in the MIP segment. Therefore ifthe search has been terminated, it cannot be continued.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\newpage\section{Problem creating and modifying routines}\subsection{glp\_create\_prob---create problem object}\subsubsection*{Synopsis}\begin{verbatim}glp_prob *glp_create_prob(void);\end{verbatim}\subsubsection*{Description}The routine \verb|glp_create_prob| creates a new problem object, whichinitially is ``empty'', i.e. has no rows and columns.\subsubsection*{Returns}The routine returns a pointer to the created object, which should beused in any subsequent operations on this object.\subsection{glp\_set\_prob\_name---assign (change) problem name}\subsubsection*{Synopsis}\begin{verbatim}void glp_set_prob_name(glp_prob *lp, const char *name);\end{verbatim}\subsubsection*{Description}The routine \verb|glp_set_prob_name| assigns a given symbolic\verb|name| (1 up to 255 characters) to the specified problem object.If the parameter \verb|name| is \verb|NULL| or empty string, the routineerases an existing symbolic name of the problem object.\subsection{glp\_set\_obj\_name---assign (change) objective functionname}\subsubsection*{Synopsis}\begin{verbatim}void glp_set_obj_name(glp_prob *lp, const char *name);\end{verbatim}\subsubsection*{Description}The routine \verb|glp_set_obj_name| assigns a given symbolic\verb|name| (1 up to 255 characters) to the objective function of thespecified problem object.If the parameter \verb|name| is \verb|NULL| or empty string, the routineerases an existing symbolic name of the objective function.\subsection{glp\_set\_obj\_dir---set (change) optimization direction\\flag}\subsubsection*{Synopsis}\begin{verbatim}void glp_set_obj_dir(glp_prob *lp, int dir);\end{verbatim}\subsubsection*{Description}The routine \verb|glp_set_obj_dir| sets (changes) the optimizationdirection flag (i.e. ``sense'' of the objective function) as specifiedby the parameter \verb|dir|:\begin{tabular}{@{}ll}\verb|GLP_MIN| & minimization; \\\verb|GLP_MAX| & maximization. \\\end{tabular}\noindent(Note that by default the problem is minimization.)\subsection{glp\_add\_rows---add new rows to problem object}\subsubsection*{Synopsis}\begin{verbatim}int glp_add_rows(glp_prob *lp, int nrs);\end{verbatim}\subsubsection*{Description}The routine \verb|glp_add_rows| adds \verb|nrs| rows (constraints) tothe specified problem object. New rows are always added to the end ofthe row list, so the ordinal numbers of existing rows are not changed.Being added each new row is initially free (unbounded) and has emptylist of the constraint coefficients.\subsubsection*{Returns}The routine \verb|glp_add_rows| returns the ordinal number of the firstnew row added to the problem object.\newpage\subsection{glp\_add\_cols---add new columns to problem object}\subsubsection*{Synopsis}\begin{verbatim}int glp_add_cols(glp_prob *lp, int ncs);\end{verbatim}\subsubsection*{Description}The routine \verb|glp_add_cols| adds \verb|ncs| columns (structuralvariables) to the specified problem object. New columns are always addedto the end of the column list, so the ordinal numbers of existingcolumns are not changed.Being added each new column is initially fixed at zero and has emptylist of the constraint coefficients.\subsubsection*{Returns}The routine \verb|glp_add_cols| returns the ordinal number of the firstnew column added to the problem object.\subsection{glp\_set\_row\_name---assign (change) row name}\subsubsection*{Synopsis}\begin{verbatim}void glp_set_row_name(glp_prob *lp, int i, const char *name);\end{verbatim}\subsubsection*{Description}The routine \verb|glp_set_row_name| assigns a given symbolic\verb|name| (1 up to 255 characters) to \verb|i|-th row (auxiliaryvariable) of the specified problem object.If the parameter \verb|name| is \verb|NULL| or empty string, the routineerases an existing name of $i$-th row.\subsection{glp\_set\_col\_name---assign (change) column name}\subsubsection*{Synopsis}\begin{verbatim}void glp_set_col_name(glp_prob *lp, int j, const char *name);\end{verbatim}\subsubsection*{Description}The routine \verb|glp_set_col_name| assigns a given symbolic\verb|name| (1 up to 255 characters) to \verb|j|-th column (structuralvariable) of the specified problem object.If the parameter \verb|name| is \verb|NULL| or empty string, the routineerases an existing name of $j$-th column.\subsection{glp\_set\_row\_bnds---set (change) row bounds}\subsubsection*{Synopsis}\begin{verbatim}void glp_set_row_bnds(glp_prob *lp, int i, int type, double lb, double ub);\end{verbatim}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -