Comments and answers for "How can I work with matrix with DOCplex?"
https://developer.ibm.com/answers/questions/247096/how-can-i-work-with-matrix-with-docplex.html
The latest comments and answers for the question "How can I work with matrix with DOCplex?"Comment by DanielJunglas on DanielJunglas's comment
https://developer.ibm.com/answers/comments/249785/view.html
I am not clear what you are trying to do. var_new_pattern * items is not a constraint at all. It is at best a left-hand side of (a set of) constraints. So add_constraint() cannot work on this expression, no matter what.
You need at least a sense (<=, >=, ==) and a right-hand side. You could for example try something like this (untested code):
rhs = [ i + i for i in range(0, product.shape[0]) ]
for (row, col), expr in numpy.ndenumerate(product):
model.add_constraint(expr <= rhs[row])
which creates a <=-constraint for each element in 'product'.Tue, 26 Jan 2016 09:50:00 GMTDanielJunglasComment by betacampello on betacampello's answer
https://developer.ibm.com/answers/comments/249575/view.html
Hello Daniel, thanks for your help. But than the problem will be after create a variable, I will need to use it for a constraint in docplex like: model.add_constraint(var_new_pattern * items), than the docplex doesn't accept the variable if it was changed in numpy matrix. I tried your suggestion to get the results after solve cplex and its work very well. But I still have the problem when I'm using docplex :(Mon, 25 Jan 2016 13:00:40 GMTbetacampelloAnswer by p_couronne
https://developer.ibm.com/answers/answers/247663/view.html
I concur with Daniel's answer. DOcplex variable creation methods return Python collections (dict, lists).
The xxx_var_matrix methods return dictionaries indexed by two-element tuples as in:
<pre>
im = integet_var_matrix(2, 3, name='x')\n
x11 = m[1,1] # prints: docplex.mp.linear.Var[x_1_1]
</pre>
But DOcplex will not build numpy structures. You have to fill numpy arrays yourself, either from single variables
(as in Daniel's example) or using the lists returned by DOcplex:
<pre>
ll = mm.integer_var_list(3, 2, name="ii")
nll = np.array(ll)
nll[1] # print docplex.mp.linear.Var[ii_1]
</pre>
Numpy array multiplication will be supported in the next drop released end of January.Tue, 12 Jan 2016 09:33:05 GMTp_couronneAnswer by DanielJunglas
https://developer.ibm.com/answers/answers/247444/view.html
The following works for me (I am not a numpy expert):
var_new_pattern = numpy.matrix([[ model.integer_var(name='x[%d,%d]' % (i,j)) for i in range(0,3)] for j in range(0,4)])
items = numpy.matrix([(i + 1) for i in range(0,3)]).transpose()
product = var_new_pattern * items
print product[0,0] # Prints x[0,0]+2x[1,0]+3x[2,0]Mon, 11 Jan 2016 06:31:12 GMTDanielJunglas