How to save results to txt file using "display".

3,979 views
Skip to first unread message

DavyGriffin

unread,
Mar 25, 2014, 2:27:04 PM3/25/14
to am...@googlegroups.com
I tried to use "display > filename" to save my result to an txt file. The model could run, but I can not find the result file. Should I create a txt file with the same name first? Or should I add the extension after the filename? If you could show me a specific syntax example, that would be more helpful. Thank you very much.

dominik...@googlemail.com

unread,
Mar 25, 2014, 3:02:58 PM3/25/14
to am...@googlegroups.com
set N1 := 1..2;
set N2 := 1..2;
set N3 := 1..2;
param X {i1 in N1,i2 in N2,i3 in N3} ;
let  {i1 in N1,i2 in N2,i3 in N3} X[i1,i2,i3] := i1+i2+i3;
display X;
display X >filename.txt;

郭炜峰

unread,
Mar 25, 2014, 3:20:54 PM3/25/14
to am...@googlegroups.com
Thanks, I did the similar one. It can display in the Ampl window, but there was still no txt file. Or in another word, I can not find it. I'm not sure which part went wrong.


--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/cH9MHS4VtaQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

dominik...@googlemail.com

unread,
Mar 25, 2014, 3:28:48 PM3/25/14
to am...@googlegroups.com
if i run that code as i posted it, the file appears in the same folder as the file ampl.exe
e.g. C:\Program Files\ampl\

Robert Fourer

unread,
Mar 27, 2014, 10:51:44 PM3/27/14
to am...@googlegroups.com
To have the filename end in .txt, you should include the .txt extension in the display statement; for example:

display >abc.txt;

The resulting file will be written to your AMPL program folder/directory, unless you have used AMPL's "cd" command to change to a different folder. The file will be created if it does not already exist.

Bob Fourer
am...@googlegroups.com

=======

maziar kermani

unread,
Sep 9, 2016, 9:58:41 AM9/9/16
to AMPL Modeling Language, 4...@ampl.com
Hello,

I have a for loop in which I solve a problem "n" times and after each solve I would like to save the results to a file with a name output_n. However I would like to have the n be printed as 0001, 0002, ... 0100. I am trying with concatenating strings however I couldn't find any solution. I would appreciate if someone can help.

printf {j in 1.._nvars} "%s= %.3f\n", _varname[j], _var[j] > ("output_" & (printf "%.4i", runnumber) & ".out"); 

Thank you.

maziar kermani

unread,
Sep 9, 2016, 10:01:14 AM9/9/16
to AMPL Modeling Language, 4...@ampl.com
I have found the solution: using sprintf:
printf {j in 1.._nvars} "%s= %.3f\n", _varname[j], _var[j] > ("output_" & sprintf("%.4i", runnumber) & ".out"); 

Victor Zverovich

unread,
Sep 9, 2016, 4:21:19 PM9/9/16
to am...@googlegroups.com
You should use sprintf instead of printf in

  sprintf "%.4i", runnumber

because the latter prints to a file rather than returns a formatted string.

HTH,
Victor

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.

To post to this group, send email to am...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages