I need to print all my linear constraints to verify the correctness of what I wrote but not knowing the CPLEX library for Python well I would not know how to do it.
That’s my part of LP:
JavaScript
51
1
import cplex
2
3
problem = cplex.Cplex()
4
problem.objective.set_sense(problem.objective.sense.minimize)
5
6
names = []
7
upper_bounds = []
8
lower_bounds = []
9
types = []
10
constraint_names = []
11
constraints = []
12
13
for i in range(0, 12):
14
for j in range(0, 12):
15
names.append("x" + str(i) + "_" + str(j))
16
types.append(problem.variables.type.integer)
17
upper_bounds.append(1.0)
18
lower_bounds.append(0.0)
19
20
for i in range(0, 12):
21
names.append("y" + str(i))
22
types.append(problem.variables.type.integer)
23
upper_bounds.append(1.0)
24
lower_bounds.append(0.0)
25
26
problem.variables.add(obj=objective,
27
lb=lower_bounds,
28
ub=upper_bounds,
29
types = types,
30
names=names)
31
32
# Constraints
33
for i in range(0, 22):
34
constraint_names.append("c" + str(i))
35
36
for j in range(1, 12):
37
variables = []
38
for i in range(0, 12):
39
variables.append("x" + str(i) + "_" + str(j))
40
variables.append("y" + str(j))
41
constraints.append([variables, ([1.0] * 12) + [-1.0]])
42
43
for i in range(0, 11):
44
variables = []
45
for j in range(0, 12):
46
variables.append("x" + str(i) + "_" + str(j))
47
variables.append("y" + str(i))
48
constraints.append([variables, ([1.0] * 12) + [-1.0]])
49
50
problem.linear_constraints.add(lin_expr=constraints, senses=constraint_senses, rhs=rhs, names=constraint_names)
51
I want to print out these 22 linear constraints. I’m working with CPLEX 12.9 on Python 3.7.9
Advertisement
Answer
This will write the Model in prob.lp:
JavaScript
2
1
problem.write("prob.lp")
2