Skip to content

Instantly share code, notes, and snippets.

@huberflores
Last active March 31, 2022 20:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huberflores/2d422581dc6657badc21 to your computer and use it in GitHub Desktop.
Save huberflores/2d422581dc6657badc21 to your computer and use it in GitHub Desktop.
GLPK and GLPK for java - Installation tutorial and Eclipse configuration
#
# author Huber Flores
#
GLPK Tutorial
Requires Eclipse 3.7 (Indigo). At the moment, there is no official plugin for Juno
#Install dependecies
> sudo
> apt-get install build-essential glpk openjdk-7-jdk libtool swig (Synaptic Package Manager includes all the dependencies automatically)
#set java home
> export $JAVA_HOME=/usr/lib/jvm/jdk1.7.0_40/bin/java
#check
> echo $JAVA_HOME
/usr/lib/jvm/jdk1.7.0_40/bin/java
#GLPK installation
$ wget ftp://ftp.gnu.org/gnu/glpk/glpk-4.55.tar.gz
$ tar -xzf glpk-4.55.tar.gz
$ cd glpk-4.55
$./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig
$ glpsol --version
GLPSOL: GLPK LP/MIP Solver, v4.55
Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
2009, 2010, 2011, 2013, 2014 Andrew Makhorin, Department for Applied
Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
reserved. E-mail: <mao@gnu.org>.
This program has ABSOLUTELY NO WARRANTY.
This program is free software; you may re-distribute it under the terms
of the GNU General Public License version 3 or later.
#GLPK for Java
#This will create the jar files to include in the project and an interface with GLPK installed in the OS
> tar -xzf glpk-java-1.0.37.tar.gz
> cd glpk-java-1.0.37
>./configure
>./configure \
CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers \
SWIGFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
Assuming libglpk.so is in different location, then
$ ./configure LDFLAGS=-L/usr/share/lib/jni
$ make
$ make check
$ sudo make install
$ sudo ldconfig
#Jars generated: glpk-java-1.0.37.jar, glpk-java-1.0.37-sources.jar, glpk-java-1.0.37-javadocs.jar
#Include glpk-java-1.0.37.jar in the classpath of your project
#Also add in Ecliṕse a reference to the folder /swig/.libs as link for java.library.path (screenshot:https://raw.githubusercontent.com/huberflores/GenericCode/master/LinearProgramming/optimj-Djava.library.path-link.png)
#FAQ
1. Be careful with installing the right version of GLPK and GLPK for java. If there is a mismatch between versions, then it will be hard to install it.
Potential error if there is a version mismatch
make[2]: *** [all] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
#Example
1. Open Eclipse and create a new "Java Project"
2. Create a class "Example1"
3. Paste the code below (This example comes with the GLPK framework and is located in the folder examples)
package ee.cs.ut.example1;
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 Example1 {
// Minimize z = -.5 * x1 + .5 * x2 - x3 + 1
//
// subject to
// 0.0 <= x1 - .5 * x2 <= 0.2
// -x2 + x3 <= 0.4
// where,
// 0.0 <= x1 <= 0.5
// 0.0 <= x2 <= 0.5
// 0.0 <= x3 <= 0.5
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 columns
GLPK.glp_add_cols(lp, 3);
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);
GLPK.glp_set_col_name(lp, 3, "x3");
GLPK.glp_set_col_kind(lp, 3, GLPKConstants.GLP_CV);
GLPK.glp_set_col_bnds(lp, 3, GLPKConstants.GLP_DB, 0, .5);
// Create constraints
// Allocate memory
ind = GLPK.new_intArray(3);
val = GLPK.new_doubleArray(3);
// Create rows
GLPK.glp_add_rows(lp, 2);
// Set row details
GLPK.glp_set_row_name(lp, 1, "c1");
GLPK.glp_set_row_bnds(lp, 1, GLPKConstants.GLP_DB, 0, 0.2);
GLPK.intArray_setitem(ind, 1, 1);
GLPK.intArray_setitem(ind, 2, 2);
GLPK.doubleArray_setitem(val, 1, 1.);
GLPK.doubleArray_setitem(val, 2, -.5);
GLPK.glp_set_mat_row(lp, 1, 2, ind, val);
GLPK.glp_set_row_name(lp, 2, "c2");
GLPK.glp_set_row_bnds(lp, 2, GLPKConstants.GLP_UP, 0, 0.4);
GLPK.intArray_setitem(ind, 1, 2);
GLPK.intArray_setitem(ind, 2, 3);
GLPK.doubleArray_setitem(val, 1, -1.);
GLPK.doubleArray_setitem(val, 2, 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();
}
}
/**
* 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);
}
}
}
4. Right click in the project -> Run as java application
5. Output
Problem created
GLPK Simplex Optimizer, v4.55
2 rows, 3 columns, 4 non-zeros
* 0: obj = 1.000000000e+00 infeas = 0.000e+00 (0)
* 3: obj = 4.250000000e-01 infeas = 0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
z = 0.42500000000000004
x1 = 0.25
x2 = 0.09999999999999998
x3 = 0.5
6. Project can be fork from here https://github.com/huberflores/GenericCode/tree/master/LinearProgramming
References:
- https://en.wikibooks.org/wiki/GLPK/Java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment