How to store output from "ampl.display(<>)" in a python variable

display feature in AMPL is very neat. Is it possible to store the value of the resolved expression in a python variable. For instance please see below command

x = ampl.display("sum {i in MONTHS, j in PRODUCTS} sell[i,j];")
shows below in python console
sum{i in MONTHS, j in PRODUCTS} sell[i,j] = 11588.6

can I store 11588.6 somehow in a python variable ? When the expression resolves, the type of x is None.

Thanks
Bhartendu

1 Like

Hi @Bhartendu_Awasthi,

You can use AMPL.get_value for that:

x = ampl.get_value("sum {i in MONTHS, j in PRODUCTS} sell[i,j]")

There is also AMPL.get_data for retrieving tabular expressions (e.g. solution = ampl.get_data("{p in PEOPLE, g in GIFTS: x[p, g] > 0} x[p, g]").to_pandas())

1 Like

Thank you very much @fdabrandao . Perhaps I need to look into AMPL python’s API documentation more.

Regards
Bhartendu

2 Likes

No problem. Please fell free to ask any questions here, it helps us to notice what we may not be highlighting well in the documentation.

1 Like