komputerwiz / csp-solver Goto Github PK
View Code? Open in Web Editor NEWA Ruby library for solving any type of constraint satisfaction problem (CSP)
Home Page: http://komputerwiz.net/apps/csp-solver
License: MIT License
A Ruby library for solving any type of constraint satisfaction problem (CSP)
Home Page: http://komputerwiz.net/apps/csp-solver
License: MIT License
I know I can in principle do this with OptaPlanner, but ruby is my language of preference, so if it can be done in ruby that would be really nice.
Would be nice if this lib followed standard Ruby convention and was installable as a gem. I'm happy to make a PR with that change if you're willing to release it on Rubygems.
I think a spec/
or test/
dir would be helpful too: one of my favorite ways to see how an API is used is to look at its tests. Likewise happy to provide one.
Hi, nice library. I tried to solve the einstein zebra variant with this;
The Brit lives in a red house.
The Swede keeps dogs as pets.
The Dane drinks tea.
The green house is on the left of the white, next to it.
The green house owner drinks coffee.
The person who smokes Pall Mall rears birds.
The owner of the yellow house smokes Dunhill.
The man living in the house right in the center drinks milk.
The Norwegian lives in the first house.
The man who smokes blend lives next to the one who keeps cats.
The man who keeps horses lives next to the man who smokes Dunhill.
The owner who smokes Blue Master drinks beer.
The German smokes Prince.
The Norwegian lives next to the blue house.
The man who smokes blend has a neighour who drinks water.
With the following implementation:
require "csp"
problem = CSP::Solver::Problem.new
houses = %w(blue green red white yellow)
nation = %w(british danish german norwegian swedish)
drinks = %w(beer coffee milk tea water)
cigars = %w(master dunhill pall prince blend)
pet = %w(cat bird dog fish horse)
entities = [houses, nation, drinks, cigars, pet]
entities.each do |e|
problem.vars e, 1..5
problem.all_different(e)
end
problem.constrain("british", "master"){ |b, m| b == m }
problem.constrain("swedish", "dog"){ |s, d| s == d }
problem.constrain("danish", "tea"){ |d, t| d == t }
problem.constrain("green", "white"){ |g, w| g == w - 1 }
problem.constrain("green", "coffee"){ |g, c| g == c }
problem.constrain("pall", "bird"){ |p, b| p == b }
problem.constrain("yellow", "dunhill"){ |y, d| y == d }
problem.assign "milk" => 3
problem.assign "norwegian" => 1
problem.constrain("blend", "cat"){ |b, c| (b - c).abs == 1 }
problem.constrain("master", "beer"){ |m, b| m == b }
problem.constrain("german", "prince"){ |g, p| g == p }
problem.constrain("norwegian", "blue"){ |n, b| (n - b).abs == 1 }
problem.constrain("blend", "water"){ |b, w| (b - w).abs == 1 }
p problem.solve
The result is: false
It was all fine until I got to the last constraint. Am I doing it wrong?
Hi, thanks for writing this library!
In the API reference at http://komputerwiz.net/apps/csp-solver, it shows all_pairs
, all_same
, and all_different
as having a splat operator in the argument, but the source code doesn't have it. I assume that instead of calling csp.all_different(:x, :y, :z)
for example, it should be csp.all_different([:x, :y, :z])
, correct?
Thanks again!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.