Module: Gecode::Constraints::Util

Defined in:
lib/gecoder/interface/constraints.rb

Overview

A module that provides some utility-methods for constraints.

Defined Under Namespace

Modules: Extensional

Constant Summary collapse

PROPAGATION_STRENGTHS =

Maps the name used in options to the value used in Gecode for propagation strengths.

{
  :default  => Gecode::Raw::ICL_DEF,
  :value    => Gecode::Raw::ICL_VAL,
  :bounds   => Gecode::Raw::ICL_BND,
  :domain   => Gecode::Raw::ICL_DOM
}
PROPAGATION_KINDS =

Maps the name used in options to the value used in Gecode for propagation kinds.

{
  :default  => Gecode::Raw::PK_DEF,
  :speed    => Gecode::Raw::PK_SPEED,
  :memory   => Gecode::Raw::PK_MEMORY,
}
RELATION_TYPES =

Maps the names of the methods to the corresponding integer relation type in Gecode.

{ 
  :== => Gecode::Raw::IRT_EQ,
  :<= => Gecode::Raw::IRT_LQ,
  :<  => Gecode::Raw::IRT_LE,
  :>= => Gecode::Raw::IRT_GQ,
  :>  => Gecode::Raw::IRT_GR
}
NEGATED_RELATION_TYPES =

The same as above, but negated.

{
  :== => Gecode::Raw::IRT_NQ,
  :<= => Gecode::Raw::IRT_GR,
  :<  => Gecode::Raw::IRT_GQ,
  :>= => Gecode::Raw::IRT_LE,
  :>  => Gecode::Raw::IRT_LQ
}
SET_RELATION_TYPES =

Maps the names of the methods to the corresponding set relation type in Gecode.

{ 
  :==         => Gecode::Raw::SRT_EQ,
  :superset   => Gecode::Raw::SRT_SUP,
  :subset     => Gecode::Raw::SRT_SUB,
  :disjoint   => Gecode::Raw::SRT_DISJ,
  :complement => Gecode::Raw::SRT_CMPL
}
NEGATED_SET_RELATION_TYPES =

The same as above, but negated.

{
  :== => Gecode::Raw::SRT_NQ
}
SET_OPERATION_TYPES =

Maps the names of the methods to the corresponding set operation type in Gecode.

{ 
  :union          => Gecode::Raw::SOT_UNION,
  :disjoint_union => Gecode::Raw::SOT_DUNION,
  :intersection   => Gecode::Raw::SOT_INTER,
  :minus          => Gecode::Raw::SOT_MINUS
}
COMPARISON_ALIASES =

Various method aliases for comparison methods. Maps the original (symbol) name to an array of aliases.

{ 
  :== => [:equal, :equal_to],
  :>  => [:greater, :greater_than],
  :>= => [:greater_or_equal, :greater_than_or_equal_to],
  :<  => [:less, :less_than],
  :<= => [:less_or_equal, :less_than_or_equal_to]
}
SET_ALIASES =
{ 
  :==         => [:equal, :equal_to],
  :superset   => [:superset_of],
  :subset     => [:subset_of],
  :disjoint   => [:disjoint_with],
  :complement => [:complement_of]
}

Class Method Summary collapse

Class Method Details

.constant_set?(expression) ⇒ Boolean

Checks whether the specified expression is regarded as a constant set. Returns true if it is, false otherwise.

Returns:

  • (Boolean)


201
202
203
204
205
206
207
208
209
# File 'lib/gecoder/interface/constraints.rb', line 201

def constant_set?(expression)
  return (
     expression.kind_of?(Range) &&        # It's a range.
     expression.first.kind_of?(Fixnum) &&
     expression.last.kind_of?(Fixnum)) ||
    expression.kind_of?(Fixnum) ||        # It's a single fixnum.
    (expression.kind_of?(Enumerable) &&   # It's an enum of fixnums.
     expression.all?{ |e| e.kind_of? Fixnum })
end

.constant_set_to_int_set(constant_set) ⇒ Object

Converts the different ways to specify constant sets in the interface to an instance of Gecode::Raw::IntSet. The different forms accepted are:

  • Single instance of Fixnum (singleton set).

  • Range (set containing all numbers in range), treated differently from other enumerations.

  • Enumeration of integers (set contaning all numbers in set).



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/gecoder/interface/constraints.rb', line 184

def constant_set_to_int_set(constant_set)
  unless constant_set?(constant_set)
    raise TypeError, "Expected a constant set, got: #{constant_set}."
  end
  
  if constant_set.kind_of? Range
    return Gecode::Raw::IntSet.new(constant_set.first, constant_set.last)
  elsif constant_set.kind_of? Fixnum
    return Gecode::Raw::IntSet.new([constant_set], 1)
  else
    constant_set = constant_set.to_a
    return Gecode::Raw::IntSet.new(constant_set, constant_set.size)
  end
end

.constant_set_to_params(constant_set) ⇒ Object

Converts the different ways to specify constant sets in the interface to the form that the set should be represented in Gecode (possibly multiple paramters. The different forms accepted are:

  • Single instance of Fixnum (singleton set).

  • Range (set containing all numbers in range), treated differently from other enumerations.

  • Enumeration of integers (set contaning all numbers in set).



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gecoder/interface/constraints.rb', line 163

def constant_set_to_params(constant_set)
  unless constant_set?(constant_set)
    raise TypeError, "Expected a constant set, got: #{constant_set}."
  end

  if constant_set.kind_of? Range
    return constant_set.first, constant_set.last
  elsif constant_set.kind_of? Fixnum
    return constant_set
  else
    constant_set = constant_set.to_a
    return Gecode::Raw::IntSet.new(constant_set, constant_set.size)
  end
end

.decode_options(options) ⇒ Object

Decodes the common options to constraints: strength, kind and reification. Returns a hash with up to three values. :strength is the strength that should be used for the constraint, :kind is the propagation kind that should be used, and :reif is the (bound) boolean variable that should be used for reification. The decoded options are removed from the hash (so in general the hash will be consumed in the process).

Raises ArgumentError if an unrecognized option is found in the specified hash. Or if an unrecognized strength is given. Raises TypeError if the reification variable is not a boolean variable.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gecoder/interface/constraints.rb', line 125

def decode_options(options)
  # Propagation strength.
  strength = options.delete(:strength) || :default
  unless PROPAGATION_STRENGTHS.include? strength
    raise ArgumentError, "Unrecognized propagation strength #{strength}."
  end
  
  # Propagation kind.
  kind = options.delete(:kind) || :default
  unless PROPAGATION_KINDS.include? kind
    raise ArgumentError, "Unrecognized propagation kind #{kind}."
  end  
                
  # Reification.
  reif_var = options.delete(:reify)
  unless reif_var.nil? or reif_var.kind_of? FreeBoolVar
    raise TypeError, 'Only boolean variables may be used for reification.'
  end
  
  # Check for unrecognized options.
  unless options.empty?
    raise ArgumentError, 'Unrecognized constraint option: ' + 
      options.keys.first.to_s
  end
  return {
    :strength => PROPAGATION_STRENGTHS[strength], 
    :kind => PROPAGATION_KINDS[kind],
    :reif => reif_var
  }
end

.extract_propagation_options(params) ⇒ Object

Extracts an array of the values selected for the standard propagation options (propagation strength and propagation kind) from the hash of parameters given. The options are returned in the order that they are given when posting constraints to Gecode.



215
216
217
# File 'lib/gecoder/interface/constraints.rb', line 215

def extract_propagation_options(params)
  params.values_at(:strength, :kind)
end