简体   繁体   中英

ValueError invalid matrix input type -- in Python Cplex

I am working with cplex in python, however, I get the following error (in line 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)

I figured out it has something to do with my definition of z, however, I can't manage to fix the error. The error message refers to some matrix, but I am unable to figure out which one. I checked the type of the input and they are all floats, which they should be.

Suggestions are highly appreciated!

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

In the constraint definition, there is a list of 2 lists [[position], [values]].

for example in your code =

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

Hence "sum(z)" and "0.0" are the positions of values 1.0 and 0.0 in the constraint coefficient matrix.

The ERROR is due to the fact that there is "0.0" in the position list, and not "0".

That is, if you change the 0.0 position to 0 or write integer positions and not float position for all the constraint definitions, then this error should be resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM