Ask Your Question
0

p.show() at MixedIntegerLinearProgram results

asked 2012-05-19 17:00:54 +0200

koukourikos gravatar image

updated 2012-05-19 20:56:28 +0200

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.add_constraint(-w[0]+w[2]+2*w[3]==4)
p.add_constraint(w[0]+w[1]+w[3]==3)
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 flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
2

answered 2012-05-20 04:18:59 +0200

Nathann gravatar image

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.add_constraint(-w[0]+2*w[2]+3*w[3]==4)
sage: p.add_constraint(w[0]+w[1]+w[3]==3)
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...

edit flag offensive delete link more

Comments

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

koukourikos gravatar imagekoukourikos ( 2012-05-20 07:43:34 +0200 )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.

kcrisman gravatar imagekcrisman ( 2012-05-21 10:29:33 +0200 )edit
0

answered 2012-05-19 23:43:07 +0200

kcrisman gravatar image

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.add_constraint(w[0]+w[1]+w[3]==3)
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.

edit flag offensive delete link more

Comments

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.

koukourikos gravatar imagekoukourikos ( 2012-05-20 07:47:27 +0200 )edit
0

answered 2012-05-19 23:38:50 +0200

Volker Braun gravatar image

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].

edit flag offensive delete link more

Comments

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.

kcrisman gravatar imagekcrisman ( 2012-05-19 23:43:36 +0200 )edit

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

koukourikos gravatar imagekoukourikos ( 2012-05-20 07:49:23 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2012-05-19 17:00:54 +0200

Seen: 385 times

Last updated: May 20 '12