我已经编写了一些使用cvxpy库解决整数编程问题的代码,但是该代码花费了太多时间来运行,我想知道是否有什么方法可以使代码更快?
在这种情况下,整数编程问题采用形状为(1,569 x 3,071)的矩阵,并且需要满足3,071个约束。代码如下:
mat_f = sys.argv[1]
matIdx2genome_dic_f = sys.argv[2]
genomes_f = sys.argv[3]
with open(matIdx2genome_dic_f, 'r') as in_f:
matIdx2genome_dic = json.load(in_f)
M = np.load(mat_f)
selection = cp.Variable(M.shape[1], boolean = True)
ones_vec = np.ones(M.shape[1])
constraints = []
for i in range(len(M)):
constraints.append(M[i] * selection >= 1)
total_genomes = ones_vec * selection
problem = cp.Problem(cp.Minimize(total_genomes), constraints)
print('solving the integer programming problem: ')
time = time.time()
problem.solve(parallel = True)
print('problem solved in: '+ str(time.time() - time))
solution = selection.value
solution = list(map(round, solution))
solution = np.array(solution)
which_genomes = np.where(solution == 1.0)[0]
with open(genomes_f, 'w') as out_f:
for idx in which_genomes:
out_f.write(matIdx2genome_dic[idx]+'\n')
第一个命令行参数在这里很重要,它是一个形状为(1569,3071)的numpy二进制矩阵。 这里的问题是最小化所需矩阵的列数,以使每一行的行中至少有1。
我的问题是如何编写此脚本,以便使其运行更快?有办法并行化吗?我已经在Solve方法中将parallel参数设置为True,因为我正在监视cpu利用率,所以我认为它并没有多大作用,而且它只有100%,所以我不认为parallel选项有什么作用? 还是有另一种方法(我应该称其为求解器)可以更快地解决此问题?