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])
).