Constraint Processing

I had to give this a couple tries before I just broke down and went
for what amounts to brute force. This solution can get slow pretty
quickly; I’m sure there are some easy speedups (i.e. narrow the search
space) that can be done, but I figured to put this up for now.


class Integer
def even?
(self % 2).zero?

class Symbol
def <=> other
self.to_s <=> other.to_s

Constraint Solver class

class Problem
def initialize(&block)
@domain = {}
@consts = { [] }

def variable(var, domain)
raise ArgumentError, “Cannot specify variable #{var} more than
once.” if @domain.has_key?(var)
@domain[var] = domain.to_a

def constrain(*vars, &foo)
raise ArgumentError, ‘Constraint requires at least one
variable.’ if
vars.each do |var|
raise ArgumentError, “Unknown variable: #{var}” unless
@consts[vars] = @consts[vars] << foo

def solve
# Separate constraint keys into unary and non-unary.
unary, multi = @consts.keys.partition{ |vars| vars.size == 1 }

  # Process unary constraints first to narrow variable domains.
  unary.each do |vars|
     a = vars.first
     @consts[vars].each do |foo|
        @domain[a] = @domain[a].select { |d| }

  # Build fully-expanded domain (i.e. across all variables).
  full = do |var|
     @domain[var].map do |val|
        { var => val }
  end.inject do |m, n| do |a| do |b|

  # Process non-unary constraints on full domain. do |d|
     multi.all? do |vars|
        @consts[vars].all? do |foo|
  { |v| d[v] } )


A simple example

problem = do
variable(:a, 0…10)
variable(:b, 0…10)
variable(:c, 0…10)

constrain(:a) { |a| a.even? }
constrain(:a, :b) { |a, b| b == 2 * a }
constrain(:b, :c) { |b, c| c == b - 3 }

puts “Simple example solutions:”
problem.solve.each { |sol| p sol }

Calculate some primes… The constraint problem actually finds

the non-primes, which we remove from our range afterward to get

the primes.

problem = do
variable(:a, 2…25)
variable(:b, 2…25)
variable(:c, 2…50)

constrain(:a, :b) { |a, b| a <= b }
constrain(:a, :b, :c) { |a, b, c| a * b == c }

puts “The primes up to 50:”
puts ((2…50).to_a - { |s| s[:c] }).join(", ")

I didn’t write this for the quiz, but here is a simple csp library
written in ruby:

It has a few features to speed up solving including forward checking,
dynamic variable ordering and support for specialized propagation,
but it remains very basic. There is lots more that could be done with
it, and I plan to release an update when I find the time. Feedback is
definitely welcome.

Here is how the famous N-Queens problem can be modeled and solved
with the library:

require ‘ai/csp’
include AI::CSP

def problem(n)

 # variables are columns and values are rows, so assigning
 # the first variable the value 2 corresponds to placing a
 # queen on the board at col 0 and row 2.

 variables = (0...n).collect {|i|, (0...n))
 problem =

 # None of the queens can share a row. AllDifferent is a
 # built in constraint type.

 # No pair of queens can be on the same diagonal.
 variables.each_with_index {|v1,i|
     variables[(i+1)..-1].each_with_index{ |v2,j|
         problem.add_constraint(v1, v2) { |row1,row2|
             (j+1) != (row1-row2).abs



solver =, FAIL_FIRST)
solver.each_solution(problem(8)) { |solution|
puts solution

puts solver # prints some statistics


Here’s some more puzzle solutions using Amb. This one is the one
described in the “Learn Scheme in Fixnum days” online book (where I
found the original Amb implementation). (see

However, this is not an exact transcription of the book’s logic, I was
able to simplify a number of the assertions.


– Jim W.

The Kalotans are a tribe with a peculiar quirk. Their males always

tell the truth. Their females never make two consecutive true

statements, or two consecutive untrue statements.

An anthropologist (let’s call him Worf) has begun to study

them. Worf does not yet know the Kalotan language. One day, he meets

a Kalotan (heterosexual) couple and their child Kibi. Worf asks

Kibi: ``Are you a boy?’’ Kibi answers in Kalotan, which of course

Worf doesn’t understand.

Worf turns to the parents (who know English) for explanation. One of

them says: Kibi said: `I am a boy.' '' The other adds:Kibi is a

girl. Kibi lied.’’

Solve for the sex of the parents and Kibi.

require ‘amb’

Some helper methods for logic

class Object
def implies(bool)
self ? bool : true
def xor(bool)
self ? !bool : bool

count = 0
A =

Begin the solution


Kibi’s parents are either male or female, but must be distinct.

parent1 = A.choose(:male, :female)
parent2 = A.choose(:male, :female)
A.assert parent1 != parent2

Kibi sex, and Kibi’s self description are separate facts

kibi = A.choose(:male, :female)
kibi_said = A.choose(:male, :female)

We will capture whether kibi lied in a local variable. This will

make some later logic conditions a bit easier. (Note: the Scheme

implementation sets the kibi_lied variable to a choice of true or

false and then uses assertions to make all three variables

consistent. This way however, is just so much easier.)

kibi_lied = kibi != kibi_said

Now we look at what the parents said. If the first parent was

male, then kibi must have described itself as male.

(parent1==:male).implies( (kibi_said == :male ) )

If the first parent is female, then there are no futher deductions

to make. Their statement could either be true or false.

If the second parent is male, then both its statements must be


A.assert( (parent2 == :male).implies( kibi==:female ))
A.assert( (parent2 == :male).implies( kibi_lied ))

If the second parent is female, then the condition is more

complex. In this case, one or the other of the parent 2’s

statements are false, but not both are false. Let’s introduce

some variables for statements 1 and 2 just to make this a bit


s1 = kibi_lied
s2 = (kibi == :female)

(parent2 == :female).implies( (s1 && !s2).xor(!s1 && s2) )

Now just print out the solution.

count += 1
puts “Solution #{count}”
puts “The first parent is #{parent1}.”
puts “The second parent is #{parent2}.”
puts “Kibi is #{kibi}.”
puts “Kibi said #{kibi_said} and #{kibi_lied ? ‘lied’ : ‘told the

A.failure # Force a search for another solution.

rescue Amb::ExhaustedError
puts “No More Solutions”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs