Python Cplex中的ValueError无效矩阵输入类型。

3

我正在使用Python中的Cplex,但是我遇到了以下错误(在第144行):

Traceback (most recent call last):
  File "objectivefunction_D.py", line 144, in <module>
    names = constraint_names)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_subinterfaces.py", line 1402, in add
    lin_expr, senses, rhs, range_values, names)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_subinterfaces.py", line 160, in _add_iter
    addfun(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_subinterfaces.py", line 1337, in _add
    self._env._apienc) as (rmat, nnz):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_procedural.py", line 187, in chbmatrix
    mat = Pylolmat_to_CHBmat(lolmat, env_lp, r_c, enc)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_procedural.py", line 195, in Pylolmat_to_CHBmat
    return CR.Pylolmat_to_CHBmat(lolmat, get_indices, r_c, cpx_transcode, enc)
ValueError: (' invalid matrix input type -- ', 1.0)

我发现这与我的z的定义有关,但我无法修复错误。错误信息涉及一些矩阵,但我无法确定是哪一个。我检查了输入的类型,它们都是浮点数,应该没有问题。
非常感谢您提供的建议!
import cplex
import numpy as np

C = 10e6
gamma = 0.01

#Get from dataset
V = np.array([31.,31.5,35.,33.])

#w_lst are the w[i] elements, except for x[i], which is a decision variable
#Remember, w[i]=x[i]V[i][T]/(C-gamma*C)
#y[i] is a combination of x[i] and z[i]
w_lst = []
for i in V:
    w = i*(1/(C-gamma*C))
    w_lst.append(w)

alpha_hat = np.array([0.0008,0.00076,0.00077,0.00078])

#w[i]*alpha[i], remember that alpha_hat = sum(w[i]*alpha_hat[i])
multiplst = w_lst*alpha_hat

#print multiplst[0]

problem = cplex.Cplex()

problem.objective.set_sense(problem.objective.sense.minimize)

#Code for one stock, add z
#names = ["x1","x2","x3","x4","x5","z1","z2",...,"G1","G2",....]

names = ["x","z","G","y"]   #x,z,G are lists
# "z","G","y"
#objective = [sum(multiplst)]

objective = [0., 0., 0., 1.0]

lower_bounds = [0.0, 0.0, 0.0, 0.0]
upper_bounds = [C, 2.0, cplex.infinity, C]

problem.variables.add(obj = objective,
                      lb = lower_bounds,
                      ub = upper_bounds,
                      names = names)

#Add the constraints from constraints.py
# c = ["c1","c2","c3","c4","c5","c6","c7"]
# cy = ["cy1","cy2","cy3","cy4"]
# constraint_names = [c,cy]

constraint_names = ["c1","c2","c3","c4","c5","c6","c7","cy1","cy2","cy3","cy4"]

z = [1.0, 0.0]
x = [10., 12.]
G = [0.05, 0.03]
M = 10e6
K = 10.
V = V[0]
delta = 1.0
epsilon = 0.01
f_s = 0.01
f_b = 0.01
y = z[0]*x[0]
X = [11.,8.]

#=============== constraint1 ========================

#Constraint sense: "E"


first_constraint = [[sum(z),0.0],[1.0,0.0]]


#=============== constraint2_a ========================

#Constraint sense: "L"

second_constraint_a = [[x,z],[V/C,delta]]


#=============== constraint2_b ========================

#Constraint sense: "G"

second_constraint_b = [[x,z],[V/C, -epsilon]]


#=============== constraint3 ========================

#Constraint sense: "G"

third_constraint = [[G,x],[1,f_s*V]]



#=============== constraint4 ========================

#Constraint sense: "G"

fourth_constraint = [[G,x],[1.0,f_b*V]]



#=============== constraint5 ========================

#Constraint sense: "L"

fifth_constraint = [[sum(G),0.0],[1.0,0.0]]


#=============== constraint6 ========================

#Constraint sense: "E"

sixth_constraint = [[sum(x),sum(G)],[V,1.0]]

#================= y[i] constraints =================

#Constraint sense: "L", rhs = M*z
yconstraint1 = [[y],[1.0]]

#Constraint sense: "L", rhs = x
yconstraint2 = [[y],[1.0]]

#Constraint sense: "G", rhs = x-M(1-z)
yconstraint3 = [[y],[1.0]]

#Constraint sense = "G", rhs = 0.0
yconstraint4 = [[y],[1.0]]

#====================================================

constraints = [first_constraint, second_constraint_a, second_constraint_b, third_constraint, fourth_constraint, fifth_constraint, sixth_constraint, yconstraint1, yconstraint2, yconstraint3, yconstraint4]

rhs = [K,0.0,0.0,f_s*X[0]*V,f_b*X[0]*V,gamma*C,C,M*z[0],x[0],x[0]-M*(1.0-z[0]),0.0]

constraint_senses = ["E","L","G","G","G","L","E","L","L","G","G"]

# ===================== Add the constraints ==================
problem.linear_constraints.add(lin_expr = constraints,
                              senses = constraint_senses,
                              rhs = rhs,
                              names = constraint_names)

# Solve the problem
problem.solve()

# And print the solutions
print(problem.solution.get_values())

1
那个错误出现在哪一行?让我们看看整个堆栈跟踪。 - Kevin
1
请查看我添加的回溯信息。 - aeengineer
1
通过替换约束定义,成功修复了错误:在第一个子列表中将变量作为字符串引用(或使用它们各自的索引),以解决此问题。 - aeengineer
如果您已经解决了问题,可以在自己的问题下发布答案。 - jtb
1个回答

0
在约束定义中,有一个由两个列表[[position],[values]]组成的列表。
例如,在您的代码中=
first_constraint = [[sum(z),0.0],[1.0,0.0]]
因此,“sum(z)”和“0.0”是约束系数矩阵中值为1.0和0.0的位置。
错误是由于位置列表中存在“0.0”,而不是“0”。
也就是说,如果您将0.0位置更改为0或者对所有约束定义都写整数位置而不是浮点位置,那么这个错误应该会被解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接