Skip to content
Advertisement

Print linear constraints of CPLEX LP on Python

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:

import cplex

problem = cplex.Cplex()
problem.objective.set_sense(problem.objective.sense.minimize)

names = []
upper_bounds = []
lower_bounds = []
types = []
constraint_names = []
constraints = []

for i in range(0, 12):
        for j in range(0, 12):
            names.append("x" + str(i) + "_" + str(j))
            types.append(problem.variables.type.integer)
            upper_bounds.append(1.0)
            lower_bounds.append(0.0)

for i in range(0, 12):
    names.append("y" + str(i))
    types.append(problem.variables.type.integer)
    upper_bounds.append(1.0)
    lower_bounds.append(0.0)
        
problem.variables.add(obj=objective,
                      lb=lower_bounds,
                      ub=upper_bounds,
                      types = types,
                      names=names)

# Constraints
for i in range(0, 22):
    constraint_names.append("c" + str(i))

for j in range(1, 12):
    variables = []
    for i in range(0, 12):
        variables.append("x" + str(i) + "_" + str(j))
    variables.append("y" + str(j))
    constraints.append([variables, ([1.0] * 12) + [-1.0]])

for i in range(0, 11):
    variables = []
    for j in range(0, 12):
        variables.append("x" + str(i) + "_" + str(j))
    variables.append("y" + str(i))
    constraints.append([variables, ([1.0] * 12) + [-1.0]])

problem.linear_constraints.add(lin_expr=constraints, senses=constraint_senses, rhs=rhs, names=constraint_names)

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:

problem.write("prob.lp")
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement