{"id":20294196,"url":"https://github.com/grab/inc-weighted-sampler","last_synced_at":"2026-06-08T10:33:00.996Z","repository":{"id":54601645,"uuid":"522521262","full_name":"grab/inc-weighted-sampler","owner":"grab","description":null,"archived":false,"fork":false,"pushed_at":"2023-06-12T05:40:07.000Z","size":19,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2026-04-21T19:55:31.218Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grab.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":"2022-08-08T11:29:15.000Z","updated_at":"2025-11-21T20:12:25.000Z","dependencies_parsed_at":"2025-01-14T09:34:07.989Z","dependency_job_id":"f871fc85-69e8-4b69-b938-a22ccf659429","html_url":"https://github.com/grab/inc-weighted-sampler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/grab/inc-weighted-sampler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grab%2Finc-weighted-sampler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grab%2Finc-weighted-sampler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grab%2Finc-weighted-sampler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grab%2Finc-weighted-sampler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grab","download_url":"https://codeload.github.com/grab/inc-weighted-sampler/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grab%2Finc-weighted-sampler/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34059156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-08T02:00:07.615Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-14T15:28:11.745Z","updated_at":"2026-06-08T10:33:00.975Z","avatar_url":"https://github.com/grab.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# INC: incremental weighted sampler\n\nINC, INCremental weighted sampler, draws samples from the set of satisfying assignments of a set of boolean contraints. The sampling distribution is defined by a literal-weighted weight function. INC takes CNF in DIMACS format and operates by compiling the CNF to OBDD[AND] diagram, performing smoothing and augmenting the diagram with weights to form the PROB that is used to perform sampling.\n\nThis repository contains code for our paper *INC: A Scalable Incremental Weighted Sampler*\n\n---\n## Installation\n\n1. INC depends on KCBox compiler to compile CNF into OBDD[AND] diagram. Please follow the instructions [here](https://github.com/meelgroup/KCBox) to compile a copy of KCBox.\n\n2. Follow the instructions to install INC from its root directorty as follows:\n\n```\nsudo apt-get install graphviz libgmp-dev libmpfr-dev libmpc-dev time\npip install --upgrade build\npython -m build\ncd dist\npip install inc-0.0.1-py3-none-any.whl\n```\n\nIf preference is to use from directory, then please cd to `src` folder and perform the following:\n```\nsudo apt-get install graphviz libgmp-dev libmpfr-dev libmpc-dev time\npip install -r requirements.txt\n```\n\n---\n\n## Using INC\n\n\nExample CNF `F = (X1 OR X2) AND (-X1 OR X3)`:\n```\np cnf 3 2\n1 2 0\n-1 3 0\n```\n\nStart by importing all the relevant components:\n```\nimport pobdd_sampler_func, pobdd_inference, pobdd_parser, pobdd_utils\nimport numpy as np\n```\n\nCompile the CNF file into OBDD[AND] format using KCBox\n```\ncompile_success, obdd_file = pobdd_sampler_func.compile_cnf_file('/path/to/KCBox', '/path/to/example.cnf')\n```\n\nNext we perform smoothing on the OBDD[AND]:\n```\nsmooth_success, smooth_obdd_file = pobdd_sampler_func.call_smoother(obdd_file)\n```\n\nParse the smoothed OBDD[AND] into memory as a PROB, default weights are 0.5 for both positive and negative literals:\n```\nvariable_ordering_list, num_nodes, root_node_id, node_list = pobdd_parser.parse_obdd(smooth_obdd_file)\nroot_node = node_list[int(root_node_id)]\n```\n\nDefine a weight function and apply the weights to PROB (0.75 for all positive literals and 0.25 for negative literals):\n```\n# only required to specify weights of positive literals if the positive weights are normalized\nweight_dict = {1:0.75, 2:0.75, 3:0.75}\npobdd_sampler_func.apply_weights(node_list, weight_dict)\n```\nA bit of preprocessing before sampling:\n```\n# figure out which nodes to consider (ignore false node and edges leading to false node)\nforward_edge_adj_dict = pobdd_parser.parse_pobdd_reduced_forwardedges(node_list)\n\n# perform toposort on reduced PROB to get bottom up ordering information\ntoposort_result_sets = pobdd_parser.perform_toposort(forward_edge_adj_dict)\n\n# split each layer into decision and conjuction nodes\nlayer_node_list = pobdd_inference.prepare_layer_node_lists(toposort_result_sets, node_list)\n\ntrue_node_id, false_node_id = pobdd_utils.get_true_false_node_id(node_list)\ntrue_node = node_list[true_node_id]\n```\n\nPerform weighted sampling:\n```\n# returns a numpy 2d array, axis 0 is of num_samples, axis 1 is the assignment for variables (not necessarily ordered)\n\n# if high_precision=True, arbitrary precision math is used, else log-space computation is used\n# conditional_assignment_set can include literals for conditional weighted sampling\nconditional_assignment_set = set()\n\n# drawing 10000 samples at once\nsamples = pobdd_sampler_func.sample_assignments(conditional_assignment_set, node_list, root_node, true_node, layer_node_list, num_samples=10000, seed=None, high_precision=False)\n\n# sort the samples according to the variables\nsamples = samples[np.arange(samples.shape[0])[:, None], np.abs(samples).argsort()]\n```\n\nAs the PROB is in memory, we can simply use `pobdd_sampler_func.apply_weights` to apply new weights and sample again.\n\nThe PROB, along with weights, can be saved into a file:\n```\n# save PROB to file\npobdd_output_file = smooth_obdd_file + '.pobdd'\npobdd_utils.write_output_pobdd_file(node_list, root_node, variable_ordering_list, pobdd_output_file)\n\n# load a PROB file\nvariable_order_list, num_nodes, root_node_id, node_list = pobdd_parser.parse_pobdd(pobdd_output_file)\n```\n\n---\n\n## License\n\nThis project is licensed under the terms of the MIT license, see the LICENSE file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrab%2Finc-weighted-sampler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrab%2Finc-weighted-sampler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrab%2Finc-weighted-sampler/lists"}