Class: Gecode::Constraints::IntEnum::Count::CountConstraint

Inherits:
ReifiableConstraint show all
Defined in:
lib/gecoder/interface/constraints/int_enum/count.rb

Overview

Describes a count constraint, which constrains the number of times a value (constant or a variable) may occurr in an enumeration of integer variable.

All relations available for SimpleRelationConstraint can be used with count constraints. Negation and reification is supported.

Examples

# Constrain +int_enum+ to not contain 0 exactly once.
int_enum.count(0).must_not == 1

# Constrain +int_enum+ to contain +x+ exactly +x_count+ times.
int_enum.count(x).must == x_count

# Reifies the constraint that +int_enum+ has +x+ zeros with the boolean
# variable +has_x_zeros+ and selects the strength +domain+.
int_enum.count(0).must.equal(x, :reify => has_x_zeros, 
  :strength => :domain)

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gecoder/interface/constraints/int_enum/count.rb', line 69

def post
  lhs, element, relation_type, rhs, reif_var = 
    @params.values_at(:lhs, :element, :relation_type, :rhs, :reif)
  
  # Bind variables if needed.
  element = element.bind if element.respond_to? :bind
  rhs = rhs.bind if rhs.respond_to? :bind
  
  # Post the constraint to gecode.
  if reif_var.nil?
    Gecode::Raw::count(@model.active_space, lhs.to_int_var_array, 
      element, relation_type, rhs, *propagation_options)
  else
    # We use a proxy int variable to get the reification.
    proxy = @model.int_var(rhs.min..rhs.max)
    rel = Gecode::Constraints::Util::RELATION_TYPES.invert[relation_type]
    proxy.must.send(rel, @params[:rhs], :reify => reif_var)
    Gecode::Raw::count(@model.active_space, lhs.to_int_var_array, 
      element, Gecode::Raw::IRT_EQ, proxy.bind, *propagation_options)
  end
end