{"id":20783599,"url":"https://github.com/tuprolog/ortools-tsp-example","last_synced_at":"2026-04-28T04:05:10.174Z","repository":{"id":43374499,"uuid":"324737613","full_name":"tuProlog/ortools-tsp-example","owner":"tuProlog","description":"2P-Kt example binding Goodle OR-Tools via primitives to support resolution of the TSP","archived":false,"fork":false,"pushed_at":"2023-12-15T05:10:17.000Z","size":68,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-27T13:15:26.243Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","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/tuProlog.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":"2020-12-27T10:32:22.000Z","updated_at":"2024-02-19T12:08:44.000Z","dependencies_parsed_at":"2025-01-18T13:32:18.604Z","dependency_job_id":null,"html_url":"https://github.com/tuProlog/ortools-tsp-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tuProlog/ortools-tsp-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuProlog%2Fortools-tsp-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuProlog%2Fortools-tsp-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuProlog%2Fortools-tsp-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuProlog%2Fortools-tsp-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuProlog","download_url":"https://codeload.github.com/tuProlog/ortools-tsp-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuProlog%2Fortools-tsp-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32365545,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-27T20:07:02.737Z","status":"online","status_checked_at":"2026-04-28T02:00:07.250Z","response_time":56,"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-17T14:21:31.218Z","updated_at":"2026-04-28T04:05:10.125Z","avatar_url":"https://github.com/tuProlog.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Travelling Salesman Problem (TSP) in 2P-Kt\n\nBrief example showing how [Google's OR-Tools](https://developers.google.com/optimization) can exploited in Prolog, to solve the TSP problem. Our solution leverages the notion of __primitives__ from the [2P-Kt](https://github.com/tuProlog/2p-kt) Prolog implementation.\n\nA standard Prolog solver can be extended with a predicate of the form:\n```prolog\ntsp(?SetOfCities, ?Circuit, ?Cost)\n```\nwhich enumerates all minimally-`Cost`ly `Circuit`s for any possible `SetOfCities`, provided that the Prolog solver's KB contains a number of `path/3` facts describing the undirected edges of a city graph, like, e.g.:\n```prolog\npath(city1, city2, cost12).\npath(city2, city3, cost23).\npath(city3, city1, cost31).\n% etc...\n```\n\nTo solve the TSP, 2P-Kt solvers treat Google OR-Tools solvers as producers of __streams__ of solutions, to be _lazily_ enumerated as part of a standard Prolog resolution strategy.\nThus, users as well may lazily consume solutions to the TSP, via backtracking.\n\nFurthermore, the `tsp/3` predicate is fully relational, thus users may perform a wide range of queries.\n\nFor example, users may be willing to solve a particular instance of the TSP, involving e.g. cities `city1`, `city2`, and `city3`:\n```prolog\ntsp({city1, city2, city3}, Circuit, Cost), Circuit = [city2 | OtherCities].\n```\nThe query above enumerates all the minimally-`Cost`ly `Circuit`s starting from `city2` and involving exactly those 3 cities.\n\nConversely, the following query\n```prolog\ntsp({city1, city2, city3, MoreCities}, Circuit, Cost), Circuit = [city2 | OtherCities].\n```\nenumerates all the the minimally-`Cost`ly `Circuit`s starting from `city2` and stepping through _at least_ 3 cities, namely `city1`, `city2`, and `city3`.\n\nFinally, the general query\n```prolog\ntsp(Cities, Circuit, Cost).\n```\nenumerates all the the minimally-`Cost`ly `Circuit`s involving all non-empty subsets of cities mentioned in the KB.\n\n## How to run the example\n\n### Prerequisites\n\n- JDK 11+\n- \\[Optionally\\] Gradle 6.7+\n- \\[Alternatively\\] Docker\n\n### Walkthrough\n\n1. Start a new command line Prolog interpreter by running\n```bash\ngradlew -q run --args=\"-T /path/to/your/map-theory.pl\"`\n```\nfor instance, you may use `./src/test/resource/mini-map.pl`\n\n0. Alternatively, you may start the interpreter by simply running\n```bash\ngradlew -q run\n```\nand later _consult_ a graph theory via the query:\n```prolog\nconsult('file:/path/to/your/map-theory.pl').\n```\nfor instance, you may write\n```prolog\nconsult('file:./src/test/resource/mini-map.pl').\n```\n    \n0. Write your `.`-terminated Prolog query of the form `tsp(Cities, Circuit, Cost).` and press \u003ckbd\u003eEnter\u003c/kbd\u003e\n\n0. The first solution will appear: press \u003ckbd\u003e;\u003c/kbd\u003e and then \u003ckbd\u003eEnter\u003c/kbd\u003e to show the next one\n    + or just \u003ckbd\u003eEnter\u003c/kbd\u003e to interrupt the solution stream and provide a new query\n\n0. You may add new edges to the map graph by asserting new facts in the theory, e.g.:\n```prolog\nassert(path(city3, city4, cost34)).\n```\n\n0. Terminate the interpreter by querying `halt.`, or simply by pressing  \u003ckbd\u003eCtrl\u003c/kbd\u003e+ \u003ckbd\u003eD\u003c/kbd\u003e\n\n### In case of issues, try using Docker\n\n1. You may dockerify this demo on your own machine by running:\n```bash\ndocker build . -t pikalab/demos:2p-kt-tsp  \n```\n\n2. Then you may then start the dockerified interpreter by running:\n```bash\ndocker run -it --rm pikalab/demos:2p-kt-tsp\n```\n(In presence of an Internet connection this step may also work without requiring the previous one, as the image `pikalab/demos:2p-kt-tsp` is publicly available on DockerHub)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuprolog%2Fortools-tsp-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuprolog%2Fortools-tsp-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuprolog%2Fortools-tsp-example/lists"}