Comments and answers for "How to formulate decision variables and constraints in matrix form in cplex?"
https://developer.ibm.com/answers/questions/514564/how-to-formulate-decision-variables-and-constraint.html
The latest comments and answers for the question "How to formulate decision variables and constraints in matrix form in cplex?"Comment by DanielJunglas on DanielJunglas's comment
https://developer.ibm.com/answers/comments/515248/view.html
For a matrix you need two dimensions, so you have to specify as second argument something that contains only one element. For example
n = mdl.integer_var_matrix(source, range(1))
Alternatively use `mdl.integer_var_list()` (you may want to scan the reference documentation for the various functions that create variables.Tue, 20 Aug 2019 07:05:46 GMTDanielJunglasComment by Jason Thom on Jason Thom's comment
https://developer.ibm.com/answers/comments/515243/view.html
Hey Daniel I got you. I have changed my follow up question to comment sorry I’m new here so I realised it after I posted my comment.
I have defined
d = mdl.integer_var_matrix(rows, cols)
I have put the constraint accordingly as da(i,j) + dc(i,j) == d(i, j)
Now I have to define another decision variable “n” which is my number of autonomous vehicles currently at a source, so it’s a matrix size is (4x1)
So when I define n = mdl.integer_var_matrix(source) it gives me error, keys2 missing
How do I define it? When there is only 1 column. aa my number of vehicles are location specific not pair specific.Tue, 20 Aug 2019 06:47:27 GMTJason ThomComment by DanielJunglas on DanielJunglas's comment
https://developer.ibm.com/answers/comments/515236/view.html
The choice of `d` in my example code is completely arbirtrary. In your question you did not specify what `d` is. If `d` is a matrix you probably want something like
mdl.add_constraint(da[i,j] + dc[i,j] == d[i,j])
From the code you attached it is not clear what the remaining problem is. Can you please elaborate on that?Tue, 20 Aug 2019 06:08:27 GMTDanielJunglasComment by Jason Thom on Jason Thom's answer
https://developer.ibm.com/answers/comments/515240/view.html
Hey @DanielJunglas Thank you for the reply! I am using docplex package in Pycharm IDE for python
In the code you wrote above why did you specify d=1.0 ,
Also, when you specified the below constraint what is the size of d? Is d another matrix or single value.
mdl.add_constraint(da[i,j] + dc[i,j] == d)
In my vehicle allocation problem,
source(1,2,3,4) destination (1,2,3,4)
da=demand to be fulfilled by autonomous vehicles is a (4 x 4) matrix
dc=demand to be fulfilled by manual vehicles is a (4 x 4) matrix
d=total demand is also (4 x 4) matrix
Matrix da, dc are my decision variables and matrix d is the input data that I'm providing to it.
I'm using d = {(i, j): np.random.poisson(lam=50) for i in source for j in target} to generate values of d.
I have attached a file with my code, I dont know how I do it in matrix form I have used list and for loops to solve the optimization problem. Please help me on how I can do it in matrix form.
[link text][1]
[1]: /answers/storage/temp/29192-new-trial.txtTue, 20 Aug 2019 02:59:17 GMTJason ThomAnswer by DanielJunglas
https://developer.ibm.com/answers/answers/515118/view.html
Since you tagged the question "docplex" I assume you are using docplex and not the Python CPLEX API (which is more low level). In order to create a matrix of decision variables you can use functions `Model.binary_var_matrix()`, `Model.continous_var_matrix()`, or `Model.integer_var_matrix()`. These are all documented and you can look them up in the reference documentation (or via "info(Model)" in a Python shell).
For example
x = mdl.integer_var_dict(range(3), range(4))
will create a 3 by 4 matrix of integer decision variables. Constraints are added with the `add_constraint` function (check the reference documentation). Here is a small example that creates the variables and the constraint you listed:
with Model() as mdl:
rows = range(3)
cols = range(4)
da = mdl.integer_var_matrix(rows, cols)
dc = mdl.integer_var_matrix(rows, cols)
d = 1.0
for i in rows:
for j in cols:
mdl.add_constraint(da[i,j] + dc[i,j] == d)
Alternatively, the constraints can be added with a single line:
mdl.add_constraints(da[i,j] + dc[i,j] == d for i in rows for j in cols)Mon, 19 Aug 2019 08:02:57 GMTDanielJunglasComment by Jason Thom
https://developer.ibm.com/answers/comments/514823/view.html
@DanielJunglas can you help?Wed, 14 Aug 2019 17:28:52 GMTJason Thom