Algorithm
¶
Algorithm
is defined as a subclass of py2dmat.algorithm.AlgorithmBase
import py2dmat
class Algorithm(py2dmat.algorithm.AlgorithmBase):
...
AlgorithmBase
¶
AlgorithmBase
class offers the following methods
- ``__init__(self, info: py2dmat.Info, runner: py2dmat.Runner = None)``
Reads the common parameters from
info
and sets the following instance variables:
self.mpicomm: Optional[MPI.Comm]
:MPI.COMM_WORLD
When
import mpi4py
fails, this will beNone
.
self.mpisize: int
: the number of MPI processes
When
import mpi4py
fails, this will be1
.
self.mpirank: int
: the rank of this process
When
import mpi4py
fails, this will be0
.
self.rng: np.random.Generator
: pseudo random number generator
For details of the seed, please see the [algorithm] section of the input parameter
self.dimension: int
: the dimension of the parameter space
self.label_list: List[str]
: the name of each axes of the parameter space
self.root_dir: pathlib.Path
: root directory
info.base["root_dir"]
self.output_dir: pathlib.Path
: output directory
info.base["root_dir"]
self.proc_dir: pathlib.Path
: working directory of each process
self.output_dir / str(self.mpirank)
Directory will be made automatically
Each process performs an optimization algorithm in this directory
self.timer: dict[str, dict]
: dictionary storing elapsed time
Three empty dictinaries,
"prepare"
,"run"
, and"post"
will be defined
prepare(self) -> None
Prepares the algorithm
It should be called before
self.run()
is calledIt calls
self._prepare()
run(self) -> None
Performs the algorithm
Enters into
self.proc_dir
, callsself._run()
, and returns to the original directory.
post(self) -> None
Runs a post process of the algorithm, for example, write the result into files
It should be called after
self.run()
is calledEnters into
self.output_dir
, callsself._post()
, and returns to the original directory.
main(self) -> None
Calls
prepare
,run
, andpost
Measures the elapsed times for calling each function, and write them into file
_read_param(self, info: py2dmat.Info) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]
Helper method for initializing defining the continuous parameter space
Reads
info.algorithm["param"]
and returns the followings:Initial value
Lower bound
Upper bound
Unit
For details, see [algorithm.param] subsection for minsearch
_meshgrid(self, info: py2dmat.Info, split: bool = False) -> Tuple[np.ndarray, np.ndarray]
Helper method for initializing defining the discrete parameter space
Reads
info.algorithm["param"]
and returns the followings:N
points in theD
dimensinal space as aNxD
matrixIDs of points as a
N
dimensional vector
If
split
isTrue
, the set of points is scatterred to MPI processesFor details, see [algorithm.param] subsection for mapper
Algorithm
¶
In Algorithm
, the following methods should be defined:
__init__(self, info: py2dmat.Info, runner: py2dmat.Runner = None)
Please transfer the arguments to the constructor of the base class:
super().__init__(info=info, runner=runner)
Reads
info
and sets information
_prepare(self) -> None
Pre process
_run(self) -> None
The algorithm itself
In this method, you can calculate
f(x)
from a parameterx
as the following:message = py2dmat.Message(x, step, set) fx = self.runner.submit(message)
_post(self) -> None
Post process