help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-glpk] a code to solve mip


From: Andrew Makhorin
Subject: [Help-glpk] a code to solve mip
Date: Thu, 25 Sep 2008 00:18:35 +0400

> Is this code safe to solve a MIP (sorry, perhaps that is already out
> of scope for bug-glpk, just didn't want to open new thread on
> help-glpk):
> (to understand code, eprintf just printf alike)

>     // read lp...
>     LPX *lp = lpx_read_cpxlp (in_filename);
>     if (!lp)
>         eprintf (C_TAG_FATAL, 0, "cannot open '%s', CPLEX LP file processing 
> error", in_filename);

>    /**
>      * this is a integer problem...
>      */
>     switch (lpx_intopt (lp))
>     {
>         // OK
>         case LPX_E_OK:
>             break;

>         // infeasible
>         case LPX_E_NOFEAS:
>         case LPX_E_NOPFS:
>         case LPX_E_NODFS:
>             eprintf (C_TAG_ERROR, 0, "this problem is infeasible or 
> unbounded");
>             writeEmptyOutput();
>             exit (EXIT_SUCCESS);
>             break;

>         // uh oh...
>         default:
>             eprintf (C_TAG_FATAL, 0, "no optimal solution found, solving 
> failed");
>             break;
>     }

>     /**
>      * status.....
>      */
>     switch (glp_mip_status (lp))
>     {
>         // optimal
>         case GLP_OPT:
>             deprintf (C_TAG_INFO, 0, "optimal solution found");
>             break;

>         // infeasible
>         case GLP_INFEAS:
>             eprintf (C_TAG_ERROR, 0, "this problem is infeasible");
>             writeEmptyOutput();
>             exit (EXIT_SUCCESS);
>             break;

>         // no feasible
>         case GLP_NOFEAS:
>             eprintf (C_TAG_ERROR, 0, "this problem has no feasible solution");
>             writeEmptyOutput();
>             exit (EXIT_SUCCESS);
>             break;

>         // unbounded
>         case GLP_UNBND:
>             eprintf (C_TAG_ERROR, 0, "this problem is unbounded");
>             writeEmptyOutput();
>             exit (EXIT_SUCCESS);
>             break;

>         // uh oh...
>         default:
>             eprintf (C_TAG_FATAL, 0, "no optimal solution found, solving 
> failed");
>             break;
>     }

>     // try to open output filename
>     FILE *output = fopen (out_filename, "wt");
>     if (!output)
>         eprintf (C_TAG_FATAL, 0, "cannot open output file '%s'", 
> out_filename);

>     /**
>      * objective function
>      */
>     double objVal = glp_mip_obj_val (lp);
>     deprintf (C_TAG_INFO, 0, "value of objective function: "REALFORMAT, 
> objVal);
>     fprintf (output, "Value of objective function: " REALFORMAT "\n", objVal);

>     /* var values... */
>     for (int col = 1; col <= glp_get_num_cols (lp); ++col)
>     {
>         const char *colName = glp_get_col_name (lp, col);
>         double vx = glp_mip_col_val (lp, col);

>         if (!colName)
>         {
>             fclose (output);
>             eprintf (C_TAG_FATAL, 0, "column %d without name in result", col);
>         }

>         fprintf (output, "%-20s " REALFORMAT "\n", colName, vx);
>     }

>     // close file...
>     fclose (output);

The code looks correct. (Note that lpx_read_cpxlp is deprecated and
was replaced by glp_read_lp; for more details see the glpk reference
manual.) Besides, I think it is more reasonable to use glp_simplex +
glp_intopt rather than lpx_intopt, because they provide more features
to control the solution process.

Andrew Makhorin







reply via email to

[Prev in Thread] Current Thread [Next in Thread]