Class: Gecode::Constraints::IntEnum::Sort::SortConstraint

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

Overview

Describes a sort constraint which constrains an enumeration of integer variables to be sorted. Supports reification and negation.

Example

# Constrains the variables in +int_enum+ to be sorted ascendingly.
int_enum.must_be.sorted

# Reifies the constraint that the variables in +int_enum+ to be sorted 
# ascendingly with the boolean variable +is_sorted+, while selecting 
# +domain+ as strength.
int_enum.must_be.sorted(:reify => :is_sorted, :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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gecoder/interface/constraints/int_enum/sort.rb', line 107

def post
  lhs, strength, kind, reif_var = 
    @params.values_at(:lhs, :strength, :kind, :reif)
  using_reification = !reif_var.nil?
  
  # We translate the constraint into n-1 relation constraints.
  options = {
    :strength => 
      Gecode::Constraints::Util::PROPAGATION_STRENGTHS.invert[strength],
    :kind => 
      Gecode::Constraints::Util::PROPAGATION_KINDS.invert[kind]
  }
  if using_reification
    reification_variables = @model.bool_var_array(lhs.size - 1)
  end
  (lhs.size - 1).times do |i|
    first, second = lhs[i, 2]
    rel_options = options.clone
    if using_reification
      # Reify each relation constraint and then bind them all together.
      rel_options[:reify] = reification_variables[i]
    end
    first.must_be.less_than_or_equal_to(second, rel_options)
  end
  if using_reification
    reification_variables.conjunction.must == reif_var
  end
end