Save iteration results txt

Hi support

I need your help. I want to save the iteration result in file txt but I don’t know how do it. I’m working in AMPL API Python. Would you help me.

1 Like

Hi @Oscar_Romero,

What do you mean by saving the iteration result? Do you want to save the current solution in a file?

You can for instance save the values for all variable in a csv file as follows:

ampl.get_data("_varname, _var").to_pandas().to_csv("solution.csv", index=False)

If you just want non-zeros, you can do that as follows:

ampl.get_data("{i in 1.._nvars: _var[i] != 0} (_varname[i], _var[i])").to_pandas().to_csv("nonzeros.csv", index=False)

Dear fdabrandao

I need only iteration like best bound, Gap, best integer, etc. In other words, all the information that appears on the console and that you can save in a file.

Hi @Oscar_Romero,

Would storing the solver output be enough? You can retrieve it with the latest version of amplpy as follows:

output = ampl.solve(return_output=True)
open("output.txt", "w").write(output)

In case you need specific information to be extracted you can either parse this output or use the options to returning solve information such as time and work which is available for some solvers such as Gurobi.