How to store the zero elements in a solution?

When exporting solutions to pandas df and excel out of GAMSPy the zeros are stripped out.

Similar to:
(How to store the zero elements in a solution? - #2 by Archiver)

is there a way to force write all zeros when reporting solution values?

Zeros are stripped out automatically for performance reasons. If you want to restore them, you can assign EPS to the unset elements of the solution. For example:

from gamspy import Container, Set, Parameter, SpecialValues

m = Container()

i = Set(m, "i", records=['i1', 'i2'])
a = Parameter(m, "a", domain=i, records=[('i1', 1)])
print(a.records)

a[i].where[~a[i]] = SpecialValues.EPS
print(a.records)

This would print:

    i  value
0  i1    1.0
    i  value
0  i1    1.0
1  i2   -0.0

As you can see from the output, EPS values are stored as -0.0 in the DataFrame. You can now export the DataFrame to a csv file with the zeros in it:

a.records.to_csv("out.csv")

If you want to export them to a gdx file, make sure to set the eps_to_zero flag to False:

m.write("out.gdx", eps_to_zero=False)

gdxdump on the generated gdx file should show:

Set i(*) /
'i1', 
'i2' /;

Parameter a(i) /
'i1' 1, 
'i2' Eps /;
1 Like