5

I want to add robustness to my AMPL code, here is what I am actually doing: In my script I use a .txt file that contains all my parameters, let's call it "instance.txt". I want to print my result in another txt file and call it "output_instance.txt". This is not an issue for me.

However, I may try the code with several instances and thus use many files "instance0.txt", "instance1.txt", "instance2.txt" etc.

I would like to generalize the above by writing :

param filename := "instance.txt"
print blablabla > out ("output" & filename)

The purpose of doing this is to have a specific file name for each output.

The first line generates this error (I don't know if the second is correct) :

error processing param filename:
    can't convert 'instance.txt' to a number.

I read the chapter about strings in the ampl book but I didn't find what I was looking for.

Please tell me if you have a way of doing this with .txt files or with .dat files

odd
  • 113
  • 3

1 Answers1

7

AMPL assumes that a parameter takes only numerical values, unless you add the keyword symbolic to say that string values are also permitted. So your first statement should be

param filename symbolic := "instance.txt";

Then you can use the string expression ("output" & filename) to specify a file that receives the output from a command. For example, if you execute

print {i in 1..5} i^2 > ("output" & filename);

then you will write this line to the file outputinstance.txt:

1 4 9 16 25
4er
  • 628
  • 3
  • 6