Class: Gecode::Constraints::Bool::BooleanConstraint

Inherits:
ReifiableConstraint show all
Defined in:
lib/gecoder/interface/constraints/bool/boolean.rb

Overview

Describes a constraint on a boolean expression.

Boolean expressions

A boolean expression consists of several boolean variable with various boolean operators. The available operators are:

|

Or

&

And

^

Exclusive or

implies

Implication

Examples

# +b1+ and +b2+
b1 & b2

# (+b1+ and +b2+) or +b3+ 
(b1 & b1) | b3

# (+b1+ and +b2+) or (+b3+ exclusive or +b1+)
(b1 & b2) | (b3 ^ b1)

# (+b1+ implies +b2+) and (+b3+ implies +b2+)
(b1.implies b2) & (b3.implies b2)

Domain

A domain constraint just specifies that a boolean expression must be true or false. Negation and reification are supported.

Examples

# +b1+ and +b2+ must be true.
(b1 & b2).must_be.true

# (+b1+ implies +b2+) and (+b3+ implies +b2+) must be false.
((b1.implies b2) & (b3.implies b2)).must_be.false

# +b1+ and +b2+ must be true. We reify it with +bool+ and select the
# strength +domain+.
(b1 & b2).must_be.true(:reify => bool, :strength => :domain)

Equality

A constraint with equality specifies that two boolean expressions must be equal. Negation and reification are supported. Any of ==, equal and equal_to may be used for equality.

Examples

# +b1+ and +b2+ must equal +b1+ or +b2+.
(b1 & b2).must == (b1 | b2)

# +b1+ and +b2+ must not equal +b3+. We reify it with +bool+ and select 
# the strength +domain+.
(b1 & b2).must_not.equal(b3, :reify => bool, :select => :domain)

Implication

A constraint using imply specified that one boolean expression must imply the other. Negation and reification are supported.

Examples

# +b1+ must imply +b2+
b1.must.imply b2

# +b1+ and +b2+ must not imply +b3+. We reify it with +bool+ and select
# +domain+ as strength.
(b1 & b2).must_not.imply b3

Instance Method Summary collapse

Methods inherited from ReifiableConstraint

#&, #reification_var, #reification_var=, #|

Methods inherited from Constraint

#initialize

Constructor Details

This class inherits a constructor from Gecode::Constraints::Constraint

Instance Method Details

#postObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/gecoder/interface/constraints/bool/boolean.rb', line 134

def post
  lhs, rhs, negate, reif_var = 
    @params.values_at(:lhs, :rhs, :negate, :reif)
  space = (lhs.model || rhs.model).active_space

  if lhs.kind_of?(Gecode::FreeBoolVar)
    lhs = Constraints::Bool::ExpressionNode.new(lhs, @model)
  end

  bot_eqv = Gecode::Raw::IRT_EQ
  bot_xor = Gecode::Raw::IRT_NQ

  if rhs.respond_to? :to_minimodel_bool_expr
    if reif_var.nil?
      tree = ExpressionTree.new(lhs, 
        Gecode::Raw::MiniModel::BoolExpr::NT_EQV, rhs)
      tree.to_minimodel_bool_expr.post(space, !negate, 
        *propagation_options)
    else
      tree = ExpressionTree.new(lhs, 
        Gecode::Raw::MiniModel::BoolExpr::NT_EQV, rhs)
      var = tree.to_minimodel_bool_expr.post(space, *propagation_options)
      Gecode::Raw::rel(space, var, (negate ? bot_xor : bot_eqv),
        reif_var.bind, *propagation_options)
    end
  else
    should_hold = !negate & rhs
    if reif_var.nil?
      lhs.to_minimodel_bool_expr.post(space, should_hold, 
        *propagation_options)
    else
      var = lhs.to_minimodel_bool_expr.post(space, *propagation_options)
      Gecode::Raw::rel(space, var, 
        (should_hold ? bot_eqv : bot_xor),
        reif_var.bind, *propagation_options)
    end
  end
end