p.show() at MixedIntegerLinearProgram results

I would appreciate it if someone could explain me how the p.show() method works (p=MixedIntegerLinearProgram)... More precisely : lets say we have the following Linear Program:

p=MixedIntegerLinearProgram()
w=p.new_variable()
p.set_objective(w[0]- w[1]) #<---- Look here


the result from p.show() is

Maximization:
x_0 -x_3   #<----and here
Constraints:
4.0 <= -x_0 +x_1 +2.0 x_2 <= 4.0
3.0 <= x_0 +x_2 +x_3 <= 3.0
Variables:
x_0 is a continuous variable (min=0.0, max=+oo)
x_1 is a continuous variable (min=0.0, max=+oo)
x_2 is a continuous variable (min=0.0, max=+oo)
x_3 is a continuous variable (min=0.0, max=+oo)


why SAGE shows a different objective function?

edit retag close merge delete

Sort by » oldest newest most voted

Helloooo everybody !!

Indeed, they are numbered this way just because of the order in which they are created. And there is no specific reason why they are called x. For the code you give as an example, I would not know from the inside of objects p or w that the value is stored in a variable which is named 'w'. Hence I do not know how to make this appear in p.show(). Actually, there is in Sage a way to "name" variables :

sage: p=MixedIntegerLinearProgram()
sage: w=p.new_variable(name="w")
sage: p.set_objective(w[0]- w[1])
sage: p.show()
Maximization:
w[0] -w[1]
Constraints:
R0: 4.0 <= -w[0] +2.0 w[2] +3.0 w[3] <= 4.0
R1: 3.0 <= w[0] +w[3] +w[1] <= 3.0
Variables:
w[0] is a continuous variable (min=0.0, max=+oo)
w[2] is a continuous variable (min=0.0, max=+oo)
w[3] is a continuous variable (min=0.0, max=+oo)
w[1] is a continuous variable (min=0.0, max=+oo)


But there you had to explicitely say that the variables had to be named by "w". Well. Now, this changes nothing to their numbering, and I have no idea how to fix that. In the part of the code that displays the formulas, the keys you used to access the variables have been forgotten a loooong time ago. I agree that the result of p.show() would be muc easier to read though...

more

Thank you very much! your trick with the naming made the results to have more sense and the indexing is also correct!

( 2012-05-20 00:43:34 -0500 )edit
1

@Nathann - what do you think about the suggestion in koukourikos' comments in the other two answers to make this default somehow? I don't know if it is syntactically possible to "grab" the name or not.

( 2012-05-21 03:29:33 -0500 )edit

Apparently Sage renames the variables in the order you actually provided them! In this example it's easier to see because I've given a different coefficient to w[2] and w[3].

sage: p=MixedIntegerLinearProgram()
sage: w=p.new_variable()
sage: p.add_constraint(-w[0]+2*w[2]+3*w[3]==4)  # - for the first, 2 for the second, 3 for the third
sage: p.set_objective(w[0]- w[1])  # so if w[1] is the last variable to arrive, it will be x_3
sage: p.show()
Maximization:
x_0 -x_3  # and indeed it is
Constraints:
4.0 <= -x_0 +2.0 x_1 +3.0 x_2 <= 4.0
3.0 <= x_0 +x_2 +x_3 <= 3.0
Variables:
x_0 is a continuous variable (min=0.0, max=+oo)
x_1 is a continuous variable (min=0.0, max=+oo)
x_2 is a continuous variable (min=0.0, max=+oo)
x_3 is a continuous variable (min=0.0, max=+oo)


Now maybe that's a bug, or at least needs to be documented better.

more

yes you are right, it has to do with the order. I don't think that this is a bug but I agree that an explanation in the documentation about how the indexing works would be useful.

( 2012-05-20 00:47:27 -0500 )edit

The variables are such that w[3] == x_1. From the looks of it, a new x_i is generated the first time you access w[j].

more

Which leads to the question of why they are called x, and the numbers. Sorry for not reloading before you posted - amazing how this often seems to happen.

( 2012-05-19 16:43:36 -0500 )edit

I think Nathann's solution solves both issues. Maybe the "name" option should be used by default.

( 2012-05-20 00:49:23 -0500 )edit