[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-glpk] java glpk
From: |
Heinrich Schuchardt |
Subject: |
Re: [Help-glpk] java glpk |
Date: |
Mon, 07 Jan 2013 21:58:20 +0100 |
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);
> }
> }