module Gecode
Problems can be formulated and solved either through defining a new class that mixes in Gecode::Mixin or by using ::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 ::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(' ')
Constants
- BoolVar
- IntVar
- Raw
We just make Gecode::Raw an alias of the real module.
- SetVar
Public Class Methods
Creates a class for a free variable that can be bound into the specified class using the specified method in a space.
# File doc/tmp/rdoc_dev/gecoder/interface/variables.rb, line 39 def Gecode::FreeVar(bound_class, space_bind_method) clazz = Class.new(FreeVarBase) clazz.class_eval " # 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 " return clazz end
Loads the binding libraries. This is done as a method in order to be easier to test.
# File doc/tmp/rdoc_dev/gecoder/bindings.rb, line 48 def self.load_bindings_lib # 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
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
# File doc/tmp/rdoc_dev/gecoder/interface/convenience.rb, line 55 def self.maximize(variable_to_maximize, &block) create_model(&block).maximize! variable_to_maximize end
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
# File doc/tmp/rdoc_dev/gecoder/interface/convenience.rb, line 80 def self.minimize(variable_to_minimize, &block) create_model(&block).minimize! variable_to_minimize end
# File doc/tmp/rdoc_dev/gecoder/interface/convenience.rb, line 93 def initialize(&init_block) instance_eval &init_block end
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!
# File doc/tmp/rdoc_dev/gecoder/interface/convenience.rb, line 30 def self.solve(&block) create_model(&block).solve! end