2017 - A Quick Start For The Python Interface To LINGO API
2017 - A Quick Start For The Python Interface To LINGO API
LINGO API
1 Introduction
The package pyLingo is a Python interface to LINGO API functions, which gives you
the ability to run a LINGO command script to access all the major features of LINGO.
2 Installation
The package is located at the folder ”\Lingo18\Programming Samples”. To install the
package, it requires the installation of LINGO 18 as well. See file INSTALL for details
of the installation and platform specifications.
3 Usage
In the LINGO interface for Python, the names of functions use the convention: ’py’ +
name of LINGO API function, e.g. pyLScreateEnvLng in the Python interface corre-
sponds to LScreateEnvLng in LINGO API.
1
4 General commands
To load the package, use sentence:
from pyLindo import *
Model: samsizr
MODEL:
! Acceptance Sampling Design;
! From a large lot, take a sample of size N, accept if C or less are defective;
! Poisson approximation to number defective is used;
DATA:
AQL = @POINTER( 1); ! ”Good” lot fraction defective;
LTFD = @POINTER( 2); ! ”Bad” lot fraction defective;
PRDRISK = @POINTER( 3); ! Tolerance for rejecting good lot;
CONRISK = @POINTER( 4); ! Tolerance for accepting bad lot;
MINSMP = @POINTER( 5); ! Lower and upper bounds on sample size;
MAXSMP = @POINTER( 6);
ENDDATA
[OBJ] MIN = N;
! Tolerance for rejecting a good lot;
1 - @PPOISCDF( N * AQL, C) <= PRDRISK;
! Tolerance for accepting a bad lot;
@PPOISCDF( N * LTFD, C) <= CONRISK;
! Give solver some help in getting into range;
@BND( MINSMP, N, MAXSMP);
@BND( 1, C, MAXSMP);
2
! Make variables general integer;
@GIN( N); @GIN( C);
DATA:
@POINTER( 7) = N;
@POINTER( 8) = C;
@POINTER( 9) = @STATUS();
ENDDATA
END
Then using the Python interface to LINGO API, we can solve the above integer, non-
linear optimization model.
def samsizr(AQL,LTFD,PRDRISK,CONRISK,MINSMP,MAXSMP):
#create Lingo enviroment object
pEnv = lingo.pyLScreateEnvLng()
if pEnv is None:
print(”cannot create LINGO environment!”)
exit(1)
#@POINTER(1)
AQL 1 = N.array([AQL],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, AQL 1, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
print(”errorcode = ”, errorcode)
exit(1)
#@POINTER(2)
LTFD 1 = N.array([LTFD],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, LTFD 1, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
3
print(”errorcode = ”, errorcode)
exit(1)
#@POINTER(3)
PRDRISK 1 = N.array([PRDRISK],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, PRDRISK 1, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
print(”errorcode = ”, errorcode)
exit(1)
#@POINTER(4)
CONRISK 1 = N.array([CONRISK],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, CONRISK 1, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
print(”errorcode = ”, errorcode)
exit(1)
#@POINTER(5)
MINSMP 1 = N.array([MINSMP],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, MINSMP 1, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
print(”errorcode = ”, errorcode)
exit(1)
#@POINTER(6)
MAXSMP 1 = N.array([MAXSMP],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, MAXSMP 1, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
print(”errorcode = ”, errorcode)
exit(1)
#@POINTER(7)
NN = N.array([-1.0],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, NN, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
print(”errorcode = ”, errorcode)
exit(1)
#@POINTER(8)
C = N.array([-1.0],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, C, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
print(”errorcode = ”, errorcode)
exit(1)
4
#@POINTER(9)
Status = N.array([-1.0],dtype=N.double)
errorcode = lingo.pyLSsetDouPointerLng(pEnv, Status, pnPointersNow)
if errorcode != const.LSERR NO ERROR LNG:
print(”errorcode = ”, errorcode)
exit(1)
#check solution
print(”\nThe Optimal sample size is ”,NN,”.\nAccept the lot if ”,
C,” or less defectives in sample.\n\n”)
#################################################
if name == ’ main ’:
samsizr(0.03,0.08,0.09,0.05,125.0,400.0)