2

I don't want my code to print anything to the terminal.

Right now, the line I'm running is:

prob.solve(cp.GLPK_MI, glpk={'msg_lev': 'GLP_MSG_OFF'}, verbosity=False)

And it's displaying the text:

Long-step dual simplex will be used

I've looked at this (which has been closed) and this (which doesn't seem to work) so far, but don't know the right options to pass to get the code to not print anything.

How can I stop the GLPK solver from displaying text?

If you want an example to test it for yourself, below is my full runnable example code (which can be understood by looking here):

'''
Requirements.txt:
cvxopt==1.2.6
cvxpy==1.1.10
ecos==2.0.7.post1
numpy==1.20.1
osqp==0.6.2.post0
qdldl==0.1.5.post0
scipy==1.6.1
scs==2.1.2
'''
from typing import List
import math
import cvxpy as cp
import numpy as np

def solve(mech_data: List[List[float]], max_time_limit) -> List[int]:

    # number of mechanisms
    n = len(mech_data)

    # for a max time limit of 10, you need slots for each mech for time=0 and time=10
    # ...and everything in between which is 10 + 1 = 11 slots
    m = max_time_limit + 1

    log_data = []
    # log the data for the formula
    for row in mech_data:
        log_row = []
        for col in row:
            if col == 1:
                new_col = float('-inf')
            else:
                new_col = math.log(1 - col)

            log_row.append(new_col)
        log_data.append(log_row)

    log_data = np.array(log_data)

    x = cp.Variable((n, m), boolean=True)
    objective = cp.Minimize(cp.sum(cp.multiply(log_data, x)))

    # the current solver doesn't work with equalities, so each equality must be split into two inequalities.
    # see https://github.com/cvxgrp/cvxpy/issues/1112
    one_choice_per_mech_constraint = [cp.sum(x[i]) <= 1 for i in range(n)] + [cp.sum(x[i]) >= 1 for i in range(n)]

    # constrain the solution to the real time
    js = np.tile(np.array(list(range(m))), (n, 1))
    time_constraint = [cp.sum(cp.multiply(js, x)) <= max_time_limit, cp.sum(cp.multiply(js, x)) >= max_time_limit]
    constraints = one_choice_per_mech_constraint + time_constraint

    prob = cp.Problem(objective, constraints)
    # THIS SHOULD NOT PRINT ANYTHING
    prob.solve(cp.GLPK_MI, glpk={'msg_lev': 'GLP_MSG_OFF'}, verbosity=False)
    # >> Long-step dual simplex will be used
    return list(np.argmax(x.value, axis=1))

if __name__ == "__main__":
    m = 11

    # has 30% chance of working if run at least 3 times
    A_list = [0, 0, 0] + [0.3] * (m - 3)

    # has 30% chance of working if run at least 3 times
    B_list = [0, 0, 0] + [0.3] * (m - 3)

    # has 40% chance of working if run 4 times, 30% otherwise
    C_list = [0.3, 0.3, 0.3, 0.3] + [0.4] * (m - 4)

    data = [A_list, B_list, C_list]

    ans = solve(data, m-1)
Pro Q
  • 4,391
  • 4
  • 43
  • 92
  • Note that you don't have to pass in `glpk={'msg_lev': 'GLP_MSG_OFF'}`, `verbose=False` tells `cvxpy` [to set the exact same option](https://github.com/cvxgrp/cvxpy/blob/c88f09215252c932c101c842dd684a60c16ea4cf/cvxpy/reductions/solvers/conic_solvers/glpk_mi_conif.py#L51-L60). – Martijn Pieters Mar 20 '21 at 17:44

1 Answers1

3

Unfortunately, the message is generated by GLPK 4.65, regardless of the GLP_MSG_* option used.

However, it appears that version 5.0 has fixed this, see this glpk-help mailing-list message from the library maintainer:

To fix the bug please replace lines 923-930 in glpk/src/draft/glpios03.c

#if 1 /* 16/III-2016 */
      if (((glp_iocp *)T->parm)->flip)
#if 0 /* 20/I-2018 */
         xprintf("WARNING: LONG-STEP DUAL SIMPLEX WILL BE USED\n");
#else
         xprintf("Long-step dual simplex will be used\n");
#endif
#endif

with the following ones:

#if 1 /* 01/III-2018 */
      if (((glp_iocp *)T->parm)->flip)
        if (T->parm->msg_lev >= GLP_MSG_ALL)
            xprintf("Long-step dual simplex will be used\n");
#endif

Please note that this change will appear in the next release of glpk.

The “next release” is 5.0, and only was published in December 2020 and the cvxopt wheels have not yet been updated.

I’ve requested an update, see issue #8 in the cvxopt wheels project.

Alternatively, you could build cvxopt from source, at which point you just make sure it is built with the new release or with a version of GLPK with the code that produces the message disabled.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343