gams2numpy02.gms : Test gams2numpy Python API raw mode

Description

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

Contributor: Clemens Westphal, July 2020


Small Model of Type : GAMS


Category : GAMS Test library


Main file : gams2numpy02.gms

$title 'Test gams2numpy API raw mode' (GAMS2NUMPY02,SEQ=827)

$ontext
Test gams2numpy Python API raw 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 raw 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.gdxReadSymbolRaw(gdx, 'i')
    j_arr = g2np.gdxReadSymbolRaw(gdx, 'j')
    p_arr = g2np.gdxReadSymbolRaw(gdx, 'p')
    uels = list(gams.get('*'))
    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 uels:
        gdxUELRegisterStr(gdx, l)
    gdxUELRegisterDone(gdx)
    g2np.gdxWriteSymbolRaw(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 uels:
        gdxUELRegisterStr(gdx, l)
    gdxUELRegisterDone(gdx)
    g2np.gdxWriteSymbolRaw(gdx, 'i', '', 1, GMS_DT_SET, 0, i_arr)
    g2np.gdxWriteSymbolRaw(gdx, 'j', '', 1, GMS_DT_SET, 0, j_arr)
    g2np.gdxWriteSymbolRaw(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 uels:
        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")
    g2np.gmdFillSymbolRaw(gmd1, p_symPtr, p_arr)
    gmdWriteGDX(gmd1, 'p_gmd1.gdx', False)
    
    rc = new_intp()
    p_arr = g2np.gmdReadSymbolRaw(gmd1, 'p')
    uels = g2np.gmdGetUelList(gmd1)
    for l in uels[1:]:
        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.gmdFillSymbolRaw(gmd2, p_symPtr, p_arr)
    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()
    uels = list(gams.get('*'))
    for l in uels:
        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([[str(uel)] for uel in gams.get('k', keyType=KeyType.INT)])

    g2np.gmdFillSymbolRaw(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.gmdFillSymbolRaw(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, keyType=KeyType.INT), dtype=object)
    p2_arr[:, :-1] = p2_arr[:, :-1].astype(str)
    p2_arr[0, 2] = 'inf'
    g2np.gmdFillSymbolRaw(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 uels:
        gdxUELRegisterStr(gdx, l)
    gdxUELRegisterDone(gdx)
    g2np.gdxWriteSymbolRaw(gdx, 'k', '', 1, GMS_DT_SET, 0, k_arr, relaxedType=True)
    g2np.gdxWriteSymbolRaw(gdx, 'l', '', 1, GMS_DT_SET, 0, k_arr, relaxedType=True)
    g2np.gdxWriteSymbolRaw(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';