5
$\begingroup$

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, lets 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 genralize 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 generate 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

$\endgroup$
7
$\begingroup$

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
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.