gams2numpy01.gms : Test gams.core.numpy Python API string mode

Description

Test gams.core.numpy 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 gams.core.numpy API string mode' (GAMS2NUMPY01,SEQ=826)

$onText
Test gams.core.numpy Python API string mode using the GAMS Embedded Code facility

Contributor: Clemens Westphal, July 2020
$offText


$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 gams.core.numpy as gnp
    from gams.core.gdx import *
    from gams.core.gmd import *

    gams2np = gnp.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_keys, i_values = gams2np.gdxReadSymbolStr(gdx, 'i')
    j_keys, j_values = gams2np.gdxReadSymbolStr(gdx, 'j')
    p_keys, p_values = gams2np.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)
    gams2np.gdxWriteSymbolStr(gdx, 'p', '', 2, GMS_DT_PAR, 0, p_keys, p_values)
    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)
    gams2np.gdxWriteSymbolStr(gdx, 'i', '', 1, GMS_DT_SET, 0, i_keys, i_values)
    gams2np.gdxWriteSymbolStr(gdx, 'j', '', 1, GMS_DT_SET, 0, j_keys, j_values)
    gams2np.gdxWriteSymbolStr(gdx, 'p', '', 2, GMS_DT_PAR, 0, p_keys, p_values, ['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_values2 = p_values.copy()
    p_values2[:,-1] = 3.14 # add wrong values
    gams2np.gmdFillSymbolStr(gmd1, p_symPtr, p_keys, p_values2)

    # add a duplicate record to the array and write correct values using merge=True
    p_keys = np.insert(p_keys, 0, np.array(['i1', 'j1']), axis=0)
    p_values = np.insert(p_values, 0, np.array([1.23]), axis=0)
    gams2np.gmdFillSymbolStr(gmd1, p_symPtr, p_keys, p_values, 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")
    gams2np.gmdFillSymbolStr(gmd2, p_symPtr, *gams2np.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_keys = np.array( [[i+1] for i in range(10)] )
    k_values = None
    gams2np.gmdFillSymbolStr(gmd3, k_symPtr, k_keys, k_values, 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")
    gams2np.gmdFillSymbolStr(gmd3, l_symPtr, k_keys, k_values, 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_keys = np.array(gams.get("p2", keyFormat=KeyFormat.FLAT, valueFormat=ValueFormat.SKIP), dtype=int)
    p2_values = np.array(gams.get("p2", keyFormat=KeyFormat.SKIP, valueFormat=ValueFormat.TUPLE), dtype=object)
    p2_values[0] = 'inf'
    gams2np.gmdFillSymbolStr(gmd3, p2_symPtr, p2_keys, p2_values, 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)
    gams2np.gdxWriteSymbolStr(gdx, 'k', '', 1, GMS_DT_SET, 0, k_keys, k_values, relaxedType=True)
    gams2np.gdxWriteSymbolStr(gdx, 'l', '', 1, GMS_DT_SET, 0, k_keys, k_values, relaxedType=True)
    gams2np.gdxWriteSymbolStr(gdx, 'p2', '',2, GMS_DT_PAR, 0, p2_keys, p2_values, 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';