Closed neddimitrov closed 7 years ago
I added a cast to int before the comparison.
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.
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:
value(m.Y[e])
m.Y[e].value
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]
I've fixed both of those issues now -- following your last suggestion, just with a comparison and .value.
Great! Thanks for the nice example!
In the
convertYsToNetworkx
function, should you account for the fact the them.Y[e]
may not be exactly 1? E.g.,