内存使用情况 / 用于优化模型的 GLPK 的 CPU 使用情况



我正在使用一个名为GLPK的求解器优化2个pyomo能量模型。

当您让求解器通过以下方式写入其输出时:result = optim.solve(model, tee=True)

求解器给出的输出以 cmd 为单位为:

GLPSOL: GLPK LP/MIP Solver, v4.65
Parameter(s) specified in the command line:
 --write /home/okan/Desktop/urbs-oemof/tmpk76ybo_5.glpk.raw --wglp /home/okan/Desktop/urbs-oemof/tmp8qv9hajy.glpk.glp
 --cpxlp /home/okan/Desktop/urbs-oemof/tmpa9k8e86c.pyomo.lp
Reading problem data from '/home/okan/Desktop/urbs-oemof/tmpa9k8e86c.pyomo.lp'...
1519 rows, 1155 columns, 3894 non-zeros
9618 lines were read
Writing problem data to '/home/okan/Desktop/urbs-oemof/tmp8qv9hajy.glpk.glp'...
7210 lines were written
GLPK Simplex Optimizer, v4.65
1519 rows, 1155 columns, 3894 non-zeros
Preprocessing...
1159 rows, 1011 columns, 2979 non-zeros
Scaling...
 A: min|aij| =  4.705e-03  max|aij| =  7.964e+02  ratio =  1.693e+05
GM: min|aij| =  4.065e-01  max|aij| =  2.460e+00  ratio =  6.052e+00
EQ: min|aij| =  1.652e-01  max|aij| =  1.000e+00  ratio =  6.052e+00
Constructing initial basis...
Size of triangular part is 1156
      0: obj =   1.914903944e+07 inf =   8.143e+06 (60)
     68: obj =   7.130756139e+10 inf =   0.000e+00 (0)
*   266: obj =   2.358801019e+10 inf =   1.920e-10 (0) 1
OPTIMAL LP SOLUTION FOUND
Time used:   0.0 secs
Memory used: 1.6 Mb (1728515 bytes)
Writing basic solution to '/home/okan/Desktop/urbs-oemof/tmpk76ybo_5.glpk.raw'...
2683 lines were written

我想比较两种型号的GLPK Memory usedTime used。我将如何在 python 中将此Memory usedTime used作为浮点值?有办法吗?或者你还能给我什么,最终做同样的事情?

我不知道

是否有更直接的方法,但这就是我从求解器日志中检索信息的方式。

  1. 解决问题,同时将求解器日志文件保存在以后可以获取的位置。

    results = opt.solve(model, logfile='MySolverLogFile.txt')
    
  2. 使用 python 文件读取器打开日志文件,并以字符串形式检索整个文件。
  3. 在字符串中搜索关键字。确保进行非常严格的搜索。这意味着,如果您的内存使用情况像"Memory used: "一样给出,不仅要搜索"Memory",还要搜索整个单词,包括双点和空格,以避免出现错误。
  4. 通过将文本"使用的内存:"之后找到的值存储到变量中,使该值成为已用内存量。

如果您对其进行编码以接受其他关键字,您将能够检索许多其他信息。但是,如果更改求解器,则必须更改从求解器日志中检索这些信息的方式。

第 1 步:

result = optim.solve(model, logfile='log.txt', tee=False)

步骤 2、3 和 4:

with open('log.txt', 'r') as log:
    mem = log.read().replace('n', ' ')
    mem = float(mem[mem.find('Memory used:')+12:
                    mem.find('Mb')])

最新更新