Ask Your Question

Revision history [back]

If you read the documentation of p.new_variable method, you can see that there is an option indices:

  • "indices" -- (optional) an iterable of keys; components corresponding to these keys are created in order, and access to components with other keys will raise an error; otherwise components of this variable can be indexed by arbitrary keys and are created dynamically on access

Using this option:

p = MixedIntegerLinearProgram(maximization=False, solver="GLPK")
x = p.new_variable(real=True, nonnegative=True, name='x', indices=[0,1,2,3])
p.set_integer(x[2])
p.add_constraint(2*x[0] + 2*x[1] + 3*x[2] - 10*x[3] <= 0)
p.add_constraint(6*x[1] + 4*x[2] - 11*x[3] >= 0)
p.add_constraint(2*x[2] - 6*x[3] <= 0)
p.add_constraint(x[0] - x[1] - x[2] >= 0)
p.add_constraint(x[3] >= 1)
p.set_objective(3*x[1] + 6*x[2] - 3*x[3])

gives the desirable behavior:

sage: p.show()                                                                  
Minimization:
  3.0 x[1] + 6.0 x[2] -3.0 x[3] 

Constraints:
  2.0 x[0] + 2.0 x[1] + 3.0 x[2] - 10.0 x[3] <= 0.0
  -6.0 x[1] - 4.0 x[2] + 11.0 x[3] <= 0.0
  2.0 x[2] - 6.0 x[3] <= 0.0
  - x[0] + x[1] + x[2] <= 0.0
  - x[3] <= -1.0
Variables:
  x[0] = x_0 is a continuous variable (min=0.0, max=+oo)
  x[1] = x_1 is a continuous variable (min=0.0, max=+oo)
  x[2] = x_2 is an integer variable (min=0.0, max=+oo)
  x[3] = x_3 is a continuous variable (min=0.0, max=+oo)