[英]receiving "None" as result in CVXPY problem solver
Trying to solve TSP as linear programming task using cvxpy and have problem with this.尝试使用 cvxpy 将 TSP 解决为线性规划任务,但遇到了问题。 It is my first experience so thanks for help.
这是我的第一次经历,所以感谢您的帮助。 As a result I want to have matrix with 0 and 1 that shows every next city for salesman.
因此,我想要有 0 和 1 的矩阵,显示每个下一个城市的推销员。
need to use exactly cvxpy需要完全使用 cvxpy
here you can read theory 在这里你可以阅读理论
thanks for help感谢帮助
import cvxpy as cp
import numpy as np
np.random.seed(1)
N = 10
distances = np.random.rand(N, N)
x = cp.Variable((N, N), boolean=True)
u = cp.Variable(N, integer=True)
constraints = []
for j in range(N):
indices = list(range(0, j)) + list(range(j + 1, N))
constraints.append(cp.sum(x[indices, j]) == 1)
for i in range(N):
indices = list(range(0, i)) + list(range(i + 1, N))
constraints.append(cp.sum(x[i, indices]) == 1)
for i in range(1, N):
for j in range(1, N):
if i != j:
constraints.append(u[i] - u[j] + N*x[i, j] <= N-1)
for i in range(N):
for j in range(N):
if i != j:
сost += (x[i,j]*distances[i,j])
prob = cp.Problem(cp.Minimize(cost), constraints)
prob.solve()
print(prob.value)
receive "None"收到“无”
feel like the problem in cost defining, but don`t know how to make it correct maybe I should use cvxpy.multiply or cvxpy.sum?感觉像是成本定义中的问题,但不知道如何使它正确也许我应该使用 cvxpy.multiply 或 cvxpy.sum?
The default MIP-solver coming with cvxpy is just a proof of concept and very unstable in non-tiny instances. cvxpy 附带的默认 MIP 求解器只是一个概念证明,在非小实例中非常不稳定。 Things like that happen then and there is not much you can do.
那样的事情就会发生,而你无能为力。
Like Erwin asked for in aboves comment: activate verbose=True
and you might recognize the solver in use.就像 Erwin 在上面的评论中所要求的那样: activate
verbose=True
你可能会认出正在使用的求解器。 I'm guessing: ECOS_BB
.我猜:
ECOS_BB
。 It's the only one available in the default install.它是默认安装中唯一可用的。
The situation in regards to deploying a MIP solver which:部署 MIP 求解器的情况:
is not great.不是很好。
The easiest approach of obtaining a usable one on windows is using pre-built glpk through cvxopt.获得Windows上的可用一个最简单的方法是使用预建GLPK通过cvxopt。
don't know what was the problem.不知道是什么问题。 maybe smth with Jupyter.
也许与 Jupyter 有关。 everything became ok after it refreshing thanks all for help!
刷新后一切都好起来了,谢谢大家的帮助!
Though it's not the problem here, be sure to call prob.solve()
.虽然这不是这里的问题,但一定要调用
prob.solve()
。 Otherwise, prob.value is None否则, prob.value 为 None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.