help-glpk
[Top][All Lists]
Advanced

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

[Help-glpk] Embedded Python in GLPK


From: Xypron
Subject: [Help-glpk] Embedded Python in GLPK
Date: Sun, 09 Mar 2008 00:16:07 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; de-AT; rv:1.8.1.12) Gecko/20080201 SeaMonkey/1.1.8

Using GLPK I missed the possibility to use a scripting language to calculate values used for filling parameters and sets.

Example:
I was working on a problem combining lot sizing and safety stock calculation. I needed to calculate the inverse incomplete Gamma distribution function depending on service level and time until next production. I has to use quadratic approximations for the required safety stock in GLPK. In ILOG OPL I was able to use javascript to make an exact calculation.

To overcome this problem I have added embedded Python scripting into GLPK 4.27. My first tentative implementation can be downloaded from
ftp://glpk.dyndns.org/glpk/glpk-4.27-py_r161.tar.gz

Installation of Python 2.5 developer files is presumed.

Compiling has only be tested under Linux and MinGW. I have not updated the make files in the W32 directory.

Python scripts can be enclosed in a MPL file with
<?python
?>

functions are declared with
function name ['alias'] python {numeric, symbolic, set} [variant] [dimen n] ([{numeric, symbolic, set} name,] ...);
[] being optional arguments.
variant describes a function where the result depends not only on the arguments. Hence the result may not be buffered for future use. An example are random number generators.

Sets are passed to python and returned form python as lists of tuples.

Where do I want to go to: I would like to use embedding Python scripting to control the solution process: adding new constraints, changing variables from float to binary, ... But that is future. Currently Python can only used to calculate numbers, symbols and sets.

I am looking forward for Your comments.

Best regards

Xypron


Example

py1.mod
=======

<?python
# Definition of Python functions
from testmod import *
?>
# Definition of MPL functions
function createnumber python numeric  variant         ();
function createstring python symbolic variant         ();
function createlist   python set              dimen 3 ();

function countset     python numeric                  (set S);
function symbollen    python numeric                  (symbolic s);
function times2       python numeric                  (numeric n);

set s dimen 3;

param a          := createnumber();
param b symbolic := createstring();
set   c dimen 3  := createlist();

param d          := countset(s);
param e          := symbollen('Hello world');
param f          := times2( 3.4 );

display a,b,c;
display d,e,f;
solve;
data;

set s :=
 1 3 2
 9 4 4
 5 6 5;

end;

testmod.py
==========
"""Test modules for GLPK

Functions to return values of different types
 createnumber()        - returns a random number
 createstring()        - returns a random string
 createlist()          - returns a list of tuples of dimension 3

Function to consume values of different types
 countset(set S)       - returns number of elements in set
 symbollen(symbolic s) - returns length of symbol
 times2(numeric n)     - returns the double of the number

"""

def createnumber() :
  """createnumber()
  returns a random number
  """
  from random import random
  return random()

def createstring() :
  """createstring()
  returns a random string
  """
  from random import random
  return str(random())

def createlist() :
  """mylist()
  creates a list of tuples of dimension 3
  """
  dct = {}
  dct["Frankfurt"] = ("Main",  652610)
  dct["Hamburg"]   = ("Elbe",  1766156)
  dct["Bremen"]    = ("Weser", 548477)
  dct["Duisburg"]  = ("Rhein", 495668)
  lst=[]
  for i in dct :
     tup = (i, dct[i][0], dct[i][1])
     lst.append(tup)
  return lst

def countset(s) :
  print "countset(", s, ")"
  i = 0;
for t in s : i += len(t)
  return i;
def symbollen(s) :
  print "symbollen('", s, "')"
  return len(s)

def times2(n) :
  print "times2(", n, ")"
  return 2 * n

Output
======
Reading model section from py1.mod...
Reading data section from py1.mod...
35 lines were read
Executing python script
Execution of python script completed
Display statement at line 24
a = 0.335536625609238
b = '0.713453044557'
c:
  (Duisburg,Rhein,495668)
  (Frankfurt,Main,652610)
  (Bremen,Weser,548477)
  (Hamburg,Elbe,1766156)
Display statement at line 25
countset( [(1.0, 3.0, 2.0), (9.0, 4.0, 4.0), (5.0, 6.0, 5.0)] )
d = 9
symbollen(' Hello world ')
e = 11
times2( 3.4 )
f = 6.8
Model has been successfully generated
~     0:   objval =   0.000000000e+00   infeas =   0.000000000e+00
OPTIMAL SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.1 Mb (71464 bytes)
Model has been successfully processed





reply via email to

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