help-glpk
[Top][All Lists]
Advanced

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

Re: [Help-glpk] java glpk


From: xypron . glpk
Subject: Re: [Help-glpk] java glpk
Date: Tue, 22 Jan 2013 19:18:10 +0100 (CET)

Hello David,

passing zero elements to the matrix only impairs the performance and not the 
result of the optimization.

Best regards

Heinrich Schuchardt

Gesendet mit der kostenlosen GMX iPhone App

Am 22.01.13 um 18:53 schrieb David Gabriel

> Hi,
>
> Thanks a lot for your continous support.
>
>
>
> I read in the documentation that is possible to pass 0 element to matrix.
>
> It will be easier for me to insert 0 in my matrix because I am writing my
>
> Linear problem dynamically (I set the parameters in java program) basing on
>
> inputs which always change .
>
> So I want to know if I keep these 0, my program will be wrong ?
>
>
>
> Thanks you.
>
>
> 
> 2013/1/15 Heinrich Schuchardt <address@hidden>
>
>
>
> > Hello David,
>
> >
>
> > keep care of the indexes used (e.g. calling GLPK.glp_set_col_kind).
>
> >
>
> > No need to pass zero elements of matrix.
>
> >
>
> > Memory assigned should be deleted.
>
> >
>
> > Integer problems are solved with glp_intopt.
>
> >
>
> > Cheers
>
> >
>
> > Heinrich
>
> >
>
> > package com.mycompany.mavenproject1;
>
> >
>
> > import org.gnu.glpk.GLPK;
>
> > import org.gnu.glpk.GLPKConstants;
>
> > import org.gnu.glpk.GlpkException;
>
> > import org.gnu.glpk.glp_iocp;
>
> > import org.gnu.glpk.glp_prob;
>
> > import org.gnu.glpk.SWIGTYPE_p_int;
>
> > import org.gnu.glpk.SWIGTYPE_p_double;
>
> >
>
> > /**
>
> >  * Hello world!
> 
> >  *
>
> >  */
>
> > public class App {
>
> >
>
> >     public static void main(String[] args) {
>
> >         /*
>
> >          * Minimize z = 0.5 x1 + 3 x2 + 2 x3 + 10 x4
>
> >          * Subject to
>
> >          * x1, x2, x3, x4 >= 0
>
> >          * x1, x2, x3, x4 are integers
>
> >          * x1 + 0.7 x2 = 4 //c1
>
> >          * x3 + 2 x2 >= 3 //c2
>
> >          * x4 + 2x1  >= 4 //c3
>
> >          */
>
> >
>
> >         glp_prob lp;
>
> >         glp_iocp iocp;
>
> >         SWIGTYPE_p_int ind;
>
> >         SWIGTYPE_p_double val;
>
> >         int ret;
>
> >         try {
>
> >
>
> >             // Create problem
>
> >             lp = GLPK.glp_create_prob();
>
> >             System.out.println("Problem created");
>
> >             GLPK.glp_set_prob_name(lp, "myProblem");
>
> >
>
> >             // Define columns
>
> >             GLPK.glp_add_cols(lp, 4);
>
> >
>
> >             GLPK.glp_set_col_name(lp, 1, "x1");
>
> >             GLPK.glp_set_col_kind(lp, 1, GLPKConstants.GLP_IV);
>
> >             GLPK.glp_set_col_bnds(lp, 1, GLPKConstants.GLP_LO, 0, 0);
>
> >
>
> >             GLPK.glp_set_col_name(lp, 2, "x2");
>
> >             GLPK.glp_set_col_kind(lp, 2, GLPKConstants.GLP_IV);
>
> >             GLPK.glp_set_col_bnds(lp, 2, GLPKConstants.GLP_LO, 0, 0);
>
> >
>
> >             GLPK.glp_set_col_name(lp, 3, "x3");
>
> >             GLPK.glp_set_col_kind(lp, 3, GLPKConstants.GLP_IV);
>
> >             GLPK.glp_set_col_bnds(lp, 3, GLPKConstants.GLP_LO, 0, 0);
>
> >
>
> >             GLPK.glp_set_col_name(lp, 4, "x4");
>
> >             GLPK.glp_set_col_kind(lp, 4, GLPKConstants.GLP_IV);
>
> >             GLPK.glp_set_col_bnds(lp, 4, GLPKConstants.GLP_LO, 0, 0);
>
> >
>
> >             // Create constraints
>
> >             GLPK.glp_add_rows(lp, 3);
>
> >
>
> >             //x1 + 0.7 x2 = 4 //c1
>
> >             GLPK.glp_set_row_name(lp, 1, "c1");
>
> >             GLPK.glp_set_row_bnds(lp, 1, GLPKConstants.GLP_FX, 4, 4);
>
> >
>
> >             //@TODO use correct length
>
> >             ind = GLPK.new_intArray(3);
>
> >             val = GLPK.new_doubleArray(3);
>
> >
>
> >             GLPK.intArray_setitem(ind, 1, 1);
>
> >             GLPK.doubleArray_setitem(val, 1, 1.0);
>
> >             GLPK.intArray_setitem(ind, 2, 2);
>
> >             GLPK.doubleArray_setitem(val, 2, 0.7);
>
> >             GLPK.glp_set_mat_row(lp, 1, 2, ind, val);
>
> >
>
> >             // x3 + 2 x2 >= 3 //c2
>
> >             GLPK.glp_set_row_name(lp, 2, "c2");
>
> >             GLPK.glp_set_row_bnds(lp, 2, GLPKConstants.GLP_LO, 3, 0);
>
> >
>
> >             GLPK.intArray_setitem(ind, 1, 2);
>
> >             GLPK.doubleArray_setitem(val, 1, 2);//x2
>
> >             GLPK.intArray_setitem(ind, 2, 3);
>
> >             GLPK.doubleArray_setitem(val, 2, 1);//x3
>
> >             GLPK.glp_set_mat_row(lp, 2, 2, ind, val);
>
> >
>
> >             //x4 + 2x1 >= 4 //c3
>
> >             GLPK.glp_set_row_name(lp, 3, "c3");
>
> >             GLPK.glp_set_row_bnds(lp, 3, GLPKConstants.GLP_LO, 4, 0);
>
> >
>
> >             GLPK.intArray_setitem(ind, 1, 1);
>
> >             GLPK.doubleArray_setitem(val, 2, 1);//x4
>
> >             GLPK.intArray_setitem(ind, 2, 4);
>
> >             GLPK.doubleArray_setitem(val, 1, 2);//x1
>
> >             GLPK.glp_set_mat_row(lp, 3, 2, ind, val);
>
> >
>
> >             // delete Arrays after usage
>
> >             GLPK.delete_doubleArray(val);
>
> >             GLPK.delete_intArray(ind);
>
> >
>
> >             // Define objective
>
> >             //Minimize z = 0.5 x1 + 3 x2 + 2 x3 + 10 x4
>
> >             GLPK.glp_set_obj_name(lp, "z");
>
> >             GLPK.glp_set_obj_dir(lp, GLPKConstants.GLP_MIN);
>
> >             GLPK.glp_set_obj_coef(lp, 0, 0);
>
> >             GLPK.glp_set_obj_coef(lp, 1, .5);
>
> >             GLPK.glp_set_obj_coef(lp, 2, 3);
>
> >             GLPK.glp_set_obj_coef(lp, 3, 2);
>
> >             GLPK.glp_set_obj_coef(lp, 4, 10);
>
> >
>
> >             // Solve model
>
> > //  solve model
>
> >             iocp = new glp_iocp();
>
> >             GLPK.glp_init_iocp(iocp);
>
> >             iocp.setPresolve(GLPKConstants.GLP_ON);
>
> > //  GLPK.glp_write_lp(lp, null, "yi.lp");
>
> >             ret = GLPK.glp_intopt(lp, iocp);
>
> >             // Retrieve solution
>
> >             if (ret == 0) {
>
> >                 write_mip_solution(lp);
>
> >             } else {
>
> >                 System.out.println("The problem could not be solved");
>
> >             }
>
> >             // Free memory
>
> >             GLPK.glp_delete_prob(lp);
>
> >         } catch (GlpkException ex) {
>
> >             ex.printStackTrace();
>
> >         }
>
> >     }
>
> >
>
> >     /**
>
> >      * write integer solution
>
> >      *
>
> >      * @param mip problem
>
> >      */
>
> >     static void write_mip_solution(glp_prob lp) {
>
> >         int i;
>
> >         int n;
>
> >         String name;
>
> >         double val;
>
> >
>
> >         name = GLPK.glp_get_obj_name(lp);
>
> >         val = GLPK.glp_mip_obj_val(lp);
>
> >         System.out.print(name);
>
> >         System.out.print(" = ");
>
> >         System.out.println(val);
>
> >         n = GLPK.glp_get_num_cols(lp);
>
> >         for (i = 1; i <= n; i++) {
>
> >             name = GLPK.glp_get_col_name(lp, i);
>
> >             val = GLPK.glp_mip_col_val(lp, i);
>
> >             System.out.print(name);
>
> >             System.out.print(" = ");
>
> >             System.out.println(val);
>
> >         }
>
> >     }
>
> > }
>
> >
>
> >
>
> >
>
> > -------- Original-Nachricht --------
>
> > > Datum: Tue, 15 Jan 2013 14:06:07 +0100
>
> > > Von: David Gabriel <address@hidden>
>
> > > An: Heinrich Schuchardt <address@hidden>
>
> > > Betreff: Re: [Help-glpk] java glpk
>
> >
>
> > > Thanks a lot for your help,
>
> > >
>
> > > I want that you check the following source code if it is ok or not ?
>
> > > It is related to the below Linear program:
>
> > >
>
> > >         /*
>
> > >          * Minimize z = 0.5 x1 + 3 x2 + 2 x3 + 10 x4
>
> > >          * Subject to
>
> > >          * x1, x2, x3, x4 >= 0
>
> > >          * x1, x2, x3, x4 are integers
>
> > >          * x1 + 0.7 x2 = 4 //c1
>
> > >          * x3 + 2 x2 >= 3 //c2
>
> > >          * x4 + 2x1  >= 4 //c3
>
> > >          */
>
> > >
>
> > >
>
> > > glp_prob lp;
>
> > >         glp_smcp parm;
>
> > >         SWIGTYPE_p_int ind;
>
> > >         SWIGTYPE_p_double val;
>
> > >         int ret;
>
> > >         try {
>
> > >
>
> > >         // Create problem
>
> > >         lp = GLPK.glp_create_prob();
>
> > >         System.out.println("Problem created");
>
> > >         GLPK.glp_set_prob_name(lp, "myProblem");
>
> > >
>
> > >         // Define columns
>
> > >
>
> > >         GLPK.glp_add_cols(lp, 4);
>
> > >
>
> > >         GLPK.glp_set_col_name(lp,1,"x1");
>
> > >         GLPK.glp_set_col_kind(lp,1,GLPKConstants.GLP_IV);
>
> > >         GLPK.glp_set_col_bnds(lp,1,GLPKConstants.GLP_LO, 0, 0);
>
> > >
>
> > >         GLPK.glp_set_col_name(lp,2,"x2");
>
> > >         GLPK.glp_set_col_kind(lp,1,GLPKConstants.GLP_IV);
>
> > >         GLPK.glp_set_col_bnds(lp,1,GLPKConstants.GLP_LO, 0, 0);
>
> > >
>
> > >         GLPK.glp_set_col_name(lp,3,"x3");
>
> > >         GLPK.glp_set_col_kind(lp,1,GLPKConstants.GLP_IV);
>
> > >         GLPK.glp_set_col_bnds(lp,1,GLPKConstants.GLP_LO, 0, 0);
>
> > >
>
> > >         GLPK.glp_set_col_name(lp,4,"x4");
>
> > >         GLPK.glp_set_col_kind(lp,1,GLPKConstants.GLP_IV);
>
> > >         GLPK.glp_set_col_bnds(lp,1,GLPKConstants.GLP_LO, 0, 0);
>
> > >
>
> > >         // Create constraints
>
> > >         GLPK.glp_add_rows(lp, 3);
>
> > >
>
> > >         //x1 + 0.7 x2 = 4 //c1
>
> > >         GLPK.glp_set_row_name(lp, 1, "c1");
>
> > >         GLPK.glp_set_row_bnds(lp, 1, GLPKConstants.GLP_FX, 4, 4);
>
> > >
>
> > >         ind = GLPK.new_intArray(6);
>
> > >         GLPK.intArray_setitem(ind, 1, 1);
>
> > >         GLPK.intArray_setitem(ind, 2, 2);
>
> > >         GLPK.intArray_setitem(ind, 3, 3);
>
> > >         GLPK.intArray_setitem(ind, 4, 4);
>
> > >
>
> > >         val = GLPK.new_doubleArray(6);
>
> > >         GLPK.doubleArray_setitem(val, 1, 1.0);
>
> > >         GLPK.doubleArray_setitem(val, 2, 0.7);
>
> > >         GLPK.doubleArray_setitem(val, 3, 0.0);
>
> > >         GLPK.doubleArray_setitem(val, 4, 0.0);
>
> > >         GLPK.glp_set_mat_row(lp, 1, 4, ind, val);
>
> > >
>
> > >
>
> > >         // x3 + 2 x2 >= 3 //c2
>
> > >         GLPK.glp_set_row_name(lp, 2, "c2");
>
> > >         GLPK.glp_set_row_bnds(lp, 2, GLPKConstants.GLP_LO, 3, 0);
>
> > >
>
> > >         ind = GLPK.new_intArray(4);
>
> > >         GLPK.intArray_setitem(ind, 1, 1);
>
> > >         GLPK.intArray_setitem(ind, 2, 2);
>
> > >         GLPK.intArray_setitem(ind, 3, 3);
>
> > >         GLPK.intArray_setitem(ind, 4, 4);
>
> > >
>
> > >         val = GLPK.new_doubleArray(4);
>
> > >         GLPK.doubleArray_setitem(val, 1, 0);//x1
>
> > >         GLPK.doubleArray_setitem(val, 2, 2);//x2
>
> > >         GLPK.doubleArray_setitem(val, 3, 1);//x3
>
> > >         GLPK.doubleArray_setitem(val, 4, 0);//x4
>
> > >         GLPK.glp_set_mat_row(lp, 2, 4, ind, val);
>
> > >
>
> > >         //x4 + 2x1 >= 4 //c3
>
> > >         GLPK.glp_set_row_name(lp, 3, "c3");
>
> > >         GLPK.glp_set_row_bnds(lp, 3, GLPKConstants.GLP_LO, 4, 0);
>
> > >
>
> > >         ind = GLPK.new_intArray(4);
>
> > >         GLPK.intArray_setitem(ind, 1, 1);
>
> > >         GLPK.intArray_setitem(ind, 2, 2);
>
> > >         GLPK.intArray_setitem(ind, 3, 3);
>
> > >         GLPK.intArray_setitem(ind, 4, 4);
>
> > >
>
> > >         val = GLPK.new_doubleArray(4);
>
> > >         GLPK.doubleArray_setitem(val, 1, 2);//x1
>
> > >         GLPK.doubleArray_setitem(val, 2, 0);//x2
>
> > >         GLPK.doubleArray_setitem(val, 3, 0);//x3
>
> > >         GLPK.doubleArray_setitem(val, 4, 1);//x4
>
> > >         GLPK.glp_set_mat_row(lp, 3, 4, ind, val);
>
> > >
>
> > >
>
> > >         // Define objective
>
> > >         //Minimize z = 0.5 x1 + 3 x2 + 2 x3 + 10 x4
>
> > >         GLPK.glp_set_obj_name(lp, "z");
>
> > >         GLPK.glp_set_obj_dir(lp, GLPKConstants.GLP_MIN);
>
> > >         GLPK.glp_set_obj_coef(lp, 0, 0);
>
> > >         GLPK.glp_set_obj_coef(lp, 1, .5);
>
> > >         GLPK.glp_set_obj_coef(lp, 2, 3);
>
> > >         GLPK.glp_set_obj_coef(lp, 3, 2);
>
> > >         GLPK.glp_set_obj_coef(lp, 4, 10);
>
> > >
>
> > >         // Solve model
>
> > >         parm = new glp_smcp();
>
> > >         GLPK.glp_init_smcp(parm);
>
> > >         ret = GLPK.glp_simplex(lp, parm);
>
> > >         // Retrieve solution
>
> > >         if (ret == 0) {
>
> > >         write_lp_solution(lp);
>
> > >         } else {
>
> > >
>
> > >         System.out.println("The problem could not be solved");
>
> > >         }
>
> > >         // Free memory
>
> > >         GLPK.glp_delete_prob(lp);
>
> > >         } catch (GlpkException ex) {
>
> > >         ex.printStackTrace();
>
> > >         }
>
> > >
>
> > > Thanks in advance.
>
> > >
>
> > >
>
> > > 2013/1/7 Heinrich Schuchardt <address@hidden>
>
> > >
>
> > > > Hello David,
>
> > > >
>
> > > > GLPK for Java is  a JNI wrapper library generated with SWIG around the
>
> > > > GLPK C
>
> > > > library.
>
> > > >
>
> > > > Your first point of reference should be file
>
> > > > glpk-4.47/doc/glpk.pdf
>
> > > > of the GLPK source distribution available at
>
> > > > ftp://ftp.gnu.org/gnu/glpk/glpk-4.47.tar.gz
>
> > > >
>
> > > > In chapter 2.2.12 you will find the description of function
>
> > > > void glp_set_mat_row(glp_prob *lp, int i, int len,
>
> > > > const int ind[], const double val[]);
>
> > > >
>
> > > > Some information is available in the javadoc that is generated
>
> > > > when building GLPK for Java and additionally available online at
>
> > > > http://glpk-java.sourceforge.net/apidocs/index.html
>
> > > >
>
> > > > Please, also have a look at the examples in directory
>
> > > > glpk-java-1.0.22/examples/java
>
> > > > of the GLPK for Java source which you can download from
>
> > > > http://glpk-java.sourceforge.net
>
> > > >
> 
> > > > A further point of reference is the GLPK wikibook at
>
> > > > http://en.wikibooks.org/wiki/GLPK
>
> > > >
>
> > > > Please, do not hesitate to contact me if further questions remain.
>
> > > >
>
> > > > If you are looking for a less complex API to call GLPK you may want
>
> > > > to have a look at my project at
>
> > > > http://www.xypron.de/projects/linopt/
>
> > > >
>
> > > > Best regards
>
> > > >
>
> > > > Heinrich Schuchardt
>
> > > >
>
> > > > -------- Original-Nachricht --------
>
> > > > > Datum: Mon, 7 Jan 2013 18:42:40 +0100
>
> > > > > Betreff: [Help-glpk] java glpk
>
> > > >
>
> > > > > Hi,
>
> > > > >
>
> > > > > It is the first I am using GLPK, and I should integrate it to my Java
>
> > > > > project.
>
> > > > > I succeed to install it in linux environment and I start reading some
>
> > > > code
>
> > > > > source examples (only 2 examples availbale in the official
>
> > > > documentation).
>
> > > > > I have some questions about setting constraints (columns).
>
> > > > > In fact, I want to know why we should use integer array and double
>
> > > array
>
> > > > ?
>
> > > > > What is their lenght (is it constraint number) ?
>
> > > > > Shall I use an array for each constraint (column) ?
>
> > > > > Which values should be used to set the table ?
>
> > > > > What is the importance of this statment ? :
>
> > > > >         GLPK.glp_set_mat_row(lp, 1, 2, ind, val);
>
> > > > > What are the 2nd and the 3rd parameters of this function ?
>
> > > > >
>
> > > > > I am looking for your help !
>
> > > > > Thanks in advance.
>
> > > > > Regards,
>
> > > > >
>
> > > > > ///The example I am speaking about : It is available at the official
>
> > > > > documentation of java-glpk
>
> > > > >
>
> > > > > // Minimize z = (x1-x2) /2 + (1-(x1-x2)) = -.5 * x1 + .5 * x2 + 1
>
> > > > >         // subject to
>
> > > > >         // 0.0<= x1 - x2 <= 0.2
>
> > > > >         // where,
>
> > > > >         // 0.0 <= x1 <= 0.5
>
> > > > >
>
> > > > >
>
> > > > >         glp_prob lp;
>
> > > > >         glp_smcp parm;
>
> > > > >         SWIGTYPE_p_int ind;
>
> > > > >         SWIGTYPE_p_double val;
>
> > > > >         int ret;
>
> > > > >         try {
>
> > > > >
>
> > > > >         // Create problem
>
> > > > >         lp = GLPK.glp_create_prob();
>
> > > > >         System.out.println("Problem created");
>
> > > > >         GLPK.glp_set_prob_name(lp, "myProblem");
>
> > > > >
>
> > > > >         // Define columns
>
> > > > >         GLPK.glp_add_cols(lp, 2);
>
> > > > >         GLPK.glp_set_col_name(lp,1,"x1");
>
> > > > >         GLPK.glp_set_col_kind(lp,1,GLPKConstants.GLP_CV);
>
> > > > >         GLPK.glp_set_col_bnds(lp,1,GLPKConstants.GLP_DB, 0, .5);
>
> > > > >         GLPK.glp_set_col_name(lp,2,"x2");
>
> > > > >         GLPK.glp_set_col_kind(lp,2,GLPKConstants.GLP_CV);
>
> > > > >         GLPK.glp_set_col_bnds(lp,2,GLPKConstants.GLP_DB, 0, .5);
>
> > > > >         // Create constraints
>
> > > > >         GLPK.glp_add_rows(lp, 1);
> 
> > > > >         GLPK.glp_set_row_name(lp, 1, "c1");
>
> > > > >         GLPK.glp_set_row_bnds(lp, 1, GLPKConstants.GLP_DB, 0, 0.2);
>
> > > > >         ind = GLPK.new_intArray(3);
>
> > > > >         GLPK.intArray_setitem(ind, 1, 1);
>
> > > > >         GLPK.intArray_setitem(ind, 2, 2);
>
> > > > >         val = GLPK.new_doubleArray(3);
>
> > > > >         GLPK.doubleArray_setitem(val, 1, 1.);
>
> > > > >         GLPK.doubleArray_setitem(val, 2, -1.);
>
> > > > >         GLPK.glp_set_mat_row(lp, 1, 2, ind, val);
>
> > > > >
>
> > > > >         // Define objective
>
> > > > >         GLPK.glp_set_obj_name(lp, "z");
>
> > > > >         GLPK.glp_set_obj_dir(lp, GLPKConstants.GLP_MIN);
>
> > > > >         GLPK.glp_set_obj_coef(lp, 0, 1.);
>
> > > > >         GLPK.glp_set_obj_coef(lp, 1, -.5);
>
> > > > >         GLPK.glp_set_obj_coef(lp, 2, .5);
>
> > > > >         // Solve model
>
> > > > >         parm = new glp_smcp();
>
> > > > >         GLPK.glp_init_smcp(parm);
>
> > > > >         ret = GLPK.glp_simplex(lp, parm);
>
> > > > >         // Retrieve solution
>
> > > > >         if (ret == 0) {
>
> > > > >         write_lp_solution(lp);
>
> > > > >         } else {
>
> > > > >
>
> > > > >         System.out.println("The problem could not be solved");
>
> > > > >         }
>
> > > > >         // Free memory
>
> > > > >         GLPK.glp_delete_prob(lp);
>
> > > > >         } catch (GlpkException ex) {
>
> > > > >         ex.printStackTrace();
>
> > > > >         }
>
> > > > >         }
>
> > > > >         /**
>
> > > > >         * write simplex solution
>
> > > > >         * @param lp problem
>
> > > > >         */
>
> > > > >         static void write_lp_solution(glp_prob lp) {
>
> > > > >         int i;
>
> > > > >         int n;
>
> > > > >         String name;
>
> > > > >         double val;
>
> > > > >         name = GLPK.glp_get_obj_name(lp);
>
> > > > >         val = GLPK.glp_get_obj_val(lp);
>
> > > > >         System.out.print(name);
>
> > > > >         System.out.print(" = ");
>
> > > > >         System.out.println(val);
>
> > > > >         n = GLPK.glp_get_num_cols(lp);
>
> > > > >         for (i = 1; i <= n; i++) {
>
> > > > >         name = GLPK.glp_get_col_name(lp, i);
>
> > > > >         val = GLPK.glp_get_col_prim(lp, i);
>
> > > > >         System.out.print(name);
>
> > > > >         System.out.print(" = ");
>
> > > > >         System.out.println(val);
>
> > > > >         }
>
> > > > >   }
>
> > > >
>
> >




reply via email to

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