python - ValueError 无效矩阵输入类型——在 Python Cplex 中

标签 python cplex

我在 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 的定义有关,但是,我无法修复错误。错误消息指的是一些矩阵,但我无法弄清楚是哪一个。我检查了输入的类型,它们都是 float ,它们应该是。

非常感谢您的建议!

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

最佳答案

在约束定义中,有 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 或写入整数位置而不是浮点位置,则应解决此错误。

关于python - ValueError 无效矩阵输入类型——在 Python Cplex 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928430/

相关文章:

python - Opencv(Python)内存使用问题

python - 检测视频文件中的人脸

python - 如何在 Python 3.7.1 中禁用然后重新启用键盘/鼠标

java - CPLEX+Java 中最大化的完整性差距漏洞?

c++ - Cplex C++多维决策变量

python - 检查文件大小时出现语法错误

python - 在 mypy 中使用 python multiprocessing.Lock 作为参数类型

c - 修复与删除 CPLEX 问题中的变量

c++ - 如何解决eclipse中cplex的目录问题?

c++ cplex访问当前解决方案以添加约束