LLM-driven optimisation modelling in R A new R-bloggers post demonstrates a pipeline that uses an LLM through the `ellmer` package to translate a plain-English credit risk problem into an `ompr` optimisation model, then solves it with `ompr.roi` and the GLPK solver. The example allocates a $20 million lending budget across seven risk grades to maximise expected profit while keeping expected loss under 6% of total exposure, capping any grade at 30% of total exposure, and never exceeding each grade's observed demand. The author wraps the chain in a reusable `nl_to_opt()` helper that re-prompts the LLM when the solver reports an error, and checks results with LLM-generated controls, framing the model as a translation layer over a deterministic solver. LLM-driven optimisation modelling in R , and kindly contributed to R'tichoke https://rtichoke.netlify.app/posts/llm-written-optimisation-models.html R-bloggers https://www.r-bloggers.com/ . You can report issue about the content on this page here https://www.r-bloggers.com/contact-us/ Want to share your content on R-bloggers? click here https://www.r-bloggers.com/add-your-blog/ if you have a blog, or here http://r-posts.com/ if you don't. When defining an optimisation problem, LLMs can be used as a translation layer . The solver is the calculator that does the actual work and makes no mistakes. The translator, however, could mistranslate occasionally. Still, as long as the problem is specified well and translated correctly, the results are deterministic and reproducible. This post focuses on building the whole chain in R: 1. Describe a credit risk problem statement in plain English 2. Let an LLM via ellmer translate the problem into ompr https://dirkschumacher.github.io/ompr/ 3. Solve it with ompr.roi https://github.com/dirkschumacher/ompr.roi + GLPK 4. Wrap it all in a reusable nl to opt helper that re-prompts the LLM when the solver complains 5. Check the answer with LLM-generated controls The post uses OpenRouter https://openrouter.ai/ as the LLM provider. To run the code in this post, create an OpenRouter key and set it as the OPENROUTER API KEY environment variable for example in your .Renviron . Required Packages install.packages c "ellmer", "ompr", "ompr.roi", "ROI", "ROI.plugin.glpk", "dplyr" library ellmer library ompr library ompr.roi library ROI.plugin.glpk library dplyr The Business Problem Suppose we want to decide how much credit exposure to allocate to each risk grade. The sample dataset has seven risk grades and the problem can be specified in English as: “We have a budget of $20 million to lend. Allocate exposure across the seven risk grades to maximise expected profit. Expected loss must stay under 6% of total exposure. No grade may hold more than 30% of total exposure. Do not lend more to a grade than its observed demand.” First, let’s turn the loan-level data into a small segment table; the LLM only receives a description of its columns. Sample dataset credit <- read.csv "https://bit.ly/42ypcnJ" seg <- credit | group by grade | summarise n = n , pd = mean bad flag , observed default rate rate = mean int rate / 100, average interest rate as a decimal demand = sum loan amnt , total $ requested in the sample .groups = "drop" | mutate lgd = 0.6, assumed loss given default profit per dollar = 1 - pd rate - pd lgd, loss per dollar = pd lgd print seg A tibble: 7 × 8 grade n pd rate demand lgd profit per dollar loss per dollar