I am using cvxopt.glpk.ilp to solve a very complicated Mixed Integer Program. I was wondering if there is a way to get the program to terminate after finding the first solution? It takes too long and a feasible solution would work fine for my purposes.
2 Answers
If I understand you correctly, you are only interested in a feasible solution?
Then it should be sufficient to set the objective function to zero.
Here is a Wikipedia example for an integer linear program and the following Python code returns a feasible solution:
import cvxopt
import cvxopt.glpk
# original program maximizing the second variable
# c=cvxopt.matrix([0,-1],tc='d')
# modified program returning only a feasible solution
c=cvxopt.matrix([0,0],tc='d')
G=cvxopt.matrix([[-1,1],[3,2],[2,3],[-1,0],[0,-1]],tc='d')
h=cvxopt.matrix([1,12,12,0,0],tc='d')
(status, x)=cvxopt.glpk.ilp(c,G.T,h,I=set([0,1]))
print('status',status)
print('variables',x[0],x[1])
print('objective',sum(c.T*x))

- 1,909
- 1
- 20
- 32
If you're using PuLP (another python library like cvxopt) to invoke glpk to solve MIP, there is one parameter called maxtime
. If you set maxtime=1
then what will solver do is, terminate search (almost) right after finding the first solution. I bet cvxopt should have some similar parameter for glpk since either PuLP or cvxopt is just a wrapper of those solvers.
Copy paste the description of maxtime
parameter in Xpress which is another solver, but I think glpk should have something similar which you may need to find it out.
The maximum time in seconds that the Optimizer will run before it terminates, including the problem setup time and solution time. For MIP problems, this is the total time taken to solve all the nodes.
0 = No time limit.
n > 0 = If an integer solution has been found, stop MIP search after n seconds, otherwise continue until an integer solution is finally found.
n < 0 = Stop in LP or MIP search after -n seconds.

- 139
- 1
- 4