Pyomo / PyomoGallery

A collection of Pyomo examples
Other
287 stars 160 forks source link

Row generation MST example #6

Closed neddimitrov closed 7 years ago

ghackebeil commented 7 years ago

In the convertYsToNetworkx function, should you account for the fact the the m.Y[e] may not be exactly 1? E.g.,

edges = [e for e in self.m.edge_set if self.m.Y[e] >= 0.5]
neddimitrov commented 7 years ago

I added a cast to int before the comparison.

ghackebeil commented 7 years ago

Casting directly to int does not fix the problem. Note that a MIP solver might return a value of 0.9999999 for a binary variable. Casting this to int, you get

 int(0.9999999) = 0

The function you really want is round, which confusingly returns a floating point number, but one that is exactly equal to the nearest integer.

ghackebeil commented 7 years ago

There is another issue that I overlooked the first time. In your comparison to 1, you are using the variable object directly, but you need to be using the value of the variable. An expression like m.Y[e] == 1 actually generates a Pyomo expression (because this is how we define constraints). Also, a strange side effect of being able to support double-sided inequality constraints is that casting a Pyomo relational expression to bool does NOT give the behavior one might expect. For example,

>>> model = ConcreteModel()
>>> model.x = Var()
>>> model.x.value = 0.0
>>> bool(model.x >= 1)
True
>>> bool(model.x.value >= 1)
False

So when you do the comparison, you need to extract the value from m.Y[e]. This can be done in three ways:

  1. value(m.Y[e])
  2. m.Y[e].value
  3. m.Y[e]()

Combining this with my previous comment, I think the comparison expression will probably look like:

edges = [e for e in self.m.edge_set if round(self.m.Y[e].value) == 1.0]

or equivalently

edges = [e for e in self.m.edge_set if self.m.Y[e].value >= 0.5]
neddimitrov commented 7 years ago

I've fixed both of those issues now -- following your last suggestion, just with a comparison and .value.

ghackebeil commented 7 years ago

Great! Thanks for the nice example!