2

I have a long list of decision variables which basically based on an input data file. However, for convenience, let's use the example below: I am adding decision variables to my model and each of these decision variables are constrained to be between 0 and 1(inclusive). I did the following: (*mylist is a column of dataframe in my program, so i used shape[0])

model.variables.add(obj=mylist, ub=list(np.ones((1, mylist.shape[0])))), types=[model.variables.type.continuous] * len(mylist) )

I might have misunderstood how the python api defines the model construction. I got a CPLEX error: Inconsistent arguments. I am suspecting that the ub is giving me this problem as when I removed it everything works fine. How should I set ub in a correct way? And what does columns argument in the model.variables.add function refer to?

Stanley Gan
  • 481
  • 1
  • 7
  • 19

2 Answers2

1

I did a minor change which is ub=list(np.ones(mylist.shape[0])) and got no errors. After checking the bounds using model.variables.get_upper_bounds(), everything looks fine.

Stanley Gan
  • 481
  • 1
  • 7
  • 19
1

The ub argument should be a sequence of the same size as that of the obj argument. So, rather than ub=list(np.ones((1, mylist.shape[0])))), you'd want to use ub=[1.0]*len(mylist) or ub=np.ones(len(mylist)), etc.

The columns argument allows you to add non-zeroes to the constraint matrix column-wise rather than row-wise (the later would normally be done via the Cplex.linear_constraints.add method). One way to understand what is going on with the columns argument is to look at the example from the documentation.

For example:

import cplex
c = cplex.Cplex()
c.linear_constraints.add(names = ["c0", "c1", "c2"])
c.variables.add(obj = [1.0, 2.0, 3.0],
                types = [c.variables.type.integer] * 3)
c.variables.add(obj = [1.0, 2.0, 3.0],
                lb = [-1.0, 1.0, 0.0],
                ub = [100.0, cplex.infinity, cplex.infinity],
                types = [c.variables.type.integer] * 3,
                names = ["var0", "var1", "var2"],
                columns = [cplex.SparsePair(ind = ['c0', 2], val = [1.0, -1.0]),
                           [['c2'],[2.0]],
                           cplex.SparsePair(ind = [0, 1], val = [3.0, 4.0])])

c.write("example.lp")

After running that, the LP file would look like:

Minimize
 obj: x1 + 2 x2 + 3 x3 + var0 + 2 var1 + 3 var3
Subject To
 c0: var0 + 3 var3  = 0
 c1: 4 var3  = 0
 c2: - var0 + 2 var1  = 0
Bounds
      x1 >= 0
      x2 >= 0
      x3 >= 0
-1 <= var0 <= 100
      var1 >= 1
      var3 >= 0
Generals
 x1  x2  x3  var0  var1  var3 
End

If you look at the first row in the constraint matrix (constraint "c0"), the first column (variable "var0") has a coefficient of 1.0. For the third row (constraint "c2" which has index 2), the first column (variable "var0") has a coefficient of -1.0. This corresponds to the first item in the columns list (i.e., cplex.SparsePair(ind = ['c0', 2], val = [1.0, -1.0])).

rkersh
  • 4,447
  • 2
  • 22
  • 31