Search code examples
pythonmemorycpupyomoglpk

MemUsage / CPU Usage of GLPK for optimization models


I am optimizing 2 pyomo energy models with a solver called GLPK.

When you let solver write its output via: result = optim.solve(model, tee=True)

Solver gives an output in cmd as:

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

I would like to compare the Memory used and Time used of GLPK from both models. How would I get this Memory used and Time used as float values in python? Is there a way? or what else could you offer me, eventually does the same thing?


Solution

  • Step 1:

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

    Step 2,3 and 4:

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