{"id":13858051,"url":"https://github.com/dirkschumacher/ompr","last_synced_at":"2025-04-04T06:09:23.630Z","repository":{"id":9425591,"uuid":"61876695","full_name":"dirkschumacher/ompr","owner":"dirkschumacher","description":"R package to model Mixed Integer Linear Programs","archived":false,"fork":false,"pushed_at":"2023-09-09T10:35:33.000Z","size":14356,"stargazers_count":269,"open_issues_count":30,"forks_count":34,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-03-28T05:13:44.739Z","etag":null,"topics":["integer-programming","linear-programming","milp","mip","optimization","r"],"latest_commit_sha":null,"homepage":"https://dirkschumacher.github.io/ompr/","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dirkschumacher.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-06-24T10:21:32.000Z","updated_at":"2025-03-19T02:55:58.000Z","dependencies_parsed_at":"2022-08-21T00:40:26.601Z","dependency_job_id":null,"html_url":"https://github.com/dirkschumacher/ompr","commit_stats":{"total_commits":460,"total_committers":4,"mean_commits":115.0,"dds":"0.013043478260869601","last_synced_commit":"0f7dfb905a754b196569e04c133cd302d98bbea8"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Fompr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Fompr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Fompr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirkschumacher%2Fompr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dirkschumacher","download_url":"https://codeload.github.com/dirkschumacher/ompr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247128752,"owners_count":20888235,"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":["integer-programming","linear-programming","milp","mip","optimization","r"],"created_at":"2024-08-05T03:01:55.166Z","updated_at":"2025-04-04T06:09:23.614Z","avatar_url":"https://github.com/dirkschumacher.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# Mixed integer linear programming in R\n\n\u003c!-- badges: start --\u003e\n[![R build status](https://github.com/dirkschumacher/ompr/workflows/R-CMD-check/badge.svg)](https://github.com/dirkschumacher/ompr/actions)\n[![CRAN Status](https://www.r-pkg.org/badges/version/ompr)](https://cran.r-project.org/package=ompr)\n[![Codecov test coverage](https://codecov.io/gh/dirkschumacher/ompr/branch/master/graph/badge.svg)](https://app.codecov.io/gh/dirkschumacher/ompr?branch=master)\n\u003c!-- badges: end --\u003e\n\nOMPR (Optimization Modeling Package) is a DSL to model and solve Mixed Integer Linear Programs. It is inspired by the excellent Jump project in Julia.\n\nHere are some problems you could solve with this package:\n\n  * What is the cost minimal way to visit a set of clients and return home afterwards?\n  * What is the optimal conference time table subject to certain constraints (e.g. availability of a projector)?\n  * [Sudokus](https://github.com/dirkschumacher/r-sudoku)\n  \nThe [Wikipedia](https://en.wikipedia.org/wiki/Integer_programming) article gives a good starting point if you would like to learn more about the topic.\n\nI am always happy to get bug reports or feedback. \n\n## Install\n\n### CRAN\n\n```R \ninstall.packages(\"ompr\")\ninstall.packages(\"ompr.roi\")\n```\n\n### Development version\n\nTo install the current development version use devtools:\n\n```R \nremotes::install_github(\"dirkschumacher/ompr\")\nremotes::install_github(\"dirkschumacher/ompr.roi\")\n```\n\n## Available solver bindings\n\n* [ompr.roi](https://github.com/dirkschumacher/ompr.roi) - Bindings to ROI (GLPK, Symphony, CPLEX etc.)\n\n## A simple example:\n\n```{r}\nsuppressPackageStartupMessages(library(dplyr, quietly = TRUE)) \nsuppressPackageStartupMessages(library(ROI))\nlibrary(ROI.plugin.glpk)\nlibrary(ompr)\nlibrary(ompr.roi)\n\nresult \u003c- MIPModel() |\u003e\n  add_variable(x, type = \"integer\") |\u003e\n  add_variable(y, type = \"continuous\", lb = 0) |\u003e\n  set_bounds(x, lb = 0) |\u003e\n  set_objective(x + y, \"max\") |\u003e\n  add_constraint(x + y \u003c= 11.25) |\u003e\n  solve_model(with_ROI(solver = \"glpk\"))\nget_solution(result, x)\nget_solution(result, y)\n```\n\n## API\n\nThese functions currently form the public API. More detailed docs can be found in the package function docs or on the [website](https://dirkschumacher.github.io/ompr/)\n\n### DSL\n* `MIPModel()` create an empty mixed integer linear model (the old way)\n* `add_variable()` adds variables to a model\n* `set_objective()` sets the objective function of a model\n* `set_bounds()` sets bounds of variables\n* `add_constraint()` add constraints\n* `solve_model()` solves a model with a given solver\n* `get_solution()` returns the column solution (primal or dual) of a solved model for a given variable or group of variables\n* `get_row_duals()` returns the row duals of a solution (only if it is an LP)\n* `get_column_duals()` returns the column duals of a solution (only if it is an LP)\n\n### Backends\n\nThere are currently two backends. A backend is the function that initializes an empty model.\n\n* `MIPModel()` is the standard MILP Model.\n* `MILPModel()` is another backend specifically optimized for linear models and is often faster than `MIPModel()`. It has different semantics, as it is vectorized. Currently experimental and might be deprecated in the future.\n\n### Solvers\n\nSolvers are in different packages. `ompr.ROI` uses the ROI package which offers support for all kinds of solvers.\n\n* `with_ROI(solver = \"glpk\")` solve the model with GLPK. Install `ROI.plugin.glpk`\n* `with_ROI(solver = \"symphony\")` solve the model with Symphony. Install `ROI.plugin.symphony`\n* `with_ROI(solver = \"cplex\")` solve the model with CPLEX. Install `ROI.plugin.cplex`\n* ... See the [ROI package](https://CRAN.R-project.org/package=ROI) for more plugins.\n\n \n## Further Examples\n\nPlease take a look at the [docs](https://dirkschumacher.github.io/ompr/articles/index.html) for bigger examples.\n\n### Knapsack\n\n```{r}\nmax_capacity \u003c- 5\nn \u003c- 10\nset.seed(1234)\nweights \u003c- runif(n, max = max_capacity)\nMIPModel() |\u003e\n  add_variable(x[i], i = 1:n, type = \"binary\") |\u003e\n  set_objective(sum_over(weights[i] * x[i], i = 1:n), \"max\") |\u003e\n  add_constraint(sum_over(weights[i] * x[i], i = 1:n) \u003c= max_capacity) |\u003e\n  solve_model(with_ROI(solver = \"glpk\")) |\u003e\n  get_solution(x[i]) |\u003e\n  filter(value \u003e 0)\n```\n\n### Bin Packing\nAn example of a more difficult model solved by GLPK\n\n```{r}\nmax_bins \u003c- 10\nbin_size \u003c- 3\nn \u003c- 10\nweights \u003c- runif(n, max = bin_size)\nMIPModel() |\u003e\n  add_variable(y[i], i = 1:max_bins, type = \"binary\") |\u003e\n  add_variable(x[i, j], i = 1:max_bins, j = 1:n, type = \"binary\") |\u003e\n  set_objective(sum_over(y[i], i = 1:max_bins), \"min\") |\u003e\n  add_constraint(sum_over(weights[j] * x[i, j], j = 1:n) \u003c= y[i] * bin_size, i = 1:max_bins) |\u003e\n  add_constraint(sum_over(x[i, j], i = 1:max_bins) == 1, j = 1:n) |\u003e\n  solve_model(with_ROI(solver = \"glpk\", verbose = TRUE)) |\u003e\n  get_solution(x[i, j]) |\u003e\n  filter(value \u003e 0) |\u003e\n  arrange(i)\n```\n\n\n## License\n\nMIT\n\n## Contributing\n\nPlease post an issue first before sending a PR.\n\nPlease note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.\n\n## Related Projects\n\n* [CVXR](https://cvxr.rbind.io/) - an excellent package for \"object-oriented modeling language for convex optimization\". LP/MIP is a special case.\n* [ROML](https://r-forge.r-project.org/projects/roml/) follows a similar approach, but it seems the package is still under initial development.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirkschumacher%2Fompr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdirkschumacher%2Fompr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirkschumacher%2Fompr/lists"}