我在python中使用cplex,但是,我收到以下错误(在第144行):
追踪(最近一次通话): 文件" objectivefunction_D.py",第144行,in names = constraint_names) 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_subinterfaces.py" ;,第1402行,另外 lin_expr,sense,rhs,range_values,names) 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_subinterfaces.py" ;,第160行,在_add_iter中 addfun(* args,** kwargs) 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_subinterfaces.py" ;,第1337行,在_add self._env._apienc)as(rmat,nnz): 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py" ;,第17行,在输入 return self.gen.next() 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_procedural.py" ;,第187行,在chbmatrix中 mat = Pylolmat_to_CHBmat(lolmat,env_lp,r_c,enc) 文件" /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cplex/_internal/_procedural.py",第195行,在Pylolmat_to_CHBmat中 返回CR.Pylolmat_to_CHBmat(lolmat,get_indices,r_c,cpx_transcode,enc) ValueError :('无效的矩阵输入类型 - ',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())
答案 0 :(得分:0)
在约束定义中,有一个包含 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 或写入整数位置而不是浮点位置,则应该解决此错误。