Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @Cyrille! I am not completely sure that I understood your question, but you could store constants in a matrix and a vector. For example, suppose you want to rewrite the following MILP:

LP = MixedIntegerLinearProgram(maximization=True)
x = LP.new_variable(nonnegative=True)
LP.add_constraint(-2*x[1] - x[2] >= 1.5)
LP.add_constraint(-x[1] - x[2] >= 1)
LP.add_constraint(-x[1] - 2*x[2] >= 1)
LP.set_objective(5*x[1] - 4*x[2])

Then you could define a matrix A containing all the coefficients for the variables, and a vector containing all the constant terms. In this particular case,

A = matrix(3, 2, [-2, -1, -1, -1, -1, -2])
b = vector([-1.5, -1, -1])

Now, you can rewrite the problem as you indicate in your question:

LP = MixedIntegerLinearProgram(maximization=True)
x = LP.new_variable(nonnegative=True)
LP.add_constraint(A[0,0]*x[1] + A[0,1]*x[2] >= b[0])
LP.add_constraint(A[1,0]*x[1] + A[1,1]*x[2] >= b[1])
LP.add_constraint(A[2,0]*x[1] + A[2,1]*x[2] >= b[2])
LP.set_objective(5*x[1] + 4*x[2])

And you even can reuse the numbers in the Polyhedron command. For example,

pol = Polyhedron(ieqs = [[-b[0], A[0,0], A[0,1]], [-b[1], A[1,0], A[1,1]], [-b[2], A[2,0], A[2,1]], [0, 1, 0], [0, 0, 1]])
pol.show()

will show you the polyhedron of this MILP in particular.

However, notice that this method has its drawbacks: For example, you should always write the inequalities as >= in order to make them correspond with the way Polyhedron(ieqs=...) draws. Another disadvantage is that the Polyhedron command doesn't take into account that your variables are nonnegative, so you should add those restrictions manually as I did in the last piece of code (note the [0, 1, 0], [0, 0, 1]).

If all you want is to draw the feasible polyhedron of a MILP, you can use the polyhedron method (yes, lowercase letters), like in the following example:

pol = LP.polyhedron()
pol.show()

This will work, no matter how you write your problem, or the inequalities, and you don't need to do any special for non-negative variables (this is already taken into account due to the command x = LP.new_variable(nonnegative=True)).

Hello, @Cyrille! I am not completely sure that I understood your question, but you could store constants in a matrix and a vector. For example, suppose you want to rewrite the following MILP:

LP = MixedIntegerLinearProgram(maximization=True)
x = LP.new_variable(nonnegative=True)
LP.add_constraint(-2*x[1] - x[2] >= 1.5)
LP.add_constraint(-x[1] - x[2] >= 1)
LP.add_constraint(-x[1] - 2*x[2] >= 1)
LP.set_objective(5*x[1] - 4*x[2])

Then you could define a matrix A containing all the coefficients for the variables, and a vector containing all the constant terms. In this particular case,

A = matrix(3, 2, [-2, -1, -1, -1, -1, -2])
b = vector([-1.5, -1, -1])

Now, you can rewrite the problem as you indicate in your question:

LP = MixedIntegerLinearProgram(maximization=True)
x = LP.new_variable(nonnegative=True)
LP.add_constraint(A[0,0]*x[1] + A[0,1]*x[2] >= b[0])
LP.add_constraint(A[1,0]*x[1] + A[1,1]*x[2] >= b[1])
LP.add_constraint(A[2,0]*x[1] + A[2,1]*x[2] >= b[2])
LP.set_objective(5*x[1] + 4*x[2])

And you even can reuse the numbers in the Polyhedron command. For example,

pol = Polyhedron(ieqs = [[-b[0], A[0,0], A[0,1]], [-b[1], A[1,0], A[1,1]], [-b[2], A[2,0], A[2,1]], [0, 1, 0], [0, 0, 1]])
pol.show()

will show you the polyhedron of this MILP in particular.

However, notice that this method has its drawbacks: For example, you should always write the inequalities as >= in order to make them correspond with the way Polyhedron(ieqs=...) draws. Another disadvantage is that the Polyhedron command doesn't take into account that your variables are nonnegative, so you should add those restrictions manually as I did in the last piece of code (note the [0, 1, 0], [0, 0, 1]).

If all you want is to draw the feasible polyhedron of a MILP, you can use the polyhedron method (yes, lowercase letters), like in the following example:

pol = LP.polyhedron()
pol.show()

This will work, no matter how you write your problem, or the inequalities, and you don't need to do any special for non-negative variables (this is already taken into account due to the command x = LP.new_variable(nonnegative=True)).

LP.new_variable(nonnegative=True)).