Sha256: 0c94cc564ade4c2047cba4984e8e8e9977541437d78d73efcd04624e48489760

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

class Gecode::FreeIntVar
  # Initiates an arithmetic absolute value constraint.
  def abs
    Gecode::Constraints::Int::Arithmetic::AbsExpressionStub.new(@model, 
      :lhs => self)
  end
  
  # Creates a linear expression where the int variable is multiplied with 
  # a constant integer.
  alias_method :pre_arith_mult, :* if instance_methods.include? '*'
  def *(var)
    if var.kind_of? Gecode::FreeIntVar
      Gecode::Constraints::Int::Arithmetic::MultExpressionStub.new(
        @model, :lhs => self, :var => var)
    else
      pre_arith_mult(var) if respond_to? :pre_arith_mult
    end
  end
end

# A module that gathers the classes and modules used by arithmetic constraints.
module Gecode::Constraints::Int::Arithmetic 
  # Describes an expression stub started with an integer variable followed by 
  # #abs .
  class AbsExpressionStub < Gecode::Constraints::Int::CompositeStub
    def constrain_equal(variable, params, constrain)
      lhs, strength = @params.values_at(:lhs, :strength)
      if constrain
        bounds = [lhs.min.abs, lhs.max.abs]
        variable.must_be.in bounds.min..bounds.max
      end
      
      Gecode::Raw::abs(@model.active_space, lhs.bind, variable.bind, strength)
    end
  end
  
  # Describes an expression stub started with an integer variable followed by 
  # #* .
  class MultExpressionStub < Gecode::Constraints::Int::CompositeStub
    def constrain_equal(variable, params, constrain)
      lhs, lhs2, strength = @params.values_at(:lhs, :var, :strength)
      if constrain
        a_min = lhs.min; a_max = lhs.max
        b_min = lhs2.min; b_max = lhs2.max
        products = [a_min*b_min, a_min*b_max, a_max*b_min, a_max*b_max]
        variable.must_be.in products.min..products.max
      end

      Gecode::Raw::mult(@model.active_space, lhs.bind, lhs2.bind, 
        variable.bind, strength)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gecoder-0.6.1 lib/gecoder/interface/constraints/int/arithmetic.rb
gecoder-0.7.0 lib/gecoder/interface/constraints/int/arithmetic.rb