{"id":28764132,"url":"https://github.com/irfansharif/solver","last_synced_at":"2025-06-17T09:11:08.408Z","repository":{"id":42185715,"uuid":"384273310","full_name":"irfansharif/solver","owner":"irfansharif","description":"SAT solver library in Go; wraps around Google's Operational Research Tools","archived":false,"fork":false,"pushed_at":"2023-04-19T22:27:35.000Z","size":287,"stargazers_count":22,"open_issues_count":5,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-06-19T01:56:56.654Z","etag":null,"topics":["constraint-programming","optimization","sat-solver"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/irfansharif.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-08T23:44:07.000Z","updated_at":"2024-03-06T14:28:05.000Z","dependencies_parsed_at":"2024-06-19T01:33:24.826Z","dependency_job_id":"fe4e2bef-6045-4797-983c-3e580e217dd6","html_url":"https://github.com/irfansharif/solver","commit_stats":null,"previous_names":["irfansharif/or-tools"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/irfansharif/solver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irfansharif%2Fsolver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irfansharif%2Fsolver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irfansharif%2Fsolver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irfansharif%2Fsolver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/irfansharif","download_url":"https://codeload.github.com/irfansharif/solver/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irfansharif%2Fsolver/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260326793,"owners_count":22992388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constraint-programming","optimization","sat-solver"],"created_at":"2025-06-17T09:11:06.458Z","updated_at":"2025-06-17T09:11:08.395Z","avatar_url":"https://github.com/irfansharif.png","language":"Go","readme":"Solver\n---\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/irfansharif/solver.svg)](https://godocs.io/github.com/irfansharif/solver)\n\nThis is a SAT solver library; underneath the hood it's using cgo and links\nagainst Google's [Operations Research\nTools](https://developers.google.com/optimization/). It exposes a high-level\npackage for the [CP-SAT\nSolver](https://developers.google.com/optimization/cp/cp_solver), targeting the\n[v9.1](https://github.com/google/or-tools/releases/tag/v9.1) release.\n\n### Examples\n\nHere's a simple example solving for free integer variables, ensuring that\nthey're all different.\n\n```go\nmodel := NewModel()\n\nvar numVals int64 = 3\nx := model.NewIntVar(0, numVals-1, \"x\")\ny := model.NewIntVar(0, numVals-1, \"y\")\nz := model.NewIntVar(0, numVals-1, \"z\")\n\nct := NewAllDifferentConstraint(x, y, z)\nmodel.AddConstraints(ct)\n\nresult := model.Solve()\nrequire.True(t, result.Optimal(), \"expected solver to find solution\")\n\n{\n  x := result.Value(x)\n  y := result.Value(y)\n  z := result.Value(z)\n\n  for _, value := range []int64{x, y, z} {\n    require.Truef(t, value \u003e= 0 \u0026\u0026 value \u003c= numVals-1,\n      \"expected %d to be in domain [%d, %d]\", value, 0, numVals-1)\n  }\n\n  require.Falsef(t, x == y || x == z || y == z,\n    \"all different constraint violated, both x=%d y=%d z=%d\", x, y, z)\n}\n```\n\nHere's another solving with a few linear constraints and a maximization\nobjective.\n\n```go\nmodel := NewModel()\nx := model.NewIntVar(0, 100, \"x\")\ny := model.NewIntVar(0, 100, \"y\")\n\n// Constraint 1: x + 2y \u003c= 14.\nct1 := NewLinearConstraint(\n  NewLinearExpr([]IntVar{x, y}, []int64{1, 2}, 0),\n  NewDomain(math.MinInt64, 14),\n)\n\n// Constraint 2: 3x - y \u003e= 0.\nct2 := NewLinearConstraint(\n  NewLinearExpr([]IntVar{x, y}, []int64{3, -1}, 0),\n  NewDomain(0, math.MaxInt64),\n)\n\n// Constraint 3: x - y \u003c= 2.\nct3 := NewLinearConstraint(\n  NewLinearExpr([]IntVar{x, y}, []int64{1, -1}, 0),\n  NewDomain(0, 2),\n)\n\nmodel.AddConstraints(ct1, ct2, ct3)\n\n// Objective function: 3x + 4y.\nmodel.Maximize(NewLinearExpr([]IntVar{x, y}, []int64{3, 4}, 0))\n\nresult := model.Solve()\nrequire.True(t, result.Optimal(), \"expected solver to find solution\")\n\n{\n  x := result.Value(x)\n  y := result.Value(y)\n\n  require.Equal(t, int64(6), x)\n  require.Equal(t, int64(4), y)\n  require.Equal(t, float64(34), result.ObjectiveValue())\n}\n```\n\nFinally, an example solving for arbitrary boolean constraints.\n\n```go\nmodel := NewModel()\n\na := model.NewLiteral(\"a\")\nb := model.NewLiteral(\"b\")\nc := model.NewLiteral(\"c\")\nd := model.NewLiteral(\"d\")\ne := model.NewLiteral(\"e\")\nf := model.NewLiteral(\"f\")\n\nmodel.AddConstraints(\n  NewBooleanAndConstraint(a, b), // a \u0026\u0026 b\n  NewBooleanOrConstraint(c, d),  // c || d\n  NewBooleanXorConstraint(e, f), // e != f\n)\n\nresult := model.Solve()\nrequire.True(t, result.Optimal(), \"expected solver to find solution\")\n\n{\n  a := result.BooleanValue(a)\n  b := result.BooleanValue(b)\n  c := result.BooleanValue(c)\n  d := result.BooleanValue(d)\n  e := result.BooleanValue(e)\n  f := result.BooleanValue(f)\n\n  require.True(t, a \u0026\u0026 b)\n  require.True(t, c || d)\n  require.True(t, e != f)\n}\n```\n\nFor more, look through the package tests and the\n[docs](https://godocs.io/github.com/irfansharif/solver).\n\n### Contributing\n\nThe Go/C++ binding code is generated using [SWIG](http://www.swig.org) and can\nbe found under `internal/`. SWIG generated code is ugly and difficult to work\nwith; a sanitized API is exposed via the top-level package.\n\nBecause of the C++ dependencies, the library is compiled/tested using\n[Bazel](https://bazel.build). The top-level Makefile packages most things\nyou'd need.\n\n```sh\n# ensure that the submodules are initialized:\n#   git submodule update --init --recursive\n#\n# supported bazel version \u003e= 4.0.0\n# supported swig version == 4.0.2\n# supported protoc version == 3.14.0\n# supported protoc-gen-go version == 1.27.1\n\n$ make help\nSupported commands: build, test, generate, rewrite\n\n$ make generate\n--- generating go:generate files\n--- generating swig files\n--- generating proto files\n--- generating bazel files\nok\n\n$ make build\nok\n\n$ make test\n...\nINFO: Build completed successfully, 4 total actions\n```\n\n#### Testing\n\nThis library is tested using the (awesome)\n[datadriven](https://github.com/cockroachdb/datadriven) library + a tiny\ntesting grammar. See `testdata/` for what that looks like.\n\n```\nsat\nmodel.name(ex)\nmodel.literals(x, y, z)\nconstrain.at-most-k(x to z | 2)\nmodel.print()\n----\nmodel=ex\n  literals (num = 3)\n    x, y, z\n  constraints (num = 1)\n    at-most-k: x, y, z | 2\n\nsat\nmodel.solve()\n----\noptimal\n\nsat\nresult.bools(x to z)\n----\nx = false\ny = false\nz = false\n```\n\n```sh\n# to update the testdata files\n$ make rewrite\n\n# to run specific tests\n$ bazel test ... --test_output=all \\\n  --cache_test_results=no \\\n  --test_arg='-test.v' \\\n  --test_filter='Test.*'\n```\n\n### Acknowledgements\n\nThe SWIG interface files to work with protobufs was cribbed from\n[AirspaceTechnologies/or-tools](https://github.com/AirspaceTechnologies/or-tools).\nTo figure out how to structure this package as a stand-alone bazel target, I\nlooked towards from\n[gonzojive/or-tools-go](https://github.com/gonzojive/or-tools-go). The CP-SAT\nstuff was then mostly pattern matching.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firfansharif%2Fsolver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Firfansharif%2Fsolver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firfansharif%2Fsolver/lists"}