platypus.io module

dump(obj, fp, indent=None)

Dumps the object to JSON.

This overrides json.dump() to convert Platypus types to JSON. Use the corresponding load() method in this module to load the JSON back into memory.

Parameters

objobject

The object to dump to JSON.

fpfile-like

A file-like object, typically created by open().

indent:

Controls the formatting of the JSON fle, see json.dump().

load(fp, problem=None)

Loads the JSON data.

Parameters

fpfile-like

A file-like object, typically created by open().

problemProblem, optional

Optional problem definition. If not set, a placeholder is used.

load_json(file, problem=None)

Loads the solutions stored in a JSON file.

Parameters

filestr, bytes, or os.PathLike

The file.

problemProblem, optional

The problem definition. If None, a placeholder is used.

load_objectives(file, problem=None)

Loads objective values from a file.

Parameters

filestr, bytes, or os.PathLike

The file.

problemProblem, optional

The problem definition. If None, a placeholder is used.

load_state(file, update_rng=True)

Restores the algorithm from a state file.

Refer to save_state() for details and warnings when working with state files.

Parameters

file: str, bytes, or os.PathLike

The file.

update_rng: bool

If True, updates the RNG state. Must be set for reproducible results.

save_json(file, solutions, indent=None)

Converts the solutions to JSON and saves to a file.

If given an Algorithm object, extra information about the algorithm state and problem are stored in the JSON; however, this is for informational purposes only and can not be read back.

Parameters

filestr, bytes, or os.PathLike

The file.

solutionsobject

The solutions, archive, or algorithm.

indent:

Controls the formatting of the JSON fle, see json.dump().

save_objectives(file, solutions)

Saves objective values to a file.

Parameters

filestr, bytes, or os.PathLike

The file.

solutionsiterable of Solution

The solutions to save.

save_state(file, algorithm, json=False, indent=None)

Capture and save the algorithm state to a file.

Allows saving the algorithm and RNG state to a file, which can be later restored using load_state(). This is useful to either:

  1. Inspect or record the configuration of an algorithm; or

  2. Allow resuming runs from the state file.

This feature is experimental. Please take note of the following:

  1. Platypus uses Python’s random library, which uses a global RNG state. Reproducibility is not guaranteed when running multithreaded or async programs.

  2. State files are not guaranteed to be compatible across versions, including minor or patch versions.

  3. Internally, pickle and jsonpickle (for JSON output) are used. Refer to each for warnings related to potential security concerns when dealing with untrusted inputs.

Setting json=True will produce human-readable output in a JSON format. This requires the optional jsonpickle dependency.

Parameters

file: str, bytes, or os.PathLike

The file.

algorithm: Algorithm

The algorithm to capture.

json: bool

If False, produces a binary-encoded state file. If True, produces a JSON file.

indent:

Controls the formatting of the JSON fle, see json.dump().