Building a linear Python program using Cplex

I am trying to solve a linear program with a lot of variables and restrictions. I need to dynamically generate constraint matrices and build lp in python. The only tutorial I can find on Cplex for Python is official from IBM, which is not completely developed. So my questions are: First, a general question: is there a better textbook or something that is well documented? Secondly, a more specific question, in the official textbook, there is an example showing another method for filling in lp, the problem operator:

Maximize
x1  + 2x2 + 3x3
subject to
–x1 +  x2 + x3 <= 20
x1 – 3x2 + x3 <= 30
with these bounds
0 <= x1 <= 40
0 <= x2 <= infinity
0 <= x3 <= infinity

and fill the line as follows:

def populatebyrow(prob):
    prob.objective.set_sense(prob.objective.sense.maximize)

# since lower bounds are all 0.0 (the default), lb is omitted here
prob.variables.add(obj = my_obj, ub = my_ub, names = my_colnames)

# can query variables like the following:

# lbs is a list of all the lower bounds
lbs = prob.variables.get_lower_bounds()

# ub1 is just the first lower bound
ub1 = prob.variables.get_upper_bounds(0) 

# names is ["x1", "x3"]
names = prob.variables.get_names([0, 2])

rows = [[[0,"x2","x3"],[-1.0, 1.0,1.0]],
        [["x1",1,2],[ 1.0,-3.0,1.0]]]


prob.linear_constraints.add(lin_expr = rows, senses = my_sense,
                            rhs = my_rhs, names = my_rownames)

# because there are two arguments, they are taken to specify a range
# thus, cols is the entire constraint matrix as a list of column vectors
cols = prob.variables.get_cols("x1", "x3")

, rows? , [0,"x2","x3"]? ( ).

!

+4
1

, , , : "", [0, "x2", "x3" ], , , [- 1.0, 1.0,1.0], , , , 0, , "x2" , .add().

+5

Source: https://habr.com/ru/post/1546951/


All Articles