title: Evaluate constraint expression as boolean
body: <p>I want to evaluate if a constraint is respected or not in Pyomo when the values of the variables contained in constraint expression are known.</p>
  
  <p>Use case: We know that one particular constraint sometimes makes the problem infeasible, depending on the value of the variable. Instead of sending the problem to the solver to test if the problem is feasible, converting the constraint expression to a boolean type would be enough to determine if the constraint is the culprit.</p>
  
  <p>For the sake of providing a feasible example, here would be the code:</p>
  
  <pre><code>from pyomo.environ import ConcreteModel, Var, Constraint
  model = ConcreteModel()
  model.X = Var()
  
  def constraint_rule(model):
      return model.X &lt;= 1
  model.a_constraint = Constraint(rule=constraint_rule)
  </code></pre>
  
  <p>Now, let's try to work with the expression to evaluate:</p>
  
  <pre><code># Let's define the expression in this way:
  expression = constraint_rule(model)
  
  # Let's show that the expression is what we expected:
  print(str(expression))
  </code></pre>
  
  <p>The previous statement should print <code>X &lt;= 1.0</code>. </p>
  
  <p>Now, the tricky part is how to evaluate the expression. </p>
  
  <pre><code>if expression == True:
      print("feasible")
  else:
      print("infeasible")
  </code></pre>
  
  <p>creates an TypeError Exception (<code>TypeError: Cannot create an EqualityExpression where one of the sub-expressions is a relational expression: X &lt;= 1.0</code>).</p>
  
  <p>The last example doesn't work because <code>constraint_rule</code> doesn't return a boolean but a Pyomo expression.</p>
  
  <p>Finally, I know that something like</p>
  
  <pre><code>def evaluate_constraint_a_expression(model):
      return value(model.X) &lt;= 1
  </code></pre>
  
  <p>would work, but I can't assume that I will always know the content of my constraint expression, so I need a robust way of evaluating it. </p>
  
  <p>Is there a clever way of achieving this? Like, evaluating the expression as a boolean and evaluating the left hand side and right hand side of the expression at the same time?</p>