qmkpy.io module
Input/Output functions.
This module contains functions to save and load QMKP instances.
- qmkpy.io.load_problem_json(fname: str)
Load a previously stored QMKProblem instance from the JSON format
This function allows loading a QMKProblem from a
.json
file, which was created by theqmkpy.io.save_problem_json()
method.See also
qmkpy.io.save_problem_json()
For saving a model in the JSON format.
- Parameters
fname (
str
orPathLike
) – Filepath of the saved model- Returns
problem – Loaded problem instance
- Return type
- qmkpy.io.load_problem_numpy(fname: str)
Load a previously stored QMKProblem instance from the Numpy format
This function allows loading a QMKProblem from a compressed
.npz
file, which was created by theqmkpy.io.save_problem_numpy()
method.See also
qmkpy.io.save_problem_numpy()
For saving a model in the Numpy format.
numpy.load()
For details on loading the
.npz
format.
- Parameters
fname (
str
orPathLike
) – Filepath of the saved model- Returns
problem – Loaded problem instance
- Return type
- qmkpy.io.load_problem_pickle(fname: Union[str, bytes, PathLike])
Load a previously stored QMKProblem instance from the Pickle format
This function allows loading a QMKProblem object from a Python pickled object file.
Caution: All warnings as for the regular
pickle.load()
apply!See also
qmkpy.io.save_problem_pickle()
For saving a model in the Pickle format.
pickle.load()
For details on loading a pickled object.
- Parameters
fname (
str
orPathLike
) – Filepath of the saved model- Returns
problem – Loaded problem instance
- Return type
- qmkpy.io.load_problem_txt(fname: Union[str, bytes, PathLike], sep: str = '\t')
Load a previously stored QMKProblem instance from the text format
This function loads a QMKProblem instance from a text file according to the format specified in
qmkpy.io.save_problem_txt()
.See also
qmkpy.io.save_problem_txt()
For saving a model in the text format.
- Parameters
fname (
str
orPathLike
) – Filepath of the saved modelsep (
str
) – Separator string that is used to separate the numbers in the file.
- Returns
problem – Loaded problem instance
- Return type
- qmkpy.io.save_problem_json(fname: Union[str, bytes, PathLike], problem, name: Optional[str] = None)
Save a QMKProblem as a JSON file
Save a QMKProblem instance using the JavaScript Object Notation (JSON) format. This only saves the
problem.profits
,problem.weights
andproblem.capacities
arrays, and theproblem.name
attribute if it is set.See also
load_problem_json()
For loading a saved model.
- Parameters
fname (
str
orPathLike
) – Filepath of the model to be saved atproblem (
qmkpy.QMKProblem
) – Problem instance to be savedname (
str ,optional
) – Optional name of the problem that is used as the first line of the output file. If it isNone
, it will first be checked whether the attributeproblem.name
is set. If this is alsoNone
, the name defaults toqmkp_{num_items:d}_{num_ks:d}_{np.random.randint(0, 1000):03d}
.
- Return type
None
- qmkpy.io.save_problem_numpy(fname: Union[str, bytes, PathLike], problem)
Save a QMKProblem using Numpys npz format
Save a QMKProblem instance using the compressed npz format. This only saves the
problem.profits
,problem.weights
, andproblem.capacities
arrays.See also
load_problem_numpy()
For loading a saved model.
numpy.savez_compressed()
For details on the
.npz
format.
- Parameters
fname (
str
orPathLike
) – Filepath of the model to be saved atproblem (
qmkpy.QMKProblem
) – Problem instance to be saved
- Return type
None
- qmkpy.io.save_problem_pickle(fname: Union[str, bytes, PathLike], problem)
Save a QMKProblem using the Python Pickle format
Save a QMKProblem object using the Python pickle library. By this, the whole object is stored in a binary format.
See also
qmkpy.io.load_problem_pickle()
For loading a saved model.
pickle.dump()
For details on the underlying pickling function.
- Parameters
fname (
str
orPathLike
) – Filepath of the model to be saved atproblem (
qmkpy.QMKProblem
) – Problem instance to be saved
- Return type
None
- qmkpy.io.save_problem_txt(fname: Union[str, bytes, PathLike], qmkp, sep: str = '\t', name: Optional[str] = None)
Save a QMKProblem instance in text form
Save a QMKProblem instance in text form inspired by the format established by Alain Billionnet and Eric Soutif for the regular QKP. The original description can be found at https://cedric.cnam.fr/~soutif/QKP/format.html.
The file format is as follows:
The first line provides a name/reference of the problem
The second line specifies the number of items
The third line specifies the number of knapsacks
The fourth line is blank to separate the meta information from the rest
The fifth line contains the linear profits (main diagonal elements of the profit matrix) separated by
sep
.The next lines contain the upper triangular part of the profit matrix (i.e., \(p_{ij}\)).
Blank line separating profits from the rest
Weights \(w_{i}\) of the items, separated by
sep
.Blank line separating weights and capacities
Capacities \(c_{u}\) of the knapsacks, separated by
sep
.
For the example with parameters
\[\begin{split}P = \begin{pmatrix}1 & 2 & 3\\ 2 & 4 & 5\\ 3 & 5 & 6\end{pmatrix}, \quad w = \begin{pmatrix}10\\ 20\\ 30\end{pmatrix}, \quad c = \begin{pmatrix}5 \\ 8\\ 1\\ 9\\ 2\end{pmatrix}\end{split}\]the output-file looks as follows
Name of the Problem 3 5 1 4 6 2 3 5 10 20 30 5 8 1 9 2
See also
qmkpy.io.load_problem_txt()
For loading a saved model.
- Parameters
fname (
str
orPathLike
) – Filepath of the model to be saved atproblem (
qmkpy.QMKProblem
) – Problem instance to be savedsep (
str
) – Separator string that is used to separate the numbers in the file.name (
str ,optional
) – Optional name of the problem that is used as the first line of the output file. If it isNone
, it will first be checked whether the attributeproblem.name
is set. If this is alsoNone
, the name defaults toqmkp_{num_items:d}_{num_ks:d}_{np.random.randint(0, 1000):03d}
.
- Return type
None