Use the CPLEX Library in Python to Solve Linear Programming Problems

Install the CPLEX Library

Install the CPLEX library in Python:

1
pip install cplex

Example

Max:2x1+6x2+10x3Max: 2x_1+6x_2+10x_3\\

S.T. :

r1:2x1+x22x3<=50r2:x15x2<=20r3:x2+2x3=10r1: 2x_1+x_2-2x_3<=50\\ r2:x_1-5x_2<=20\\ r3:x_2+2x_3=10

Bounds:

x1<=500<=x20<=x3,Intergerx_1<=50\\ 0<=x_2\\ 0<=x_3, Interger\\


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import cplex

# Initialize the model
LP_problem = cplex.Cplex()
# Find the maximum
LP_problem.objective.set_sense(LP_problem.objective.sense.maximize)
# The name of the decision variables
var_name = ["x1", "x2", "x3"]
# Coefficients of independent variables in the objective function
obj_parameters = [2.0, 6.0, 10.0]

# The upper bound of the value range of the decision variables
x_upper_bound = [50.0, cplex.infinity, cplex.infinity]
# The lower bound of the value range of the decision variable
x_lower_bound = [-cplex.infinity, 0.0, 0.0]

# Represents the type of 3 decision variables, where C stands for Continuous (floating-point number) and I stands for Integer
var_type = 'CCI'
LP_problem.variables.add(obj=obj_parameters, ub=x_upper_bound, lb=x_lower_bound, types=var_type, names=var_name)

print("The lower limit of the value of the variable: ", LP_problem.variables.get_lower_bounds())
print("The upper limit of the value of the variable: ", LP_problem.variables.get_upper_bounds())
print("Variable Name: ", LP_problem.variables.get_names())
print("=========================================================\n")

# In the constraint, L means less than or equal to, E means equal to, refer to the following links: https://www.ibm.com/docs/en/icos/12.9.0?topic=g-cpxxgetsense-cpxgetsense
senses = "LLE"
rhs = [50.0, 20.0, 10.0]
row_name = ["r1", "r2", "r3"]
rows = [[["x1", "x2", "x3"], [2.0, 1.0, -2.0]],
[["x1", "x2", "x3"], [1.0, -5.0, 0.0]],
[["x1", "x2", "x3"], [0.0, 1.0, 2.0]]]
LP_problem.linear_constraints.add(lin_expr=rows, senses=senses, rhs=rhs, names=row_name)
LP_problem.solve()

# Result output
print("\n=========================================================")
print("Objective Function Values: ", LP_problem.solution.get_objective_value())
print("Optimal solution: ", LP_problem.solution.get_values())

Results

The output of the program is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
The lower limit of the value of the variable:  [-1e+20, 0.0, 0.0]
The upper limit of the value of the variable: [50.0, 1e+20, 1e+20]
Variable Name: ['x1', 'x2', 'x3']
=========================================================

Version identifier: 22.1.1.0 | 2023-02-09 | 22d6266e5
CPXPARAM_Read_DataCheck 1
Found incumbent of value 100.000000 after 0.00 sec. (0.00 ticks)
Tried aggregator 2 times.
Aggregator did 1 substitutions.
Reduced MIP has 2 rows, 2 columns, and 4 nonzeros.
Reduced MIP has 0 binaries, 1 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.00 sec. (0.00 ticks)
Tried aggregator 1 time.
MIP Presolve modified 1 coefficients.
Reduced MIP has 2 rows, 2 columns, and 4 nonzeros.
Reduced MIP has 0 binaries, 2 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.00 sec. (0.00 ticks)
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 12 threads.
Root relaxation solution time = 0.00 sec. (0.00 ticks)

Nodes Cuts/
Node Left Objective IInf Best Integer Best Bound ItCnt Gap

* 0+ 0 100.0000 120.0000 20.00%
0 0 108.3333 2 100.0000 108.3333 0 8.33%
* 0+ 0 108.0000 108.3333 0.31%
0 0 cutoff 108.0000 108.3333 0 0.31%
Elapsed time = 0.01 sec. (0.02 ticks, tree = 0.01 MB, solutions = 2)

Root node processing (before b&c):
Real time = 0.01 sec. (0.02 ticks)
Parallel b&c, 12 threads:
Real time = 0.00 sec. (0.00 ticks)
Sync time (average) = 0.00 sec.
Wait time (average) = 0.00 sec.
------------
Total (root+branch&cut) = 0.01 sec. (0.02 ticks)

=========================================================
Objective Function Values: 108.0
Optimal solution: [28.0, 2.0, 4.0]