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)