Skip to content

Archiving a solve

How to keep the spec, the data and the answer of a solve, to read or solve again later. The reference is read_archive.

Archive as you solve

sps.solve, model.solve and sps.solve_over take archive=:

import specsolve as sps

sps.solve('dispatch.yaml', sources, archive='case/')

A path without a .zip suffix is a directory:

case/
    spec.yaml
    sources/cost.parquet          each source, as the solve read it
    catalog.parquet               what each file holds
    answer/record.parquet         how it ended, what it reached, and on which solver and versions
    answer/metrics.parquet        how long each phase took, from the spec to the archive
    answer/primal/p.parquet       one file per variable
    answer/dual/power_balance.parquet

An answer holds the primal, the duals and the declared expressions. Anything else is held only when outputs= asks for it.

The record keeps the solver options that change the answer, such as a time limit or a gap. Any other option is <not recorded> unless you name it in also_record=.

Read it back

result = sps.read_archive('case/')

result.objective  # what it reached
result.primal('p')  # the values it came back with
sps.solve(result.spec, result.sources)  # the same question, asked again
sps.solve(result.spec, result.sources, **result.solver_settings)  # on the same solver and options

solver_settings gives back the solver and options the archive recorded, in the keywords every solving call takes. An option the record kept no value of holds '<not recorded>', and a solve refuses it by name. Pass its value again, or name the option in also_record= when you archive, so the archive keeps it:

settings = result.solver_settings
options = {**settings['solver_options'], 'mip_max_nodes': 1000}
sps.solve(result.spec, result.sources, solver_name=settings['solver_name'], solver_options=options)

A sweep gives its settings the same way, as sps.solve_over(sweep.spec, sweep.sources, sweep.axis, **sweep.solver_settings).

read_archive returns the answer itself: the Result, or the Sweep of a sweep archive. It carries the spec, the sources and the metrics reaching it took; a sweep also carries its axis.

An archive always carries its sources, and a regular answer does not:

result = sps.solve(spec, sources)  # result.sources is None: no archive
result = sps.solve(spec, sources, archive='case/')  # the paths under case/sources/
result = sps.solve(spec, sources, archive='case.zip')  # the tables it wrote
result = sps.read_archive('case/')  # the tables
sps.solve(result.spec, result.sources)

A directory archive's answer points at its members and holds no table in memory. A zip has no file to point at, so its answer holds the tables. A sweep carries its axis whether or not it was archived.

read_inputs reads the spec and the data, and never the answer. It still reads an archive whose answer layout read_archive refuses.

Read one too big to hold

scan_archive reads nothing until asked:

sweep = sps.scan_archive('sweep.zip', 'sweep/')
sweep.scan('p')  # read at the collect, one name at a time
read_archive scan_archive
a source the table the member holds the path to it
the answer tables in memory read at the call that asks
a sweep's answer in memory: sweep.primal('p') on disk: sweep.scan('p')
a zip's into= optional, and scratch without one required, and kept

Archive a sweep

archive= on solve_over also keeps the axis, carry included, so the sweep runs again from the archive alone:

axis = sps.EachWindow('snapshot', horizon=24, lookahead=24, carry={'soc_initial': 'soc'})
sps.solve_over('window.yaml', sources, axis, archive='roll/')

sweep = sps.read_archive('roll/')
sps.solve_over(sweep.spec, sweep.sources, sweep.axis)

A sweep's answer/ holds one file per slice of each name, so primal(..., per_window=True) reads off the archive too. A directory archive is written as the sweep goes, one slice at a time, so the sweep holds one slice in memory. Run the same call at the same directory again and the slices already there are read back rather than solved, which is how a sweep that stopped resumes. A .zip is packed once the sweep ends, and is not resumed.

What an archive will not take

  • A directory that already holds something, unless it is the sweep archive that sweep resumes. A .zip target is replaced.
  • A resume from another spec, axis or sources than the directory holds. Its slices were solved from those, so archive to a new directory.

A directory of archives reads as one set of tables: reading a directory of runs.