Class: Gecode::Int::IntConstraintReceiver

Inherits:
ConstraintReceiver show all
Defined in:
lib/gecoder/interface/constraints/int_var_constraints.rb,
lib/gecoder/interface/constraints/int/domain.rb,
lib/gecoder/interface/constraints/int/channel.rb,
lib/gecoder/interface/constraints/int/relation.rb

Overview

IntConstraintReceiver contains all constraints that can be placed on an IntOperand.

Constraints are placed by calling IntOperand#must (or any other of the variations defined in Operand), which produces a IntConstraintReceiver from which the desired constraint can be used.

Each constraint accepts a number of options. See ConstraintReceiver for more information.

Examples

Constrains int_operand to be strictly greater than 5 using IntConstraintReceiver#>:

int_operand.must > 5

Constrains int_operand1 plus int_operand2 to be strictly greater than 5 using the IntOperand#+ property and IntConstraintReceiver#>:

(int_operand1 + int_operand2).must > 5

Constrains the maximum value of the integer operands in int_enum to not be in the range 3..7 using the IntEnumOperand#max property and IntConstraintReceiver#in:

int_enum.max.must_not_be.in 3..7

Constrains the integer operand at position int_operand in int_enum, an enumeration of integer operands, to be greater than or equal to int_operand2. This uses the IntEnumOperand#[] property and IntConstraintReceiver#>=:

int_enum[int_operand].must >= int_operand2

The same as above, but specifying that strength :domain should be used and that the constraint should be reified with bool_operand:

int_enum[int_operand].must_be.greater_or_equal(int_operand2, :strength => :domain, :reify => bool_operand)

Instance Method Summary collapse

Constructor Details

#initialize(model, params) ⇒ IntConstraintReceiver

Raises TypeError unless the left hand side is an int operand.



216
217
218
219
220
221
222
# File 'lib/gecoder/interface/constraints/int_var_constraints.rb', line 216

def initialize(model, params) #:nodoc:
  super

  unless params[:lhs].respond_to? :to_int_var
    raise TypeError, 'Must have int operand as left hand side.'
  end
end

Instance Method Details

#<(int_operand_or_fixnum, options = {}) ⇒ Object

Constrains the integer operand to be strictly less than int_operand_or_fixnum. #lesser and #lesser_than are aliases of this method.

Examples

# +int1+ must be strictly less than +int2+
int1.must < int2

# +int+ must be strictly less than 17
int.must < 17

# +int1+ must be strictly less than +int2+. We reify the
# constraint with +bool+ and select +domain+ as strength.
int1.must_be.less_than(int2, :reify => bool, :strength => :domain)


74
75
76
# File 'lib/gecoder/interface/constraints/int/relation.rb', line 74

def <(int_operand_or_fixnum, options = {})
  comparison(:<, int_operand_or_fixnum, options)
end

#<=(int_operand_or_fixnum, options = {}) ⇒ Object

Constrains the integer operand to be less than or equal to int_operand_or_fixnum. #less_or_equal and #less_than_or_equal_to are aliases of this method.

Examples

# +int1+ must be less than or equal to +int2+
int1.must <= int2

# +int+ must be less than or equal to 17
int.must <= 17

# +int1+ must be less than or equal to +int2+. We reify the
# constraint with +bool+ and select +domain+ as strength.
int1.must.less_or_equal(int2, :reify => bool, :strength => :domain)


93
94
95
# File 'lib/gecoder/interface/constraints/int/relation.rb', line 93

def <=(int_operand_or_fixnum, options = {})
  comparison(:<=, int_operand_or_fixnum, options)
end

#==(int_operand_or_fixnum, options = {}) ⇒ Object

Constrains the integer operand to equal int_operand_or_fixnum. #equal and #equal_to are aliases of this method.

Examples

# +int1+ must equal +int2+
int1.must == int2

# +int+ must equal 17
int.must == 17

# +int1+ must equal +int2+. We reify the constraint with 
# +bool+ and select +domain+ as strength.
int1.must.equal(int2, :reify => bool, :strength => :domain)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gecoder/interface/constraints/int/channel.rb', line 14

def ==(bool, options = {})
  unless @params[:lhs].respond_to? :to_int_var and 
      bool.respond_to? :to_bool_var
    return pre_channel_equals(bool, options)
  end
  
  if @params[:negate]
    raise Gecode::MissingConstraintError, 'A negated channel constraint ' +
      'is not implemented.'
  end
  unless options[:reify].nil?
    raise ArgumentError, 'Reification is not supported by the channel ' + 
      'constraint.'
  end
  
  @params.update(Gecode::Util.decode_options(options))
  @params[:rhs] = bool
  @model.add_constraint Channel::ChannelConstraint.new(@model, @params)
end

#>(int_operand_or_fixnum, options = {}) ⇒ Object

Constrains the integer operand to be strictly greater than int_operand_or_fixnum. #greater and #greater_than are aliases of this method.

Examples

# +int1+ must be strictly greater than +int2+
int1.must > int2

# +int+ must be strictly greater than 17
int.must > 17

# +int1+ must be strictly greater than +int2+. We reify the
# constraint with +bool+ and select +domain+ as strength.
int1.must_be.greater_than(int2, :reify => bool, :strength => :domain)


36
37
38
# File 'lib/gecoder/interface/constraints/int/relation.rb', line 36

def >(int_operand_or_fixnum, options = {})
  comparison(:>, int_operand_or_fixnum, options)
end

#>=(int_operand_or_fixnum, options = {}) ⇒ Object

Constrains the integer operand to be greater than or equal to int_operand_or_fixnum. #greater_or_equal and #greater_than_or_equal_to are aliases of this method.

Examples

# +int1+ must be greater than or equal to +int2+
int1.must >= int2

# +int+ must be greater than or equal to 17
int.must >= 17

# +int1+ must be greater than or equal to +int2+. We reify the
# constraint with +bool+ and select +domain+ as strength.
int1.must.greater_or_equal(int2, :reify => bool, :strength => :domain)


55
56
57
# File 'lib/gecoder/interface/constraints/int/relation.rb', line 55

def >=(int_operand_or_fixnum, options = {})
  comparison(:>=, int_operand_or_fixnum, options)
end

#in(domain, options = {}) ⇒ Object

Creates a domain constraint using the specified domain, specified as an enumeration of integers. The integer operand is constrained to take a value in the domain. Domains should be specified as ranges if possible.

Examples

# +x+ must be in the range 1..10
x.must_be.in 1..10

# +x+ must not be in the range -5...5
x.must_not_be.in -5...5

# Specifies the above, but reifies the constraint with the boolean 
# operand +bool+ and specified +value+ as strength.
x.must_not_be.in(-5...5, :reify => bool, :strength => :value)

# +x+ must be in the enumeration [3,5,7].
x.must_be.in [3,5,7]

# +x+ must not be in the enumeration [5,6,7,17].
x.must_not_be.in [5,6,7,17]

# Specifies the above, but reifies the constraint with the boolean 
# operand +bool+ and specified +value+ as strength.
x.must_not_be.in([5,6,7,17], :reify => bool, :strength => :value)


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gecoder/interface/constraints/int/domain.rb', line 30

def in(domain, options = {})
  @params.update(Gecode::Util.decode_options(options))
  @params[:domain] = domain
  if domain.kind_of? Range
    @model.add_constraint Domain::RangeDomainConstraint.new(@model, @params)
  elsif domain.kind_of?(Enumerable) and domain.all?{ |e| e.kind_of? Fixnum }
    @model.add_constraint Domain::EnumDomainConstraint.new(@model, 
      @params)
  else
    raise TypeError, "Expected integer enumerable, got #{domain.class}."
  end
end

#pre_channel_equalsObject



3
# File 'lib/gecoder/interface/constraints/int/channel.rb', line 3

alias_method :pre_channel_equals, :==