2020-10-17 14:17

# 用python3.7调用cplex12.9解决一个简单的tsp问题报错CPLEX Error 1217: No solution exists.

## 下面是代码

``````"""
1.这次解决的是四城市数量的旅行商问题
2.使用线性规划模型
3.调用cplex包
"""
import cplex
from cplex.exceptions import CplexError

# 将城市节点数量与他们之间的距离表示出来
number = 4
nodes = 3
my_obj = [0, 2, 5, 7, 2, 0, 3, 8, 5, 3, 0, 10, 7, 8, 10, 0, 0, 0, 0, 0]
# 变量的名称
b_var = ["x11", "x12", "x13", "x14", "x21", "x22", "x23", "x24", "x31", "x32", "x33", "x34",
"x41", "x42", "x43", "x44"]
b_var_ind_format = [["x11", "x12", "x13", "x14"], ["x21", "x22", "x23", "x24"], ["x31", "x32", "x33", "x34"],
["x41", "x42", "x43", "x44"]]
c_var = ["u1", "u2", "u3", "u4"]
my_sense_b = "E" * 12  # 限流变量约束条件类型定义
my_sense_c = "L" * 12  # 连续变量约束条件类型定义
my_rownames_b = ["r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"]  # 约束条件的名称
my_rownames_c = []
list_c = []  # 存储约束条件中变量和他们系数的小列表
rows_c = []
# 下面是一个构建连续变量约束条件系数矩阵的循环程序
k = 0
for i in range(4):
for j in range(4):
if i != j:
list_c.append([c_var[i], c_var[j], b_var_ind_format[i][j]])
# 上面是完成了一个变量的小组合
list_c.append([1, -1, number])
rows_c.append([list_c[k], list_c[k + 1]])
k += 2
rhs_n = len(rows_c)
# print(rhs_n)
print("定义连续变量的约束条件矩阵为：",rows_c)
# rhs = [float(nodes)] * int(rhs_n)
# print(len(rhs)),这些都是找错误用的
# 下面构建连续约束条件的名字大概就是c1,c2,c3这么个意思
for i in range(int(rhs_n)):
my_rownames_c.append("c" + str(i + 1))

def tsp_method(prob):
"""
:param prob:代表建立起的cplex实例
:return: 无返回值
实现变量的添加，设置好约束条件了。针对旅行商问题我们设置的变量为0-1变量，约束条件为MZT模型
"""
prob.objective.set_sense(prob.objective.sense.minimize)  # 目标函数的意义

prob.variables.add(names=b_var + c_var, types="B" * 16 + "C" * 4, obj=my_obj)  # 定义变量名，定义0-1类型，定义目标函数系数
# prob.variables.add(names=c_var, types="C" * 4)  # 定义连续变量，消除子环

rows = [[["x21", "x31", "x41"], [1.0, 1.0, 1.0]],
[["x12", "x32", "x42"], [1.0, 1.0, 1.0]],
[["x13", "x23", "x43"], [1.0, 1.0, 1.0]],
[["x14", "x24", "x34"], [1.0, 1.0, 1.0]],
[["x12", "x13", "x14"], [1.0, 1.0, 1.0]],
[["x21", "x23", "x24"], [1.0, 1.0, 1.0]],
[["x31", "x32", "x34"], [1.0, 1.0, 1.0]],
[["x41", "x42", "x43"], [1.0, 1.0, 1.0]],
[["x11"], [1.0]],
[["x22"], [1.0]],
[["x33"], [1.0]],
[["x44"], [1.0]]]  # 这个是定义限制流量的约束条件的系数
rhs = [float(nodes)] * int(rhs_n)
prob.linear_constraints.add(lin_expr=rows + rows_c, senses=my_sense_b + my_sense_c,
rhs=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0] + rhs,
names=my_rownames_b + my_rownames_c)  # 建立好约束条件
#                             rhs=[float(nodes)] * int(rhs_n),
#                             names=my_rownames_c)
my_prob = cplex.Cplex()
handle = tsp_method(my_prob)
my_prob.solve()
# solution.get_status() returns an integer code
print("Solution status = ", my_prob.solution.get_status(), ":", end='')
# the following line prints the corresponding string
print(my_prob.solution.status[my_prob.solution.get_status()])
# print("Solution value  = ", my_prob.solution.get_objective_value())  # 这个函数是返回目标函数值

numcols = my_prob.variables.get_num()
numrows = my_prob.linear_constraints.get_num()

x = my_prob.solution.get_values()

print("x: ")
print(x)

``````

CPLEX Error 1217: No solution exists.
Traceback (most recent call last):
File "E:/software_location/pycharm_2020.2/pycharm2020.2/ran/TSP.py", line 102, in
x = my_prob.solution.get_values()
File "E:\software_location\pycharm_2020.2\pycharm2020.2\ran\cplex_internal_subinterfaces.py", line 7612, in get_values
getx, self._cplex.variables._conv, args)
File "E:\software_location\pycharm_2020.2\pycharm2020.2\ran\cplex_internal_aux_functions.py", line 148, in apply_freeform_two_args
return fn(0)
File "E:\software_location\pycharm_2020.2\pycharm2020.2\ran\cplex_internal_subinterfaces.py", line 7610, in getx
return CPX_PROC.getx(self._env._e, self._cplex._lp, a, b)
File "E:\software_location\pycharm_2020.2\pycharm2020.2\ran\cplex_internal_procedural.py", line 2043, in getx
check_status(env, status)
File "E:\software_location\pycharm_2020.2\pycharm2020.2\ran\cplex_internal_procedural.py", line 303, in call
raise CplexSolverError(error_string, env, status)
cplex.exceptions.errors.CplexSolverError: CPLEX Error 1217: No solution exists.

Process finished with exit code 1

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答