module Gecode::Set::SetOperand

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 {0, 1, 2}, inside a problem formulation, using Gecode::Mixin#set_var:

set_operand = set_var(0, 0..2)

Uses the #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]

Public Instance Methods

disjoint_union(set_operand_or_constant_set) click to toggle source

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]
# 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() click to toggle source

Produces a SetElementsOperand on which relation constraints can be placed that constrain all elements in the set.

Examples ¶ ↑

# The elements of +set+.
set.elements
# 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) click to toggle source

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]
# 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() click to toggle source

Produces an IntOperand representing the maximum of the set.

Examples ¶ ↑

# The maximum of +set+.
set.max
# File lib/gecoder/interface/constraints/set/connection.rb, line 19
def max
Connection::SetMaxOperand.new(@model, self)
end
min() click to toggle source

Produces an IntOperand representing the minimum of the set.

Examples ¶ ↑

# The minimum of +set+.
set.min
# File lib/gecoder/interface/constraints/set/connection.rb, line 9
def min
Connection::SetMinOperand.new(@model, self)
end
minus(set_operand_or_constant_set) click to toggle source

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]
# 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() click to toggle source

Produces an IntOperand representing the size of the set.

Examples ¶ ↑

# The size of +set+.
set.size
# 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)}) click to toggle source

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})
# File lib/gecoder/interface/constraints/set/connection.rb, line 44
def sum(options = {:weights => weights = Hash.new(1)})
if options.empty? or options.keys.size > 1
raise ArgumentError, 'At most one of the options :weights and ' +
':substitutions may be specified.'
end

case options.keys.first
when :substitutions
subs = options[:substitutions]
when :weights
weights = options[: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 #{options.keys.first}."
end
Connection::SetSumOperand.new(@model, self, subs)
end
union(set_operand_or_constant_set) click to toggle source

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]
# 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