[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-glpk] [Fwd: GLPK for Java]
From: |
Andrew Makhorin |
Subject: |
[Help-glpk] [Fwd: GLPK for Java] |
Date: |
Sat, 16 Sep 2017 16:01:13 +0300 |
-------- Forwarded Message --------
From: address@hidden
To: address@hidden <address@hidden>
Subject: GLPK for Java
Date: Sat, 16 Sep 2017 14:03:45 +0200
Dear All --
We have already run several GMPL examples with WinGLPK but need to solve
linear programs in our Java application. I imported glpk-4.63\examples
\java into an Eclipse Project so I am able to run the examples now. In
particular we need to do call GLPK as shown in Lp.java which I wanted to
modify such that it first solves another simple example whose GMPL code
is as follows:
minimize z = -3 * x1 - x2
subject to
2 * x2 <= 6
2 * x1 + 3 * x2 <= 13
2 <= x1 <= 5
where,
0.0 <= x1
0.0 <= x2
Unfortunately my modification of Lp.java results in the error
glp_set_mat_row: i = 1; ind[2] = 0; column index out of range
Error detected in file ..\src\api\prob1.c at line 773
org.gnu.glpk.GlpkException: function glp_set_mat_row failed
at org.gnu.glpk.GLPKJNI.glp_set_mat_row(Native Method)
at org.gnu.glpk.GLPK.glp_set_mat_row(GLPK.java:382)
but although I have found the Javadoc I seems difficult to me to explain
the reason for my error.
My questions are
1. Can anybody explain this error?
2. Is is possible to create the GPLK for Java problem instances by
loading GMPL source code from a file?
3. Can we write a problem correctly created with GPLK for Java to a
GMPL source code file on disk?
The answer to Q3 is the line
GLPK.glp_write_lp(lp, null, "mincost.lp");
which I found in the example MinimumCostFlow.java so
glp_read_lp
public static int glp_read_lp(glp_prob P,
glp_cpxcp parm,
String fname)
should be the answer to Q2. If the latter will help me load the above
GMPL Problem into GLPK and GPLK for Java could generate a Java source
file creating an instance of the Problem that has been loaded into an
GLPK object then my current Problem would be solved.
* Is somebody able to devise a general method for generating the
Java source creating a GLPK instance which represents a problem
described by GMPL code above?
This is my current Code:
import org.gnu.glpk.GLPK;
import org.gnu.glpk.GLPKConstants;
import org.gnu.glpk.GlpkException;
import org.gnu.glpk.SWIGTYPE_p_double;
import org.gnu.glpk.SWIGTYPE_p_int;
import org.gnu.glpk.glp_prob;
import org.gnu.glpk.glp_smcp;
public class Produktionsplanung {
// Minimize z = -3 * x1 - x2
//
// subject to
// 2 * x2 <= 6
// 2 * x1 + 3 * x2 <= 13
// x1 <= 5
// where,
// 0.0 <= x1
// 0.0 <= x2
public static void main(String[] arg) {
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 one column per variable (2)
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, 100);
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, 100);
// Create constraints
// Allocate memory
ind = GLPK.new_intArray(3);
val = GLPK.new_doubleArray(2);
// Create one row per constraint (3)
GLPK.glp_add_rows(lp, 3);
// Set row details
// subject to 2 * x2 <= 6
GLPK.glp_set_row_name(lp, 1, "c1");
GLPK.glp_set_row_bnds(lp, 1, GLPKConstants.GLP_DB, 0, 6);
GLPK.intArray_setitem(ind, 1, 2);
GLPK.doubleArray_setitem(val, 1, 2);
GLPK.glp_set_mat_row(lp, 1, 2, ind, val);
// subject to 2 * x1 + 3 * x2 <= 13
GLPK.glp_set_row_name(lp, 2, "c2");
GLPK.glp_set_row_bnds(lp, 2, GLPKConstants.GLP_UP, 0, 13);
GLPK.intArray_setitem(ind, 1, 1);
GLPK.intArray_setitem(ind, 2, 2);
GLPK.doubleArray_setitem(val, 1, 2);
GLPK.doubleArray_setitem(val, 2, 3);
GLPK.glp_set_mat_row(lp, 2, 2, ind, val);
// subject to x1 <= 5
GLPK.glp_set_row_name(lp, 3, "c3");
GLPK.glp_set_row_bnds(lp, 3, GLPKConstants.GLP_UP, 0, 5);
GLPK.intArray_setitem(ind, 1, 1);
GLPK.doubleArray_setitem(val, 1, 1);
GLPK.glp_set_mat_row(lp, 2, 2, ind, val);
// Free memory
GLPK.delete_intArray(ind);
GLPK.delete_doubleArray(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);
GLPK.glp_set_obj_coef(lp, 3, -1);
// Write model to file
// GLPK.glp_write_lp(lp, null, "lp.lp");
// 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();
ret = 1;
}
System.exit(ret);
}
/**
* 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);
}
}
}
Gesendet von Mail für Windows 10
- [Help-glpk] [Fwd: GLPK for Java],
Andrew Makhorin <=