Python example

Heinrich Schuchardt <[email protected]>
Newsgroups gmane.comp.gnu.glpk
Message-ID <[email protected]>
Hello Andrew, hello Jason

I think it would be helpful for other users to add the appended Python
example to the GLPK source.

Best regards

Heinrich

_______________________________________________
Help-glpk mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/help-glpk
mip.py (text/x-python, 2.9 KB)
#!/usr/bin/python
""" Mixed integer problem

This program demonstrates how to call the GLPK library from Python
to solve a mixed integer problem.

The ctypes library is used to wrap the calls to GLPK.
"""

from ctypes import *
import platform

# Constants
GLP_MAX = 2
GLP_ON = 1
GLP_IV = 2
GLP_LO = 2
GLP_UP = 3

# Structure glp_iocp
class IOCP(Structure) :
	_fields_ = [
		('msg_lev', c_int),
		('br_tech', c_int),
		('bt_tech', c_int),
		('tol_int', c_double),
		('tol_obj', c_double),
		('tm_lim', c_int),
		('out_frq', c_int),
		('out_dly', c_int),
		('cb_func', c_void_p),
		('cb_info', c_void_p),
		('cb_size', c_int),
		('pp_tech', c_int),
		('mip_gap', c_double),
		('mir_cuts', c_int),
		('gmi_cuts', c_int),
		('cov_cuts', c_int),
		('clq_cuts', c_int),
		('presolve', c_int),
		('fp_heur', c_int),
		('ps_heur', c_int),
		('ps_tm_lim', c_int),
		('sr_heur', c_int),
		('use_sol', c_int),
		('save_sol', c_char_p),
		('alien', c_int),
		('flip', c_int),
		('foo_bar', c_double * 23)]

if platform.system() == "Linux":
	solver = cdll.LoadLibrary('libglpk.so')
else :
	solver = windll.LoadLibrary('glpk_4_64.dll')

# Adjust function prototypes
solver.glp_create_prob.restype = c_void_p
solver.glp_init_iocp.argtypes = [ POINTER(IOCP) ]
solver.glp_intopt.argtypes = [ c_void_p, POINTER(IOCP) ]
solver.glp_mip_obj_val.restype = c_double
solver.glp_mip_col_val.restype = c_double
solver.glp_set_obj_coef.argtypes = [ c_void_p, c_int, c_double]
solver.glp_set_col_bnds.argtypes = [ c_void_p, c_int, c_int, c_double, c_double ]
solver.glp_set_row_bnds.argtypes = [ c_void_p, c_int, c_int, c_double, c_double ]

# Create problem
lp = c_void_p(solver.glp_create_prob())
solver.glp_set_prob_name(lp, 'Problem')

# Define rows and columns
solver.glp_add_rows(lp, 2)
solver.glp_set_row_name(lp, 1, 'p')
solver.glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 10.5)
solver.glp_set_row_name(lp, 2, 'q')
solver.glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 19.7)
solver.glp_add_cols(lp, 2)
solver.glp_set_col_name(lp, 1, 'x1')
solver.glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0)
solver.glp_set_col_name(lp, 2, 'x2')
solver.glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0)
solver.glp_set_col_kind(lp, 2, GLP_IV)

# Define objective
solver.glp_set_obj_dir(lp, GLP_MAX)
solver.glp_set_obj_name(lp, 'obj')
solver.glp_set_obj_coef(lp, 1, 0.61)
solver.glp_set_obj_coef(lp, 2, 0.49)

# Define matrix
ia = (c_int * 5)()
ja = (c_int * 5)()
ar = (c_double * 5)()
ia[1]=1; ia[2]=1; ia[3]=2; ia[4]=2
ja[1]=1; ja[2]=2; ja[3]=1; ja[4]=2
ar[1]=1.0; ar[2]=2.0; ar[3]=3.0; ar[4]=1.0
solver.glp_load_matrix(lp, 4, ia, ja, ar);

# Solve
iocp = IOCP()
solver.glp_init_iocp(iocp)
iocp.presolve = GLP_ON
ret = solver.glp_intopt(lp, iocp)
if ret:
	print('Failure\n')
	solver.glp_delete_prob(lp)
	solver.glp_free_env()
	quit()

# Get results
z = solver.glp_mip_obj_val(lp)
x1 = solver.glp_mip_col_val(lp, c_int(1))
x2 = solver.glp_mip_col_val(lp, c_int(2))
print('obj = %g; x1 = %g; x2 = %g\n'%(z, x1, x2))

# Cleanup
solver.glp_delete_prob(lp)
solver.glp_free_env()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.