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 correspondingload()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.
- filestr, bytes, or
- 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
Algorithmobject, 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.
- filestr, bytes, or
- 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:Inspect or record the configuration of an algorithm; or
Allow resuming runs from the state file.
This feature is experimental. Please take note of the following:
Platypus uses Python’s
randomlibrary, which uses a global RNG state. Reproducibility is not guaranteed when running multithreaded or async programs.State files are not guaranteed to be compatible across versions, including minor or patch versions.
Internally,
pickleandjsonpickle(for JSON output) are used. Refer to each for warnings related to potential security concerns when dealing with untrusted inputs.
Setting
json=Truewill produce human-readable output in a JSON format. This requires the optionaljsonpickledependency.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. IfTrue, produces a JSON file.- indent:
Controls the formatting of the JSON fle, see
json.dump().