[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-glpk] GLPK Code
From: |
Heinrich Schuchardt |
Subject: |
Re: [Help-glpk] GLPK Code |
Date: |
Tue, 21 Apr 2015 22:14:01 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.6.0 |
Hello Lance,
in your program you are suppressing the terminal output.
This output would tell you the details of the error in the library.
You should check the return value of glp_mpl_generate before calling
glp_mpl_build_prob.
I have updated
http://sourceforge.net/p/glpk-java/code/HEAD/tree/trunk/examples/java/Gmpl.java
to show the correct exception handling.
Best regards
Heinrich
On 20.04.2015 23:14, Lance Broad wrote:
> Hello Heinrich
>
> I am solving an LP using a Java wrapper for GMPL/GLPK and have decided
> to separate out the generation and the solution phase of the problem -
> placing each under separate Menu items.
>
> I have placed the generation/solution code under the control of menu
> items and invoke the Generate method in the following class (see below)
> to produce CPlex output.
>
> The code works perfectly the first time I invoke it with a Generate menu
> item click after starting up the application. If I invoke it again I
> get the following exception event: function glp_mpl_build_prob failed.
> My generator log indicates that the data being presented to generate the
> problem the second time around is correct.
>
> The menu item invocation for generation includes:
>
> // instantiate GMPL class
> myGmpl = new Gmpl();
> // execute GMPL Generate and produce CPLEX.lp problem file
> myGmpl.Generate();
>
> If you can shed any light on the glp_mpl_build_prob failure I would be
> extremely grateful.
>
> Many Thanks
>
> Lance Broad
>
>
>
>
>
> package org.GMPL;
>
> import org.apache.log4j.Logger;
> import org.JDBCsqlite.DBhandler;
> import org.gnu.glpk.GLPK;
> import org.gnu.glpk.GLPKConstants;
> import org.gnu.glpk.GlpkException;
> import org.gnu.glpk.glp_smcp;
> import org.gnu.glpk.glp_prob;
> import org.gnu.glpk.glp_cpxcp;
> import org.gnu.glpk.glp_tran;
> import org.gnu.glpk.GlpkTerminal;
> import org.gnu.glpk.GlpkTerminalListener;
> import org.user.UserFormulation;
> import org.Gui.Gui;
>
>
>
>
>
> // import org.GMPL.Gmpl requires public class declaration
> public class Gmpl implements GlpkTerminalListener
> ////////////////////////////////////////////////////////////////////////////////////////////////////
>
> //Java wrapper for co-ordination of execution of GMPL modeling language
> code
> ////////////////////////////////////////////////////////////////////////////////////////////////////
>
> {
> // java logger log4j - get class name to be logged
> private static final Logger log = Logger.getLogger(Gmpl.class.getName());
>
>
>
>
>
> public Gmpl()
> {
> // any constructor code goes here
> }
>
>
>
>
>
> public void Generate()
> ////////////////////////////////////////////////////////////////////////////////////////////////////
>
> //GNU GLPK control language for CPLEX format generation
> ////////////////////////////////////////////////////////////////////////////////////////////////////
>
> //note - method is invoked from Gui LP menu Generate item
> ////////////////////////////////////////////////////////////////////////////////////////////////////
>
> {
> // flag controlling access to data section in GMPL code
> int skip = 0;
> // return status from GLPK procedure calls
> int ret;
>
> glp_prob lp = null;
> glp_tran tran;
>
> try
> {
> log.info("Start GLPK problem generation");
> // set the terminal hook to call GlpkTerminal
> GLPK.glp_term_hook(null, null);
> // add listener to GlpkTerminal
> try
> {
> GlpkTerminal.addListener(this);
> }
> catch (Exception ex)
> {
> log.error("Exception event encountered during GLPK Terminal
> Listener addition.");
>
> System.exit(0);
> }
> // create lp problem handle
> lp = GLPK.glp_create_prob();
> // allocate workspace
> tran = GLPK.glp_mpl_alloc_wksp();
> // read GMPL model
> ret = GLPK.glp_mpl_read_model(tran, "GMPL32.mod", skip);
>
> if (ret != 0)
> {
> try
> {
> // free workspace
> GLPK.glp_mpl_free_wksp(tran);
> // delete problem
> GLPK.glp_delete_prob(lp);
> // throw runtime exception
> throw new RuntimeException("");
> }
> catch (Exception ex)
> {
> log.error("Encountered exception during read on GMPL file
> GMPL32.mod.");
>
> System.exit(0);
> }
> }
> // generate model
> GLPK.glp_mpl_generate(tran, null);
> // build model
> GLPK.glp_mpl_build_prob(tran, lp);
> // write problem data in CPLEX format as report
> GLPK.glp_write_lp(lp, null, UserFormulation.projectdirpath + "/" +
> "CPLEX.lp");
> // free workspace memory
> GLPK.glp_mpl_free_wksp(tran);
> // delete problem object
> GLPK.glp_delete_prob(lp);
> // indicate successful matrix generation
> UserFormulation.isgenerateok = true;
>
> log.info("Finish GLPK problem generation\n");
> }
> catch (GlpkException ex)
> {
> log.error("Encountered GLPK exception event.");
>
> System.exit(0);
> }
> }
>
>
>
>
>
> public boolean output(final String str)
> ////////////////////////////////////////////////////////////////////////////////////////////////////
>
> // Implements GLPKTerminalListener.output(String) by over-riding as part
> of GLPK terminal listener
> ////////////////////////////////////////////////////////////////////////////////////////////////////
>
> {
> // suppess GLPK generated console output
> return false;
> }
>
>
>
>
>
> //end class Gmpl
> }
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: [Help-glpk] GLPK Code,
Heinrich Schuchardt <=