Skip to contents

Reads the variables a solver wrote for one run and returns the scenario with its @modOut populated. This is the import step: solve_scenario() leaves the raw output on disk, and nothing else reads it.

Usage

read_solution(
  object,
  run = NULL,
  dir = NULL,
  wait = FALSE,
  save = FALSE,
  restore_solver = NULL,
  promote = FALSE,
  cleanup = FALSE,
  ...,
  ondisk = !isInMemory(object)
)

# S4 method for class 'scenario'
read(
  object,
  run = NULL,
  dir = NULL,
  wait = FALSE,
  save = FALSE,
  restore_solver = NULL,
  promote = FALSE,
  cleanup = FALSE,
  ...,
  ondisk = !isInMemory(object)
)

Arguments

object

scenario object.

run

character, optional run to read: "<solve>" for a base-problem run or "<variant>/<solve>" — see scenario_runs(). The scenario's active run switches to it. Default NULL reads the active run (or, for a freshly loaded scenario, the manifest's default: run).

dir

deprecated: the former spelling of solver.dir, still accepted with a message.

wait

FALSE (default) reads what is there now. TRUE blocks until the run's output/ is complete, which is what a detached solve (wait = FALSE on the solve) needs; a number waits that many seconds and then gives up. Needs run or dir, to know what to wait for.

save

logical. TRUE persists the solution with save_scenario(), so it survives the session – this is what import_solution() did. A read that produced no variables refuses to save rather than writing an empty store over a run whose output/ is the only copy.

restore_solver

logical. Copy the solver identity recorded in the run's solver.csv onto the scenario. NULL (default) means "when saving", which is what makes an imported run reproducible.

promote

logical, make the read run the scenario's active solution (promote_solution()). Requires the store on disk, so it implies save.

cleanup

logical, drop the solver's raw output/ afterwards (drop_solver_outputs()). A clean-up failure warns and does not undo the import.

...

optional solver.dir: a run's working folder outside the scenario, read instead of resolving a recorded run – a solve that ran outside R, on a cluster or a rented GPU. It is the same argument as in solve_scenario() and write_model(); a recorded run is chosen with run = instead. tmp.dir is a deprecated alias.

ondisk

logical. TRUE writes each variable into the run's modOut/ store as it is read, instead of returning the whole solution in memory; the returned @modOut is then an on-disk object read lazily. Defaults to !isInMemory(obj). Needs a run folder, so an external solver.dir falls back to memory. The saved scenario still has to be saved: save_scenario() records the store, and skips rewriting it.

Value

The function returns the scenario object with populated modOut slot from the solved model directory.

Details

Without run, it reads the active run — or, for a freshly loaded scenario, the default: run named in scenario.yml. Naming a variant run switches the whole in-memory problem, settings and parameters together, so getData() stays consistent with the run you are looking at.

Reading alone does not persist anything: save_scenario() writes the modOut/ store, and import_solution() does both in one call. scenario_solutions() lists what each run holds before you choose.

Why the imported store, and not just output/

The two are different things, and modOut/ is not a compressed copy of output/. The solver's dump is untyped text in the backend's own naming; the import normalises the backend away (GDX, CSV, Arrow and Parquet all land in one store), synthesises the set aliases no solver writes (src, dst, regionp, yearp, acomm, commp, timeslicep), types every dimension against its declared set members rather than the values that happen to occur, keeps an empty typed skeleton for variables the solver skipped so their columns stay known, and computes two variables no solver ever wrote — vTechEmsFuel (emission factors against vTechInp) and vUserCosts (each cost object against the variable it is defined on).

Re-deriving that on demand would also need @modInp, since the set members come from the problem — and a variant swap may have replaced it.

The solver's output/ is never modified

Reading a solution only ever reads output/, whatever ondisk is. It is the solver's raw dump and, until the solution has been imported, the only copy of it — drop_solver_outputs() is the one verb that removes it, it refuses while the run is not imported, and it is dry-run by default.

See also

save = TRUE keeps the solution (import_solution() is the deprecated spelling of that); scenario_solutions() to see what each run holds; solve_scenario(), save_scenario().

Other solving: mm_run(), solve_by_region(), solve_by_sample(), solve_guided(), solve_model(), solve_recursive(), with_solver_log(), write_model()

Examples

if (FALSE) { # \dontrun{
scen <- read_solution(scen)                    # the active run
scen <- read_solution(scen, run = "low/glpk")  # a variant's run
} # }