I am trying to run the following optimization problem at Python by using the CPLEX API: $$\min \{x_1 + x_2\ | \ x_1 \geq 3, x_2 \geq 2, 2x_1 + x_2 \geq 9\} $$
I just want to give a matrix of coefficients and implement the problem as simple as possible. However, the easiest way I was able to do was:
import cplex
my_prob = cplex.Cplex()
my_obj = [1,1]
rows = [[["x1","x2"],[-1, 0]], [["x1","x2"],[0, -1]], [["x1","x2"],[-2,-1]]]
my_colnames = ["x1", "x2"]
my_rhs = [-3, -2, -9]
my_prob.variables.add(obj = my_obj, names = my_colnames)
my_prob.objective.set_sense(my_prob.objective.sense.minimize)
my_prob.linear_constraints.add(lin_expr = rows, senses = ["LLL"], rhs = my_rhs)
my_prob.solve()
my_prob.solution.get_values()
My questions are:
- I really don't want to define each constraint of form
[["x1","x2"],[1,0]]
, is there any way I can get rid of the naming? Because I will give huge matrices to CPLEX and I just want to have all the coefficients! I mean, my matrix will include all the variables' coefficients, so I really don't need to give which coefficient is for which variable. - I don't want to give
senses["LLL"]
all the time, but default should stay $\leq$.
Edit for the first case, obviously I can define my_colnames
in the beginning and all ["x1","x2"]
can be replaced by my_colnames
. But still...
cplex
. Trydocplex
(not that I know much aboutcplex
package but I've only trieddocplex
and I've never had any problem with it defining my variables and constraints the way I wanted them and I had thousands of them) $\endgroup$