cvxpy Repeatedly solving a problem get the “”Problem is unbounded“”

42 Views Asked by At

I used CVXPY to solve a planning problem, which requires running the same problem multiple times with inputting different parameters. enter image description hereI obtained the result in the form of DCP, which is "Problem is unbound". If the same data replaced by a constant “c” enter image description heredirectly passed in, the correct result can be obtained。the “x” is x = cp.Variable(matrix_len, integer=True) 。

c= np.array([1,2,3,6])
matrix_len = len(c)
gmma = cp.Parameter(matrix_len,nonneg=True)
gmma.value = [1,2,3,6]
# c = np.array([1.42900, 1.45900, 1.31800, 1.27500, 1.26300, 1.24900, 1.25900])
constraint_matrix = np.zeros((matrix_len,matrix_len),dtype='int')
# # 定义约束矩阵      /将矩阵对角线一侧设置为0
for i in range(len(constraint_matrix)):
    for j in range(i + 1):
        constraint_matrix[i][j] = -1
# 生成tmp矩阵
mid_matrix = np.zeros((matrix_len,matrix_len),dtype='int')+1

# # 定义约束矩阵      /将矩阵对角线一侧设置为0
for i in range(matrix_len):
    for j in range(i):
        mid_matrix[j][i] = 0

cs_matrix2 = mid_matrix * gmma

# 生成对角矩阵
# d = np.diag(np.zeros(5))
b = np.zeros((matrix_len),dtype='int')+5685    # 定义约束条件的右边向量
c = np.zeros((matrix_len),dtype='int')
x = cp.Variable(matrix_len, integer=True)  # 定义两个整数决策变量
obj = cp.Minimize(gmma * x)  # 构造目标函数
cons = [constraint_matrix * x <= b,cs_matrix2 * x <= c]  # 构造约束条件
prob = cp.Problem(obj, cons)  # 构建问题模型
prob.solve(solver='CBC', verbose=True )  # 求解问题
print("最优值为:", prob.value+c[0]*5685)

the second one using the common method

c= np.array([1,2,3,6])
matrix_len = len(c)
# c = np.array([1.42900, 1.45900, 1.31800, 1.27500, 1.26300, 1.24900, 1.25900])
constraint_matrix = np.zeros((matrix_len,matrix_len),dtype='int')
# # 定义约束矩阵      /将矩阵对角线一侧设置为0
for i in range(len(constraint_matrix)):
    for j in range(i + 1):
        constraint_matrix[i][j] = -1
# 生成tmp矩阵
mid_matrix = np.zeros((matrix_len,1),dtype='int')+1
cs_matrix2 = mid_matrix*c
# # 定义约束矩阵      /将矩阵对角线一侧设置为0
for i in range(len(cs_matrix2)):
    for j in range(i):
        cs_matrix2[j][i] = 0

# 生成对角矩阵
# d = np.diag(np.zeros(5))
b = np.zeros((matrix_len),dtype='int')+5685    # 定义约束条件的右边向量
c = np.zeros((matrix_len),dtype='int')
x = cp.Variable(matrix_len, integer=True)  # 定义两个整数决策变量
obj = cp.Minimize(c * x)  # 构造目标函数
cons = [constraint_matrix * x <= b,cs_matrix2 * x <= c]  # 构造约束条件
prob = cp.Problem(obj, cons)  # 构建问题模型
prob.solve(solver='CBC', verbose=True )  # 求解问题
print("最优值为:", prob.value+c[0]*5685)

using the same solver and data produce different results ,i dont know why

0

There are 0 best solutions below