编写 pyomo .DAT 文件
Write pyomo .DAT File
我有一个用于优化参数的 .dat 文件:
param: A:= 100;
param: B:=
1 0.5
2 0.2
3 0.3;
我正在尝试重现 .dat 文件,但 A 和 B 的值不同,我需要将它集成到代码中,所以我编写了一个脚本,将字符串写入 .dat 文件,但是当我 运行 在我的模型中它会产生错误。我相信这与格式有关,根据 Pyomo 的说法,.dat 文件应该是 AMPL 格式,但是关于如何构建这样的文件的解释很少。
目前正在这样做:
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()
感谢任何帮助!
示例文件给出了 B 的每个元素的索引:
10.5
2 0.2
30.3
但看起来您的代码似乎只是在创建值,而不是索引:
0.5
0.2
0.3
AMPL .dat
如 , 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 中所述。
我有一个用于优化参数的 .dat 文件:
param: A:= 100;
param: B:=
1 0.5
2 0.2
3 0.3;
我正在尝试重现 .dat 文件,但 A 和 B 的值不同,我需要将它集成到代码中,所以我编写了一个脚本,将字符串写入 .dat 文件,但是当我 运行 在我的模型中它会产生错误。我相信这与格式有关,根据 Pyomo 的说法,.dat 文件应该是 AMPL 格式,但是关于如何构建这样的文件的解释很少。
目前正在这样做:
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()
感谢任何帮助!
示例文件给出了 B 的每个元素的索引:
10.5
2 0.2
30.3
但看起来您的代码似乎只是在创建值,而不是索引:
0.5
0.2
0.3
AMPL .dat
如