【问题标题】:ValueError invalid matrix input type -- in Python CplexValueError 无效的矩阵输入类型——在 Python Cplex 中
【发布时间】:2017-12-21 15:51:49
【问题描述】:

我在 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())

【问题讨论】:

  • 该错误发生在哪一行?让我们看看整个堆栈跟踪。
  • 查看我添加的 Traceback
  • 通过替换约束定义来修复错误:在第一个子列表中将变量作为字符串引用(或使用它们各自的索引),以解决问题。
  • 如果您已经解决了问题,您可以发布自己的问题的答案。

标签: python cplex


【解决方案1】:

在约束定义中,有一个由 2 个列表组成的列表 [[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 或写入整数位置而不是浮点位置,则应该解决此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2016-04-28
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    相关资源
    最近更新 更多