Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/cspPublic

Constraint satisfaction solvers in Elixir

NotificationsYou must be signed in to change notification settings

Lakret/csp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build statusHex.pmLicenses

This is a basic implementation of constraint satisfaction problem solver algorithms and some example problems in Elixir.

It has an accompanyingYouTube video andTwitch stream.

Installation

The package can be installed by addingcsp to your list of dependencies inmix.exs:

defdepsdo[{:csp,"~> 0.1"}]end

Docs are availableon Hex.pm.

The library is dually licensed under Apache 2 or MIT (choose whichever you prefer).

Usage

Constraints are modelled as normal Elixir structs, with the following structure:

%Csp{# list of variable ids; could be any Elixir terms that are hashablevariables:[:x,:y,...],# domains map each variable to a list of all possible values it can be assigned todomains:%{x:[1,2,3,4],y:[true,false],    ...},# constraints are specified as a list of tuples `{arguments_list, predicate}`.# `arguments_list` is a list of variables participating in the constraint.# `predicate` is an unary function taking a list of those variables values (in the same order)# and returning `true` or `false` signifying if the constraint was satisfiedconstraints:%{# the most common kind is inequality constraint, e.g. to specify that x != y:{[:x,:y],fn[x,y]->x!=yend},...}}

You can also use helpers fromCsp.Constraints andCsp.Domains modules to simplify creating CSP definitions.

Once you have a CSP definition, you can solve it:

Csp.solve(csp)

You can specify different methods, for example, min-conflicts, and pass parameters to them, e.g.:

Csp.solve(csp,method::min_conflicts,tabu_depth:10)

Additionally, you can check this repo out, build the provided escript, and play with the CLI interface for the example problems:

mix deps.getMIX_ENV=prod mix escript.build./csp

Currently implemented solvers

  • backtracking search (supports AC-3 inference, andvariable_selector strategies: naïve, minimum remaining values, and custom)
  • min-conflicts with tabu search
  • AC-3 with backtracking to extract results
  • brute-force search (used for performance comparisons with backtracking; don't use it in the real code!)

Currently provided test problems

  • N Queens (with 3 different representations)
  • Map coloring
  • Sudoku (takenfrom here)
  • Squares problem

Future plans

  • Literal constraints (e.g.,{[:x, :y], :distinct})
  • Parallel solvers
  • More examples
  • Possibly:
    • PC-2
    • Bounds propagation

About

Constraint satisfaction solvers in Elixir

Resources

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp