Key functions
- One-call workflow:
solve_model()andsolver_options. - Explicit stages:
interpolate_model(),write_model(),solve_scenario(),read_solution(). - Runs and diagnostics:
scenario_runs(),with_solver_log().
The solve pipeline
Solving is a short pipeline with inspectable boundaries:
- a
modelholds economic objects and configuration; -
interpolate_model()expands their compact data into ascenarioand validates the solver input (modInp); - a backend writes and solves the mathematical program;
-
read_solution()decodes solver output intomodOut.
For routine work, solve_model() performs the whole
pipeline:
scen <- solve_model(model, name = "BASE", solver = solver_options$glpk)During development, keep interpolation and solving separate. It makes data errors distinguishable from backend or solver errors:
scen <- interpolate_model(model, name = "BASE")
scen <- solve_scenario(scen, solver = solver_options$glpk, wait = TRUE)Model configuration such as calendar, horizon, regions, geoscale, and discount rate belongs to the model or scenario. A solver preset controls only how the already-declared optimization problem is written, exchanged, and solved.
Choosing a backend
solver_options is a named catalog of working presets.
Start with a preset and pass it explicitly while comparing runs:
names(solver_options)
solver_options$glpk
solve_model(model, solver = solver_options$julia_highs)
solve_model(model, solver = solver_options$gams_gdx_cplex)| family | modelling layer | typical use |
|---|---|---|
| GLPK / MathProg | native energyRt formulation | portable baseline and small models |
| Python / Pyomo | Pyomo | access to CBC, CPLEX, and other Python solvers |
| Julia / JuMP | JuMP | HiGHS, CPLEX, and GPU-capable solvers |
| GAMS | GAMS | established commercial-solver installations |
| matrix route | direct sparse matrix | large LPs and local or cloud GPU solving |
| NEOS | remote service | occasional remote solving; never send confidential data |
Not every preset works on every machine. It may require its language runtime, solver license, or an exchange library. The installation article lists those prerequisites. GLPK is the useful reference backend: solve a small case there before investigating a more complex stack.
To make one preset the session default:
set_default_solver(solver_options$julia_highs)
scen <- solve_model(model)Keep project scripts explicit when reproducibility matters; a session default is convenient interactively but invisible in a saved analysis.
Solver settings
A preset is an R list containing the backend, exchange format, executable or runtime, and solver-specific options. Copy a preset before changing it:
my_solver <- solver_options$julia_highs
# Modify the documented backend/solver option in my_solver.
scen <- solve_model(model, solver = my_solver)Prefer solver-native tolerances, time limits, algorithms, and thread settings inside that copy. Save the chosen preset with the analysis or record it in the scenario metadata; otherwise two runs named alike may not be comparable.
Runs and project storage
With a project directory configured, interpolation creates the stable scenario area and each solve creates a run below it. A named run preserves alternatives side by side:
scen <- solve_scenario(
scen,
solver = solver_options$glpk,
run = "glpk-baseline",
wait = TRUE
)
scenario_runs(scen)
scen <- read_solution(scen, run = "glpk-baseline")Use meaningful labels such as solver, algorithm, or experiment ID. Reusing an unlabelled default run is appropriate for disposable iteration, not for a comparison that must be audited. See Models, scenarios, and projects for the directory layout and in-memory versus on-disk objects.
wait = TRUE returns after the solution is available. A
backend that supports asynchronous execution can return earlier with
wait = FALSE; inspect scenario_runs() before
trying to read the result.
Inspecting the stages
When something fails, identify the boundary first.
Interpolation and declaration
interpolate_model() validates names, dimensions, bounds,
and relationships while it constructs modInp. Inspect it
before blaming the solver:
scen <- interpolate_model(model, name = "CHECK")
names(scen@modInp)
getData(scen, "pTechCapUp")An error here is usually a model-data or configuration problem. The validation article gives a systematic checklist.
Written program and raw output
For backend-level diagnosis, keep the generated files and use the explicit write/run/read stages supported by that backend:
scen <- write_model(scen, solver = solver_options$glpk)
scen <- solve_scenario(scen, solver = solver_options$glpk, wait = TRUE)
scen <- read_solution(scen)The run directory contains the generated program or matrix, exchange files, solver log, and raw output. These are implementation artifacts; query the decoded scenario rather than parsing them in reports.
Logs and status
Ask a supported solver preset to write a progress log while diagnosing numerical or installation problems:
logged_solver <- with_solver_log(solver_options$julia_highs, "highs.log")
scen <- write_model(scen, solver = logged_solver)
scen <- solve_scenario(scen, solver = logged_solver, wait = TRUE)Check three things in order: process exit status, solver termination status, and whether a primal solution was decoded. “Executable not found”, “infeasible”, and “optimal but unreadable output” are different failures and need different fixes.
Cross-checking a solution
For a new backend or a material model change:
- solve a small deterministic case with GLPK and the target backend;
- compare termination status and objective within an explicit tolerance;
- compare a few structural quantities (capacity, activity, imports, storage);
- retain both run labels and solver logs.
Barrier or first-order methods can legitimately return objectives that differ in the last digits, and alternative optima can have the same objective with different activities. Treat the objective as a first check, not proof that the solutions are identical.
Beyond one perfect-foresight solve
Base-year calibration, recursive/myopic solving, repeated calendar samples, and region-by-region sequences change the solution method rather than the backend. They are covered with subsetting and aggregation in Scaling models and alternative solves. Results, plots, run selection, and reports are covered in Results and reporting.
