R
Ruby Quiz
The three rules of Ruby Quiz:
1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.
2. Support Ruby Quiz by submitting ideas as often as you can:
http://www.rubyquiz.com/
3. Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Jay Anderson
For this quiz the goal is to make a constraint processing library for ruby. A
Constraint Satisfaction Problem consists of variables, domains for each
variable, and constraints among the variables. Here's a sample (Solutions DO NOT
need to follow this syntax, this is just an example):
a = IntVar.newa, (0..4).to_a) #Set up the variables and their domains.
b = IntVar.newb, (0..4).to_a)
c = IntVar.newc, (0..4).to_a)
con1 = a < b #Create constraints on the problem.
con2 = a + b == c
prob = Problem.new(con1, con2) #Create a problem with the constraints
solution = prob.solve #Find a solution
p solution
There are many solutions. It could return any (or all) of the following:
{:a => 0, :b => 1, :c => 1}
{:a => 0, :b => 2, :c => 2}
{:a => 0, :b => 3, :c => 3}
{:a => 0, :b => 4, :c => 4}
{:a => 1, :b => 2, :c => 3}
{:a => 1, :b => 3, :c => 4}
Another example would be to solve the magic square:
SIDE = 3
MAX = SIDE**2
SUM = (MAX*(MAX+1))/(2*SIDE)
square = Array.new(SIDE) do |x|
Array.new(SIDE) {|y| IntVar.new("#{x},#{y}", (1..MAX).to_a ) }
end
cons = []
zero = IntVar.newzero, [0])
SIDE.times do |row|
ÊÊÊ sum = zero
ÊÊÊ SIDE.times {|col| sum += square[col][row] }
ÊÊÊ cons << sum == SUM
end
SIDE.times do |col|
ÊÊÊ sum = zero
ÊÊÊ SIDE.times {|row| sum += square[col][row] }
ÊÊÊ cons << sum == SUM
end
#A constraint to ensure no two variables have the same value in a solution.
cons << AllDistinct.new(*square.flatten)
prob = Problem.new(*cons)
solution = prob.solve
p solution
There are many problems that can be solved through constraint programming (even
some past quizzes): gift exchanges, sudoku, magic squares, N queens,
cryptoarithmetics, scheduling problems, etc... So be creative here. Pick a
simple problem to solve with your Constraint Programming Engine.
Good luck!
For more information see:
http://ktiml.mff.cuni.cz/~bartak/constraints/
and:
http://en.wikipedia.org/wiki/Constraint_programming
1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.
2. Support Ruby Quiz by submitting ideas as often as you can:
http://www.rubyquiz.com/
3. Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Jay Anderson
For this quiz the goal is to make a constraint processing library for ruby. A
Constraint Satisfaction Problem consists of variables, domains for each
variable, and constraints among the variables. Here's a sample (Solutions DO NOT
need to follow this syntax, this is just an example):
a = IntVar.newa, (0..4).to_a) #Set up the variables and their domains.
b = IntVar.newb, (0..4).to_a)
c = IntVar.newc, (0..4).to_a)
con1 = a < b #Create constraints on the problem.
con2 = a + b == c
prob = Problem.new(con1, con2) #Create a problem with the constraints
solution = prob.solve #Find a solution
p solution
There are many solutions. It could return any (or all) of the following:
{:a => 0, :b => 1, :c => 1}
{:a => 0, :b => 2, :c => 2}
{:a => 0, :b => 3, :c => 3}
{:a => 0, :b => 4, :c => 4}
{:a => 1, :b => 2, :c => 3}
{:a => 1, :b => 3, :c => 4}
Another example would be to solve the magic square:
SIDE = 3
MAX = SIDE**2
SUM = (MAX*(MAX+1))/(2*SIDE)
square = Array.new(SIDE) do |x|
Array.new(SIDE) {|y| IntVar.new("#{x},#{y}", (1..MAX).to_a ) }
end
cons = []
zero = IntVar.newzero, [0])
SIDE.times do |row|
ÊÊÊ sum = zero
ÊÊÊ SIDE.times {|col| sum += square[col][row] }
ÊÊÊ cons << sum == SUM
end
SIDE.times do |col|
ÊÊÊ sum = zero
ÊÊÊ SIDE.times {|row| sum += square[col][row] }
ÊÊÊ cons << sum == SUM
end
#A constraint to ensure no two variables have the same value in a solution.
cons << AllDistinct.new(*square.flatten)
prob = Problem.new(*cons)
solution = prob.solve
p solution
There are many problems that can be solved through constraint programming (even
some past quizzes): gift exchanges, sudoku, magic squares, N queens,
cryptoarithmetics, scheduling problems, etc... So be creative here. Pick a
simple problem to solve with your Constraint Programming Engine.
Good luck!
For more information see:
http://ktiml.mff.cuni.cz/~bartak/constraints/
and:
http://en.wikipedia.org/wiki/Constraint_programming