module Gecode::Util
A module that provides some utility-methods for constraints.
Constants
- COMPARISON_ALIASES
Various method aliases for comparison methods. Maps the original (symbol) name to an array of aliases.
- NEGATED_RELATION_TYPES
The same as above, but negated.
- NEGATED_SET_RELATION_TYPES
The same as above, but negated.
- PROPAGATION_KINDS
Maps the name used in options to the value used in Gecode for propagation kinds.
- PROPAGATION_STRENGTHS
Maps the name used in options to the value used in Gecode for propagation strengths.
- RELATION_TYPES
Maps the names of the methods to the corresponding integer relation type in Gecode.
- SET_ALIASES
- SET_OPERATION_TYPES
Maps the names of the methods to the corresponding set operation type in Gecode.
- SET_RELATION_TYPES
Maps the names of the methods to the corresponding set relation type in Gecode.
Public Instance Methods
Checks whether the specified expression is regarded as a constant set. Returns true if it is, false otherwise.
# File doc/tmp/rdoc_dev/gecoder/interface/constraints.rb, line 415 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
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).
# File doc/tmp/rdoc_dev/gecoder/interface/constraints.rb, line 398 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
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).
# File doc/tmp/rdoc_dev/gecoder/interface/constraints.rb, line 377 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
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 operand 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 operand is not a boolean operand.
# File doc/tmp/rdoc_dev/gecoder/interface/constraints.rb, line 339 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.respond_to? :to_bool_var raise TypeError, 'Only boolean operands 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
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.
# File doc/tmp/rdoc_dev/gecoder/interface/constraints.rb, line 429 def extract_propagation_options(params) params.values_at(:strength, :kind) end