Skip to content

Commit

Permalink
using new version of pycommons and moved path_to_file to PerRunData
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasWeise committed Oct 22, 2024
1 parent d8cc11d commit b1875ce
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 50 deletions.
2 changes: 1 addition & 1 deletion moptipy/api/mo_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def check_mo_problem(mo_problem: Any) -> MOProblem:
... except TypeError as te:
... print(te)
multi-objective optimziation problem should be an instance of moptipy.\
api.mo_problem.MOProblem but is int, namely '1'.
api.mo_problem.MOProblem but is int, namely 1.
>>> try:
... check_mo_problem(None)
... except TypeError as te:
Expand Down
2 changes: 1 addition & 1 deletion moptipy/api/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def check_op1_with_step_size(op1: Any) -> Op1WithStepSize:
... except TypeError as te:
... print(te)
op1 should be an instance of moptipy.api.operators.Op1WithStepSize \
but is moptipy.api.operators.Op1, namely 'Op1'.
but is moptipy.api.operators.Op1.
>>> try:
... check_op1_with_step_size(None)
... except TypeError as te:
Expand Down
52 changes: 36 additions & 16 deletions moptipy/evaluation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
from dataclasses import dataclass
from typing import Any, Callable, Final

from pycommons.io.path import Path
from pycommons.types import check_int_range, type_error

from moptipy.api.logging import FILE_SUFFIX
from moptipy.utils.nputils import rand_seed_check
from moptipy.utils.strings import sanitize_name
from moptipy.utils.strings import (
sanitize_name,
sanitize_names,
)
from moptipy.version import __version__ as moptipy_version

#: The key for the total number of runs.
Expand Down Expand Up @@ -44,7 +49,7 @@ def check_time_unit(time_unit: Any) -> str:
... check_time_unit(1)
... except TypeError as te:
... print(te)
time_unit should be an instance of str but is int, namely '1'.
time_unit should be an instance of str but is int, namely 1.
>>> try:
... check_time_unit("blabedibla")
... except ValueError as ve:
Expand Down Expand Up @@ -77,7 +82,7 @@ def check_f_name(f_name: Any) -> str:
... check_f_name(1.0)
... except TypeError as te:
... print(te)
f_name should be an instance of str but is float, namely '1.0'.
f_name should be an instance of str but is float, namely 1.0.
>>> try:
... check_f_name("oops")
... except ValueError as ve:
Expand Down Expand Up @@ -128,7 +133,7 @@ def _set_name(dest: object, name: str, what: str,
... _set_name(t, 1, "algorithm")
... except TypeError as te:
... print(te)
algorithm name should be an instance of str but is int, namely '1'.
algorithm name should be an instance of str but is int, namely 1.
>>> t.algorithm
'bla'
>>> try:
Expand Down Expand Up @@ -433,7 +438,7 @@ class PerRunData(EvaluationDataElement):
... PerRunData(3, "i", "f", "e", 234)
... except TypeError as te:
... print(te)
algorithm name should be an instance of str but is int, namely '3'.
algorithm name should be an instance of str but is int, namely 3.
>>> try:
... PerRunData("@1 2", "i", "f", "e", 234)
... except ValueError as ve:
Expand All @@ -443,7 +448,7 @@ class PerRunData(EvaluationDataElement):
... PerRunData("x", 3.2, "f", "e", 234)
... except TypeError as te:
... print(te)
instance name should be an instance of str but is float, namely '3.2'.
instance name should be an instance of str but is float, namely 3.2.
>>> try:
... PerRunData("x", "sdf i", "f", "e", 234)
... except ValueError as ve:
Expand All @@ -453,7 +458,7 @@ class PerRunData(EvaluationDataElement):
... PerRunData("a", "i", True, "e", 234)
... except TypeError as te:
... print(te)
objective name should be an instance of str but is bool, namely 'True'.
objective name should be an instance of str but is bool, namely True.
>>> try:
... PerRunData("x", "i", "d-f", "e", 234)
... except ValueError as ve:
Expand All @@ -464,7 +469,7 @@ class PerRunData(EvaluationDataElement):
... except TypeError as te:
... print(te)
encoding name should be an instance of any in {None, str} but is float, \
namely '54.2'.
namely 54.2.
>>> try:
... PerRunData("y", "i", "f", "x x", 234)
... except ValueError as ve:
Expand All @@ -474,7 +479,7 @@ class PerRunData(EvaluationDataElement):
... PerRunData("x", "i", "f", "e", 3.3)
... except TypeError as te:
... print(te)
rand_seed should be an instance of int but is float, namely '3.3'.
rand_seed should be an instance of int but is float, namely 3.3.
>>> try:
... PerRunData("x", "i", "f", "e", -234)
... except ValueError as ve:
Expand Down Expand Up @@ -527,6 +532,21 @@ def _tuple(self) -> tuple[Any, ...]:
"" if self.encoding is None else self.encoding, 1,
self.rand_seed)

def path_to_file(self, base_dir: str) -> Path:
"""
Get the path that would correspond to the log file of this end result.
Obtain a path that would correspond to the log file of this end
result, resolved from a base directory `base_dir`.
:param base_dir: the base directory
:returns: the path to a file corresponding to the end result record
"""
return Path(base_dir).resolve_inside(
self.algorithm).resolve_inside(self.instance).resolve_inside(
sanitize_names([self.algorithm, self.instance,
hex(self.rand_seed)]) + FILE_SUFFIX)


@dataclass(frozen=True, init=False, order=False, eq=False)
class MultiRunData(EvaluationDataElement):
Expand Down Expand Up @@ -564,7 +584,7 @@ class MultiRunData(EvaluationDataElement):
... except TypeError as te:
... print(te)
algorithm name should be an instance of any in {None, str} but is int, \
namely '1'.
namely 1.
>>> try:
... MultiRunData("x x", "i", "f", "e", 234)
... except ValueError as ve:
Expand All @@ -575,7 +595,7 @@ class MultiRunData(EvaluationDataElement):
... except TypeError as te:
... print(te)
instance name should be an instance of any in {None, str} but is float, \
namely '5.5'.
namely 5.5.
>>> try:
... MultiRunData("x", "a-i", "f", "e", 234)
... except ValueError as ve:
Expand All @@ -586,7 +606,7 @@ class MultiRunData(EvaluationDataElement):
... except TypeError as te:
... print(te)
objective name should be an instance of any in {None, str} but is bool, \
namely 'True'.
namely True.
>>> try:
... MultiRunData("xx", "i", "d'@f", "e", 234)
... except ValueError as ve:
Expand All @@ -597,7 +617,7 @@ class MultiRunData(EvaluationDataElement):
... except TypeError as te:
... print(te)
encoding name should be an instance of any in {None, str} but is float, \
namely '-9.4'.
namely -9.4.
>>> try:
... MultiRunData("xx", "i", "f", "e-{a", 234)
... except ValueError as ve:
Expand All @@ -607,7 +627,7 @@ class MultiRunData(EvaluationDataElement):
... MultiRunData("x", "i", "f", "e", -1.234)
... except TypeError as te:
... print(te)
n should be an instance of int but is float, namely '-1.234'.
n should be an instance of int but is float, namely -1.234.
>>> try:
... MultiRunData("xx", "i", "f", "e", 1_000_000_000_000_000_000_000)
... except ValueError as ve:
Expand Down Expand Up @@ -698,7 +718,7 @@ class MultiRun2DData(MultiRunData):
... 3, F_NAME_SCALED)
... except TypeError as te:
... print(te)
time_unit should be an instance of str but is int, namely '3'.
time_unit should be an instance of str but is int, namely 3.
>>> try:
... MultiRun2DData("a", "i", "f", None, 3,
... "sdfjsdf", F_NAME_SCALED)
Expand All @@ -710,7 +730,7 @@ class MultiRun2DData(MultiRunData):
... TIME_UNIT_FES, True)
... except TypeError as te:
... print(te)
f_name should be an instance of str but is bool, namely 'True'.
f_name should be an instance of str but is bool, namely True.
>>> try:
... MultiRun2DData("a", "i", "f", None, 3,
... TIME_UNIT_FES, "blablue")
Expand Down
19 changes: 0 additions & 19 deletions moptipy/evaluation/end_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
)

from moptipy.api.logging import (
FILE_SUFFIX,
KEY_ALGORITHM,
KEY_BEST_F,
KEY_GOAL_F,
Expand Down Expand Up @@ -81,9 +80,6 @@
from moptipy.evaluation.log_parser import SetupAndStateParser
from moptipy.utils.help import moptipy_argparser
from moptipy.utils.math import try_float_div, try_int, try_int_div
from moptipy.utils.strings import (
sanitize_names,
)

#: a description of the random seed
DESC_RAND_SEED: Final[str] = (
Expand Down Expand Up @@ -277,21 +273,6 @@ def success(self) -> bool:
"""
return False if self.goal_f is None else self.best_f <= self.goal_f

def path_to_file(self, base_dir: str) -> Path:
"""
Get the path that would correspond to the log file of this end result.
Obtain a path that would correspond to the log file of this end
result, resolved from a base directory `base_dir`.
:param base_dir: the base directory
:returns: the path to a file corresponding to the end result record
"""
return Path(base_dir).resolve_inside(
self.algorithm).resolve_inside(self.instance).resolve_inside(
sanitize_names([self.algorithm, self.instance,
hex(self.rand_seed)]) + FILE_SUFFIX)

def get_best_f(self) -> int | float:
"""
Get the best objective value reached.
Expand Down
6 changes: 3 additions & 3 deletions moptipy/evaluation/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,20 @@ def select_consistent(data: Iterable[T], log: bool = True) -> list[T]:
... select_consistent(1)
... except TypeError as te:
... print(te)
data should be an instance of typing.Iterable but is int, namely '1'.
data should be an instance of typing.Iterable but is int, namely 1.
>>> try:
... select_consistent((a2i1o1e1s2, a2i1o1e1s3), 3)
... except TypeError as te:
... print(te)
log should be an instance of bool but is int, namely '3'.
log should be an instance of bool but is int, namely 3.
>>> try:
... select_consistent({234})
... except TypeError as te:
... print(te)
dataElement should be an instance of moptipy.evaluation.base.PerRunData \
but is int, namely '234'.
but is int, namely 234.
"""
if not isinstance(data, Iterable):
raise type_error(data, "data", Iterable)
Expand Down
12 changes: 6 additions & 6 deletions moptipy/utils/nputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def int_range_to_dtype(min_value: int, max_value: int,
... int_range_to_dtype(-1.0, (2 ** 64) - 1)
... except TypeError as e:
... print(e)
min_value should be an instance of int but is float, namely '-1.0'.
min_value should be an instance of int but is float, namely -1.0.
>>> try:
... int_range_to_dtype(-1, 'a')
... except TypeError as e:
Expand Down Expand Up @@ -291,18 +291,18 @@ def dtype_for_data(always_int: bool,
... except TypeError as v:
... print(v)
finite lower_bound of always_int should be an instance of int but is \
float, namely '1.0'.
float, namely 1.0.
>>> try:
... dtype_for_data(True, 0, 2.0)
... except TypeError as v:
... print(v)
finite upper_bound of always_int should be an instance of int but is \
float, namely '2.0'.
float, namely 2.0.
>>> try:
... dtype_for_data(3, 0, 2)
... except TypeError as v:
... print(v)
always_int should be an instance of bool but is int, namely '3'.
always_int should be an instance of bool but is int, namely 3.
"""
if not isinstance(always_int, bool):
raise type_error(always_int, "always_int", bool)
Expand Down Expand Up @@ -423,7 +423,7 @@ def rand_seed_check(rand_seed: Any) -> int:
... rand_seed_check(1.2)
... except TypeError as te:
... print(te)
rand_seed should be an instance of int but is float, namely '1.2'.
rand_seed should be an instance of int but is float, namely 1.2.
"""
return check_int_range(rand_seed, "rand_seed",
__MIN_RAND_SEED, __MAX_RAND_SEED)
Expand Down Expand Up @@ -608,7 +608,7 @@ def np_to_py_number(number: Any) -> int | float:
... except TypeError as te:
... print(te)
number should be an instance of any in {float, int, numpy.floating, \
numpy.integer} but is numpy.complex64, namely '(1+0j)'.
numpy.integer} but is numpy.complex64.
"""
if isinstance(number, int):
return number
Expand Down
2 changes: 1 addition & 1 deletion moptipy/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from typing import Final

#: the version string of `moptipy`
__version__: Final[str] = "0.9.128"
__version__: Final[str] = "0.9.129"
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
#

# pycommons provides lots of utilities
pycommons[dev] == 0.8.52
pycommons[dev] == 0.8.54
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ psutil == 6.0.0

# pycommons offers many of the tools and utilities used in moptipy that are
# not related to optimization.
pycommons == 0.8.52
pycommons == 0.8.54

# scikit-learn is used to obtain some clusters of JSSP instances for our
# experiments.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ install_requires =
matplotlib >= 3.9.2
pdfo >= 2.2.0
psutil >= 6.0.0
pycommons >= 0.8.52
pycommons >= 0.8.54
scikit-learn >= 1.5.2
scipy >= 1.14.1
packages = find:
Expand Down

0 comments on commit b1875ce

Please sign in to comment.