gams2numpy01.gms : Test gams2numpy Python API string mode

Description

Test gams2numpy Python API string mode using the GAMS Embedded Code facility

Contributor: Clemens Westphal, July 2020


Small Model of Type : GAMS


Category : GAMS Test library


Main file : gams2numpy01.gms

$title 'Test gams2numpy API string mode' (GAMS2NUMPY01,SEQ=826)

$ontext
Test gams2numpy Python API string mode using the GAMS Embedded Code facility

Contributor: Clemens Westphal, July 2020
$offtext


* On the major platforms (Windows, Linux, Mac), GMSPYTHONLIB gets automatically set 
* to use the internal Python installation in sysdir/GMSPython.
$if not setEnv GMSPYTHONLIB $abort.noError Embedded code Python not ready to be used
$log --- Using Python library %sysEnv.GMSPYTHONLIB%

set i /i1*i10/;
set j /j1*j10/;
set k /1*10/;
set l /1*10/;

parameter p(i,j), p2(k,l);
p(i,j) = uniform(0,10);
p2(k,l) = uniform(0,10);
p2('1','1') = +inf;

execute_unload 'p_reference.gdx', p;
execute_unload 'ijp_reference.gdx', i j p;
execute_unload 'klp2_reference.gdx', k l p2;

*Test string mode
embeddedCode Python:
    import numpy as np
    import gams2numpy
    from gdxcc import *
    from gmdcc import *

    g2np = gams2numpy.Gams2Numpy(r"%gams.sysdir%.")
    gdx = new_gdxHandle_tp()
    gmd1 = new_gmdHandle_tp()
    gmd2 = new_gmdHandle_tp()
    
    rc, msg = gmdCreate(gmd1, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    
    rc, msg = gmdCreate(gmd2, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    
    rc, msg = gdxCreate(gdx, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    
    if not gdxOpenRead(gdx, 'ijp_reference.gdx')[0]:
        raise Exception("Error opening GDX file 'ijp_reference.gdx'")
    i_arr = g2np.gdxReadSymbolStr(gdx, 'i')
    j_arr = g2np.gdxReadSymbolStr(gdx, 'j')
    p_arr = g2np.gdxReadSymbolStr(gdx, 'p')
    gdxClose(gdx)
    if not gdxFree(gdx):
        raise Exception("Error in gdxFree")
    
    rc, msg = gdxCreate(gdx, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    if not gdxOpenWrite(gdx, 'p_gdx1.gdx', '')[0]:
        raise Exception("Error creating GDX file 'p_gdx1.gdx'")
    gdxUELRegisterStrStart(gdx)
    for l in gams.get('*'):
        gdxUELRegisterStr(gdx, l)
    gdxUELRegisterDone(gdx)
    g2np.gdxWriteSymbolStr(gdx, 'p', '', 2, GMS_DT_PAR, 0, p_arr)
    if gdxClose(gdx):
        raise Exception("Error closing GDX file")
    if not gdxFree(gdx):
        raise Exception("Error in gdxFree")

    rc, msg = gdxCreate(gdx, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    if not gdxOpenWrite(gdx, 'ijp_gdx.gdx', '')[0]:
        raise Exception("Error creating GDX file 'ijp_gdx.gdx'")
    gdxUELRegisterStrStart(gdx)
    for l in gams.get('*'):
        gdxUELRegisterStr(gdx, l)
    gdxUELRegisterDone(gdx)
    g2np.gdxWriteSymbolStr(gdx, 'i', '', 1, GMS_DT_SET, 0, i_arr)
    g2np.gdxWriteSymbolStr(gdx, 'j', '', 1, GMS_DT_SET, 0, j_arr)
    g2np.gdxWriteSymbolStr(gdx, 'p', '', 2, GMS_DT_PAR, 0, p_arr, ['i', 'j'])
    if gdxClose(gdx):
        raise Exception("Error closing GDX file")
    if not gdxFree(gdx):
        raise Exception("Error in gdxFree")

    rc = new_intp()
    for l in gams.get('*'):
        gmdMergeUel(gmd1, l)
    p_symPtr = gmdAddSymbolPy(gmd1, 'p', 2, GMS_DT_PAR, 0, '', rc)
    if not intp_value(rc):
        raise Exception("Error adding symbol to GMD")

    p_arr2 = p_arr.copy()
    p_arr2[:,-1] = 3.14 # add wrong values
    g2np.gmdFillSymbolStr(gmd1, p_symPtr, p_arr2)

    # add a duplicate record to the array and write correct values using merge=True
    p_arr = np.insert(p_arr, 0, np.array(['i1', 'j1', 1.23], dtype=object), axis=0)
    g2np.gmdFillSymbolStr(gmd1, p_symPtr, p_arr, merge=True)
    gmdWriteGDX(gmd1, 'p_gmd1.gdx', False)
    rc = new_intp()
    for l in gams.get('*'):
        gmdMergeUel(gmd2, l)
    p_symPtr = gmdAddSymbolPy(gmd2, 'p', 2, GMS_DT_PAR, 0, '', rc)
    if not intp_value(rc):
        raise Exception("Error adding symbol to GMD")
    g2np.gmdFillSymbolStr(gmd2, p_symPtr, g2np.gmdReadSymbolStr(gmd1, 'p'))
    gmdWriteGDX(gmd2, 'p_gmd2.gdx', False)
    
    if not gmdFree(gmd1):
        raise Exception("Error in gmdFree")
    if not gmdFree(gmd2):
        raise Exception("Error in gmdFree")

    # Test parameter relaxedType=True (GMD)
    gmd3 = new_gmdHandle_tp()
    rc, msg = gmdCreate(gmd3, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    rc = new_intp()
    for l in gams.get('*'):
        gmdMergeUel(gmd3, l)
    k_symPtr = gmdAddSymbolPy(gmd3, 'k', 1, GMS_DT_SET, 0, '', rc)
    if not intp_value(rc):
        raise Exception("Error adding symbol to GMD")
    k_arr = np.array( [[i+1] for i in range(10)] )
    g2np.gmdFillSymbolStr(gmd3, k_symPtr, k_arr, relaxedType=True)

    l_symPtr = gmdAddSymbolPy(gmd3, 'l', 1, GMS_DT_SET, 0, '', rc)
    if not intp_value(rc):
        raise Exception("Error adding symbol to GMD")
    g2np.gmdFillSymbolStr(gmd3, l_symPtr, k_arr, relaxedType=True)

    p2_symPtr = gmdAddSymbolPy(gmd3, 'p2', 2, GMS_DT_PAR, 0, '', rc)
    if not intp_value(rc):
        raise Exception("Error adding symbol to GMD")
    p2_arr = np.array(gams.get("p2", keyFormat=KeyFormat.FLAT), dtype=object)
    p2_arr[:, :-1] = p2_arr[:, :-1].astype(int)
    p2_arr[0, 2] = 'inf'
    g2np.gmdFillSymbolStr(gmd3, p2_symPtr, p2_arr, relaxedType=True)
    gmdWriteGDX(gmd3, 'klp2_gmd.gdx', False)
    if not gmdFree(gmd3):
        raise Exception("Error in gmdFree")

    # Test parameter relaxedType=True (GDX)
    rc, msg = gdxCreate(gdx, GMS_SSSIZE)
    if not rc:
        raise Exception(msg)
    if not gdxOpenWrite(gdx, 'klp2_gdx.gdx', '')[0]:
        raise Exception("Error creating GDX file 'klp2_gdx.gdx'")
    gdxUELRegisterStrStart(gdx)
    for l in gams.get('*'):
        gdxUELRegisterStr(gdx, l)
    gdxUELRegisterDone(gdx)
    g2np.gdxWriteSymbolStr(gdx, 'k', '', 1, GMS_DT_SET, 0, k_arr, relaxedType=True)
    g2np.gdxWriteSymbolStr(gdx, 'l', '', 1, GMS_DT_SET, 0, k_arr, relaxedType=True)
    g2np.gdxWriteSymbolStr(gdx, 'p2', '',2, GMS_DT_PAR, 0, p2_arr, relaxedType=True)
    if gdxClose(gdx):
        raise Exception("Error closing GDX file")
    if not gdxFree(gdx):
        raise Exception("Error in gdxFree")
endEmbeddedCode

execute 'gdxdiff p_reference.gdx p_gdx1.gdx > %system.nullfile%';
abort$errorlevel 'p_reference.gdx and p_gdx1.gdx are different';

execute 'gdxdiff ijp_reference.gdx ijp_gdx.gdx > %system.nullfile%';
abort$errorlevel 'ijp_reference.gdx and ijp_gdx.gdx are different';

execute 'gdxdiff p_reference.gdx p_gmd1.gdx > %system.nullfile%';
abort$errorlevel 'p_reference.gdx and p_gmd1.gdx are different';

execute 'gdxdiff p_reference.gdx p_gmd2.gdx > %system.nullfile%';
abort$errorlevel 'p_reference.gdx and p_gmd2.gdx are different';

execute 'gdxdiff klp2_reference.gdx klp2_gmd.gdx > %system.nullfile%';
abort$errorlevel 'klp2_reference.gdx and klp2_gmd.gdx are different';

execute 'gdxdiff klp2_reference.gdx klp2_gdx.gdx > %system.nullfile%';
abort$errorlevel 'klp2_reference.gdx and klp2_gdx.gdx are different';