Search code examples
python-3.xpyomoampl

Write pyomo .DAT File


I have a .dat file for the parameters of my optimization:

 param: A:= 100;

 param: B:=
 1 0.5
 2 0.2
 3 0.3;

I am trying to reproduce the .dat file but with different values of A and B, I need to integrate it in a code so I wrote a script that writes strings to a .dat file but when I run in my model it produces an error. I believe it has something to do with the format, according to Pyomo the .dat file should be in AMPL format but there is little explanation on how to build such a file.

Currently doing this:

A = 100, B1 = '1 0.5', B2 = '2 0.2', B3 = '3 0.3'
file = open('ata.dat','w')
file.write('param: A:= '+str(A)+';\n')
file.write('param: B:=\n')
file.write(B1+'\n')
file.write(B2+'\n')
file.write(B3+';')
file.close()

Any help is appreciated!


Solution

  • The sample file gives an index for each element of B:

    1 0.5

    2 0.2

    3 0.3

    But it looks as if your code is only creating the values, not the indices:

    0.5

    0.2

    0.3

    AMPL .dat

    As discussed in this recent question, when you're specifying an indexed parameter in AMPL, you generally need to state the indices explicitly; there are a couple of different ways to do this, but you can't just let them be implied by position. For more on AMPL data formats, see chapter 9 of The AMPL Book.