Algorithm¶
Algorithm is defined as a subclass of odatse.algorithm.AlgorithmBase:
import odatse
class Algorithm(odatse.algorithm.AlgorithmBase):
pass
AlgorithmBase¶
AlgorithmBase class offers the following methods.
__init__(self, info: odatse.Info, runner: odatse.Runner = None)Reads the common parameters from
infoand sets the following instance variables:self.mpicomm: Optional[MPI.Comm]:MPI.COMM_WORLDself.mpisize: int: the number of MPI processesself.mpirank: int: the rank of this processWhen
import mpi4pyfails,self.mpicommis set toNone,self.mpisizeis set to 1, andself.mpirankis set to 0.
self.rng: np.random.Generator: pseudo random number generatorFor details of the seed, see the [algorithm] section of the input parameter
self.dimension: int: the dimension of the parameter spaceself.label_list: List[str]: the name of each axes of the parameter spaceself.root_dir: pathlib.Path: root directoryIt is taken from
info.base["root_dir"].
self.output_dir: pathlib.Path: output directoryIt is taken from
info.base["output_dir"].
self.proc_dir: pathlib.Path: working directory of each processIt is set to
self.output_dir / str(self.mpirank).The directory will be made automatically.
Each process performs an optimization algorithm in this directory.
self.timer: dict[str, dict]: dictionary storing elapsed timeThree empty dictinaries,
"prepare","run", and"post", will be defined.
self.checkpoint: bool: enable/disable checkpointingself.checkpoint_stepsself.checkpoint_intervalself.checkpoint_fileThe parameters concerning the checkpointing feature are stored.
prepare(self) -> NonePrepares the algorithm.
It should be called before
self.run()is called.
run(self) -> NonePerforms the algorithm
The following steps are executed:
Enter into the directory
self.proc_dir.Run
self.runner.prepare().Run
self._run().Run
self.runner.post().Move to the original directory.
It should be called after
self.prepare()is called.
post(self) -> DictRuns a post process of the algorithm, for example, writing the results into files.
Enters into
self.output_dir, callsself._post(), and returns to the original directory.It should be called after
self.run()is called.
main(self, run_mode) -> DictCalls
prepare,run, andpost.Measures the elapsed times for calling functions, and writes them into a file
Takes an argument for the execution mode as a string. The default value is
initialize."initialize": start from the initial state."resume": resume from the interrupted run."continue": continue from the finished run.
The argument contains
"-resetrand"when the random number generator should be initialized. The behavior of “continue” depends on the algorithm.Returns the result of the optimization in the form of dictionary.
Algorithm¶
Algorithm provides a concrete description of the algorithm.
It is defined as a subclass of AlgorithmBase and should have the following methods.
__init__(self, info: odatse.Info, runner: odatse.Runner = None, domain = None)The arguments
infoandrunnershould be transferred to the constructor of the base class:super().__init__(info=info, runner=runner)
Reads
infoand sets information.If
domainis given, the search region should be taken from thedomainparameter. Otherwise, the search region should be created frominfobyodatse.domain.Region(info)(for continuous parameter space) orodatse.domain.MeshGrid(info)(for discrete parameter space).
_prepare(self) -> NoneDescribes pre-processes of the algorithm.
_run(self) -> NoneDescribes the algorithm body.
In order to obtain the value of the objective function
f(x)for the search parameterx, the method of Runner class should be called in the following manner:args = (step, set) fx = self.runner.submit(x, args)
_post(self) -> DictDescribes post-process of the algorithm.
Returns the result of the optimization in the form of dictionary.
Definition of Domain¶
Two classes are preprared to specify the search region.
Region class¶
Region is a helper class to define a continuous parameter space.
The constructor takes an
Infoobject, or a dictionary inparam=form.When the
Infoobject is given, the lower and upper bounds of the region, the units, and the initial values are obtained fromInfo.algorithm.paramfield.When the dictionary is given, the corresponding data are taken from the dictionary data.
For details, see [algorithm.param] subsection for minsearch
Initialize(self, rnd, limitation, num_walkers)should be called to set the initial values. The arguments are the random number generatorrng, the constraint objectlimitation, and the number of walkersnum_walkers.
MeshGrid class¶
MeshGrid is a helper class to define a discrete parameter space.
The constructor takes an
Infoobject, or a dictionary inparam=form.When the
Infoobject is given, the lower and upper bounds of the region, the units, and the initial values are obtained fromInfo.algorithm.paramfield.When the dictionary is given, the corresponding data are taken from the dictionary data.
For details, see [algorithm.param] subsection for mapper
do_split(self)should be called to divide the grid points and distribute them to MPI ranks.For input and output, the following methods are provided.
A class method
from_file(cls, path)is prepared that reads mesh data frompathand creates an instance ofMeshGridclass.A method
store_file(self, path)is prepared that writes the grid information to the file specified bypath.