Module: Gecode

Defined in:
lib/gecoder/bindings.rb,
lib/gecoder/interface/mixin.rb,
lib/gecoder/interface/branch.rb,
lib/gecoder/interface/search.rb,
lib/gecoder/interface/variables.rb,
lib/gecoder/interface/constraints.rb,
lib/gecoder/interface/convenience.rb,
lib/gecoder/interface/enum_wrapper.rb,
lib/gecoder/interface/constraints/reifiable_constraints.rb

Overview

Problems can be formulated and solved either through defining a new class that mixes in Gecode::Mixin or by using Gecode.solve et al. Gecode::Mixin describes how to formulate problems.

Examples

The following two examples show how to solve the following equation system, using both ways to define and solve a problem.

Equation system:

x + y = z
x = y - 3
0 <= x,y,z <= 9

Mixing in Gecode::Mixin

class EquationProblem 
  include Gecode::Mixin

  def initialize
    variables_is_an int_var_array(3, 0..9)
    x, y, z = variables

    (x + y).must == z
    x.must == y - 3

    branch_on variables
  end
end
puts EquationProblem.new.solve!.variables.join(' ')

Using Gecode.solve

solution = Gecode.solve do
  variables_is_an int_var_array(3, 0..9)
  x, y, z = variables

  (x + y).must == z
  x.must == y - 3

  branch_on variables
end
puts solution.variables.values.join(' ')

Defined Under Namespace

Modules: Bool, BoolEnum, BoolEnumMethods, EnumMethods, FixnumEnum, FixnumEnumMethods, Int, IntEnum, IntEnumMethods, LoggingLayer, Mixin, Operand, SelectedSet, Set, SetElements, SetEnum, SetEnumMethods, Util, VariableEnumMethods Classes: BlockConstraint, BoolVar, Constraint, ConstraintReceiver, EnumerableView, FreeVarBase, IntVar, MissingConstraintError, Model, NoSolutionError, ReifiableConstraint, SetVar

Constant Summary collapse

Raw =

We just make Gecode::Raw an alias of the real module.

::GecodeRaw

Class Method Summary collapse

Class Method Details

.FreeVar(bound_class, space_bind_method) ⇒ Object

Creates a class for a free variable that can be bound into the specified class using the specified method in a space.



39
40
41
42
43
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/variables.rb', line 39

def Gecode::FreeVar(bound_class, space_bind_method) #:nodoc:
  clazz = Class.new(FreeVarBase)
  clazz.class_eval <<-"end_method_definitions"      
    # Binds the variable to the currently active space of the model, 
    # returning the bound variable.
    def bind
      active_space.method(:#{space_bind_method}).call(@index)
    end
    
    private
    
    # Delegates the method with the specified name to a method with the 
    # specified name when the variable is bound. If the bound method's name
    # is nil then the same name as the new method's name is assumed.
    def self.delegate(method_name, bound_method_name = nil)
      bound_method_name = method_name if bound_method_name.nil?
      module_eval <<-"end_code"
        def \#{method_name}(*args)
          @model.allow_space_access do
            bind.method(:\#{bound_method_name}).call(*args)
          end
        end
      end_code
    end
  end_method_definitions
  return clazz
end

.load_bindings_libObject

Loads the binding libraries. This is done as a method in order to be easier to test.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gecoder/bindings.rb', line 48

def self.load_bindings_lib #:nodoc:
  # Workaround to get the precompiled DLLs into the DLL search path on 
  # Windows.
  dll_dir = File.dirname(__FILE__) + '/../../vendor/gecode/win32/lib'
  if RUBY_PLATFORM =~ /mswin/ and File.exists? dll_dir
    # Switch directory while loading libraries so that the DLLs are in the 
    # work directory.
    require 'fileutils'
    FileUtils.cd dll_dir do
      require 'gecode'
    end
  else
    require 'gecode'
  end
end

.maximize(variable_to_maximize, &block) ⇒ Object

Provides a convenient way to construct a model and then find the solution that maximizes a given variable. The model constructed uses the specified block as initialization method. The solution that maximizes the specified variable is then returned.

For instance

solution = Gecode.maximize :variable_bar do
  # Do something
end

is equivalent to

class Foo 
  include Gecode::Mixin

  def initialize
    # Do something
  end
end
solution = Foo.new.maximize :variable_bar


55
56
57
# File 'lib/gecoder/interface/convenience.rb', line 55

def self.maximize(variable_to_maximize, &block)
  create_model(&block).maximize! variable_to_maximize
end

.minimize(variable_to_minimize, &block) ⇒ Object

Provides a convenient way to construct a model and then find the solution that minimizes a given variable. The model constructed uses the specified block as initialization method. The solution that minimizes the specified variable is then returned.

For instance

solution = Gecode.minimize :variable_bar do
  # Do something
end

is equivalent to

class Foo
  include Gecode::Mixin

  def initialize
    # Do something
  end
end
solution = Foo.new.minimize :variable_bar


80
81
82
# File 'lib/gecoder/interface/convenience.rb', line 80

def self.minimize(variable_to_minimize, &block)
  create_model(&block).minimize! variable_to_minimize
end

.solve(&block) ⇒ Object

Provides a convenient way to construct a model and then find a solution. The model constructed uses the specified block as initialization method. The first solution to the model is then returned.

For instance

solution = Gecode.solve do
  # Do something
end

is equivalent to

class Foo 
  include Gecode::Mixin

  def initialize
    # Do something
  end
end
solution = Foo.new.solve!


30
31
32
# File 'lib/gecoder/interface/convenience.rb', line 30

def self.solve(&block)
  create_model(&block).solve!
end