Gecode/R is a Ruby interface to Gecode, an open source library for constraint programming. The interface is intended for people with no previous experience of constraint programming, aiming to be easy to pick up and use in practice.
Constraint programming is a technique where you just describe the problem and then let the solver find solutions for you.
Solve the equation system x + y = z, x = y - 3, 0 <= x,y,z <= 9.
class ExampleProblem < Gecode::Model attr :vars def initialize # Set up the variables, three integers with domain 0..9. x,y,z = @vars = int_var_array(3, 0..9) # Describe the problem (in this case the equations). (x + y).must == z x.must == y - 3 # Tell it what the variables of the problem are. branch_on @vars end end
To find a solution we just have to ask.
puts 'x y z' puts ExampleProblem.new.solve!.vars.values.join(' ')
x y z 0 3 3
The equation system is no harder to solve than to describe. But many problems are simpler to describe than to solve, causing constraint programming to be an easy way of solving them. Sudoku is one of many examples.