Module: Gecode::Set::SetOperand
- Includes:
- Operand
- Defined in:
- lib/gecoder/interface/constraints/set_var_constraints.rb,
lib/gecoder/interface/constraints/set/operation.rb,
lib/gecoder/interface/constraints/set/connection.rb,
lib/gecoder/interface/constraints/set/cardinality.rb,
lib/gecoder/interface/constraints/set_elements_constraints.rb
Overview
A SetOperand is a combination of operands on which the constraints defined in SetConstraintReceiver can be placed.
Set operands can be created either by using Gecode::Mixin#set_var et al, or by using properties that produce set operands. The operands, no matter how they were created, all respond to the properties defined by SetOperand.
Examples
Produces a single set operand (more specifically a SetVar), with greatest lower bound 0 and least upper bound 1, 2, inside a problem formulation, using Gecode::Mixin#set_var:
set_operand = set_var(0, 0..2)
Uses the SetOperand#union property to produce a new set operand representing the union between set_operand1
and set_operand2
:
new_set_operand = set_operand1.union(set_operand2)
Uses the SetEnumOperand#union property to produce a new set operand representing the union of the set operands in the enumeration set_enum
:
new_set_operand = set_enum.union
Uses the SetEnumOperand#[] property to produce a new set operand representing the set operand at the index decided by int_operand
(which can change during search) in the enumeration set_enum
:
new_set_operand = set_enum[int_operand]
– Classes that mix in SetOperand must define #model and #to_set_var .
Instance Method Summary collapse
-
#disjoint_union(set_operand_or_constant_set) ⇒ Object
Produces a new SetOperand representing the disjoint union between this operand and
set_operand_or_constant_set
. -
#elements ⇒ Object
Produces a SetElementsOperand on which relation constraints can be placed that constrain all elements in the set.
-
#intersection(set_operand_or_constant_set) ⇒ Object
Produces a new SetOperand representing the intersection between this operand and
set_operand_or_constant_set
. -
#max ⇒ Object
Produces an IntOperand representing the maximum of the set.
-
#method_missing(method, *args) ⇒ Object
:nodoc:.
-
#min ⇒ Object
Produces an IntOperand representing the minimum of the set.
-
#minus(set_operand_or_constant_set) ⇒ Object
Produces a new SetOperand representing this operand minus
set_operand_or_constant_set
. -
#size ⇒ Object
Produces an IntOperand representing the size of the set.
-
#sum(options = {:weights => weights = Hash.new(1)}) ⇒ Object
Produces an IntOperand representing the sum of the values in the set.
-
#union(set_operand_or_constant_set) ⇒ Object
Produces a new SetOperand representing the union between this operand and
set_operand_or_constant_set
.
Methods included from Operand
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
:nodoc:
43 44 45 46 47 48 49 50 |
# File 'lib/gecoder/interface/constraints/set_var_constraints.rb', line 43 def method_missing(method, *args) #:nodoc: if Gecode::SetVar.instance_methods.include? method.to_s # Delegate to the set var. to_set_var.method(method).call(*args) else super end end |
Instance Method Details
#disjoint_union(set_operand_or_constant_set) ⇒ Object
Produces a new SetOperand representing the disjoint union between this operand and set_operand_or_constant_set
. The disjoint union is the union of the disjoint parts of the sets.
Examples
# The disjoint union between +set1+ and +set2+.
set1.disjoint_union set2
# The disjoint union between +set+ and {1, 3, 5}.
set.disjoint_union [1,3,5]
28 29 30 |
# File 'lib/gecoder/interface/constraints/set/operation.rb', line 28 def disjoint_union(set_operand_or_constant_set) set_operation(:disjoint_union, set_operand_or_constant_set) end |
#elements ⇒ Object
Produces a SetElementsOperand on which relation constraints can be placed that constrain all elements in the set.
Examples
# The elements of +set+.
set.elements
91 92 93 |
# File 'lib/gecoder/interface/constraints/set_elements_constraints.rb', line 91 def elements Gecode::SetElements::SetElementsOperand.new(self) end |
#intersection(set_operand_or_constant_set) ⇒ Object
Produces a new SetOperand representing the intersection between this operand and set_operand_or_constant_set
.
Examples
# The intersection between +set1+ and +set2+.
set1.intersection set2
# The intersection between +set+ and {1, 3, 5}.
set.intersection [1,3,5]
42 43 44 |
# File 'lib/gecoder/interface/constraints/set/operation.rb', line 42 def intersection(set_operand_or_constant_set) set_operation(:intersection, set_operand_or_constant_set) end |
#max ⇒ Object
Produces an IntOperand representing the maximum of the set.
Examples
# The maximum of +set+.
set.max
19 20 21 |
# File 'lib/gecoder/interface/constraints/set/connection.rb', line 19 def max Connection::SetMaxOperand.new(@model, self) end |
#min ⇒ Object
Produces an IntOperand representing the minimum of the set.
Examples
# The minimum of +set+.
set.min
9 10 11 |
# File 'lib/gecoder/interface/constraints/set/connection.rb', line 9 def min Connection::SetMinOperand.new(@model, self) end |
#minus(set_operand_or_constant_set) ⇒ Object
Produces a new SetOperand representing this operand minus set_operand_or_constant_set
.
Examples
# +set1+ minus +set2+.
set1.minus set2
# +set+ minus {1, 3, 5}.
set.minus [1,3,5]
56 57 58 |
# File 'lib/gecoder/interface/constraints/set/operation.rb', line 56 def minus(set_operand_or_constant_set) set_operation(:minus, set_operand_or_constant_set) end |
#size ⇒ Object
Produces an IntOperand representing the size of the set.
Examples
# The size of +set+.
set.size
9 10 11 |
# File 'lib/gecoder/interface/constraints/set/cardinality.rb', line 9 def size Cardinality::SetSizeOperand.new(@model, self) end |
#sum(options = {:weights => weights = Hash.new(1)}) ⇒ Object
Produces an IntOperand representing the sum of the values in the set. One of the following options may also be given:
- :weights
-
Produces the weighted sum using the specified hash of weights. The hash should map each value to that value’s weight.
- :substitutions
-
Produces the sum of the set with all elements replaced according to the hash.
Elements not included in the weights or substitutions hash are removed from the upper bound of the set.
Examples
# The sum of +set+.
set.sum
# The sum of +set+ with primes < 10 given twice the weight.
set.sum(:weights => {2 => 2, 3 => 2, 5 => 2, 7 => 2})
# The sum of +set+ with odd values in [1,6] being counted as 1.
set.sum(:substitutions => {1 => 1, 3 => 1, 5 => 1})
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gecoder/interface/constraints/set/connection.rb', line 44 def sum( = {:weights => weights = Hash.new(1)}) if .empty? or .keys.size > 1 raise ArgumentError, 'At most one of the options :weights and ' + ':substitutions may be specified.' end case .keys.first when :substitutions subs = [:substitutions] when :weights weights = [:weights] subs = Hash.new do |hash, key| if weights[key].nil? hash[key] = nil else hash[key] = key * weights[key] end end else raise ArgumentError, "Unrecognized option #{.keys.first}." end Connection::SetSumOperand.new(@model, self, subs) end |
#union(set_operand_or_constant_set) ⇒ Object
Produces a new SetOperand representing the union between this operand and set_operand_or_constant_set
.
Examples
# The union between +set1+ and +set2+.
set1.union set2
# The union between +set+ and {1, 3, 5}.
set.union [1,3,5]
13 14 15 |
# File 'lib/gecoder/interface/constraints/set/operation.rb', line 13 def union(set_operand_or_constant_set) set_operation(:union, set_operand_or_constant_set) end |