blackboxopt.evaluation
Evaluation (EvaluationSpecification, _EvaluationBase)
dataclass
An evaluated specification with a timestamp indicating the time of the evaluation, and a result dictionary for all objective values.
NOTE: NaN
is not allowed as an objective value, use None
instead.
Source code in blackboxopt/evaluation.py
class Evaluation(EvaluationSpecification, _EvaluationBase):
"""An evaluated specification with a timestamp indicating the time of the
evaluation, and a result dictionary for all objective values.
NOTE: `NaN` is not allowed as an objective value, use `None` instead.
"""
constraints: Optional[Dict[str, Optional[float]]] = field(
default=None,
metadata={
"Description": "For each constraint name the float value indicates "
+ "how much the constraint was satisfied, with negative values implying "
+ "a violated and positive values indicating a satisfied constraint."
},
)
finished_unixtime: float = field(
default_factory=_datetime_now_timestamp,
metadata={"Description": "Timestamp at completion of this evaluation."},
)
stacktrace: Optional[str] = field(
default=None,
metadata={
"Description": "The stacktrace in case an unhandled exception occurred "
+ "inside the evaluation function."
},
)
user_info: Optional[dict] = field(
default=None,
metadata={"Description": "Miscellaneous information provided by the user."},
)
def get_specification(
self, reset_created_unixtime: bool = False
) -> EvaluationSpecification:
"""Get the evaluation specifiation for which this result was evaluated."""
eval_spec_kwargs = deepcopy(
dict(
configuration=self.configuration,
settings=self.settings,
optimizer_info=self.optimizer_info,
context=self.context,
)
)
if reset_created_unixtime:
return EvaluationSpecification(
created_unixtime=_datetime_now_timestamp(), **eval_spec_kwargs
)
return EvaluationSpecification(
created_unixtime=self.created_unixtime, **eval_spec_kwargs
)
@property
def any_objective_none(self) -> bool:
return any([v is None for v in self.objectives.values()])
@property
def all_objectives_none(self) -> bool:
return all([v is None for v in self.objectives.values()])
create_evaluation(self, objectives, constraints=None, user_info=None, stacktrace=None, finished_unixtime=None)
inherited
Create a blackboxopt.Evaluation based on this evaluation specification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objectives |
Dict[str, Optional[float]] |
For each objective name the respective value. |
required |
constraints |
Optional[Dict[str, Union[float, NoneType]]] |
For each constraint name the float value indicates how much the constraint was satisfied, with negative values implying a violated and positive values indicating a satisfied constraint. |
None |
user_info |
Optional[dict] |
Miscellaneous information provided by the user. |
None |
stacktrace |
Optional[str] |
The stacktrace in case an unhandled exception occurred inside the evaluation function. |
None |
finished_unixtime |
Optional[float] |
Timestamp at completion of this evaluation. If none is provided, the current time is used. |
None |
Source code in blackboxopt/evaluation.py
def create_evaluation(
self,
objectives: Dict[str, Optional[float]],
constraints: Optional[Dict[str, Optional[float]]] = None,
user_info: Optional[dict] = None,
stacktrace: Optional[str] = None,
finished_unixtime: Optional[float] = None,
):
"""Create a blackboxopt.Evaluation based on this evaluation specification.
Args:
objectives: For each objective name the respective value.
constraints: For each constraint name the float value indicates how much the
constraint was satisfied, with negative values implying a violated and
positive values indicating a satisfied constraint.
user_info: Miscellaneous information provided by the user.
stacktrace: The stacktrace in case an unhandled exception occurred inside
the evaluation function.
finished_unixtime: Timestamp at completion of this evaluation. If none is
provided, the current time is used.
"""
evaluation = Evaluation(
objectives=objectives,
constraints=constraints,
user_info=user_info,
stacktrace=stacktrace,
**self,
)
# Data class default factories like in this case time.time are only triggered
# when the argument is not provided, so in case of it being None we can't just
# pass the argument value in, because it would set it to None instead of
# triggering the default factory for the current time.
if finished_unixtime is not None:
evaluation.finished_unixtime = finished_unixtime
return evaluation
get(self, key, default=None)
inherited
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
Source code in blackboxopt/evaluation.py
def get(self, key, default=None):
'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
try:
return self[key]
except KeyError:
return default
get_specification(self, reset_created_unixtime=False)
Get the evaluation specifiation for which this result was evaluated.
Source code in blackboxopt/evaluation.py
def get_specification(
self, reset_created_unixtime: bool = False
) -> EvaluationSpecification:
"""Get the evaluation specifiation for which this result was evaluated."""
eval_spec_kwargs = deepcopy(
dict(
configuration=self.configuration,
settings=self.settings,
optimizer_info=self.optimizer_info,
context=self.context,
)
)
if reset_created_unixtime:
return EvaluationSpecification(
created_unixtime=_datetime_now_timestamp(), **eval_spec_kwargs
)
return EvaluationSpecification(
created_unixtime=self.created_unixtime, **eval_spec_kwargs
)
items(self)
inherited
D.items() -> a set-like object providing a view on D's items
Source code in blackboxopt/evaluation.py
def items(self):
"D.items() -> a set-like object providing a view on D's items"
return ItemsView(self)
keys(self)
inherited
D.keys() -> a set-like object providing a view on D's keys
Source code in blackboxopt/evaluation.py
def keys(self):
return self.__dataclass_fields__.keys() # pylint: disable=no-member
values(self)
inherited
D.values() -> an object providing a view on D's values
Source code in blackboxopt/evaluation.py
def values(self):
"D.values() -> an object providing a view on D's values"
return ValuesView(self)
EvaluationSpecification (Mapping, Generic)
dataclass
EvaluationSpecification(args, *kwds)
Source code in blackboxopt/evaluation.py
class EvaluationSpecification(Mapping[str, Any]):
configuration: dict = field(
metadata={"Description": "The configuration to be evaluated next."}
)
settings: dict = field(
default_factory=dict,
metadata={
"Description": "Additional settings like the fidelity or target task."
},
)
optimizer_info: dict = field(
default_factory=dict,
metadata={"Description": "Information about and for internal optimizer state."},
)
created_unixtime: float = field(
default_factory=_datetime_now_timestamp,
metadata={"Description": "Creation time of the evaluation specificiation."},
)
context: Optional[Dict[str, Any]] = field(
default=None,
metadata={
"Description": "Contextual information is what you can determine but not "
+ "influence, like the environmental temperature."
},
)
def keys(self):
return self.__dataclass_fields__.keys() # pylint: disable=no-member
def create_evaluation(
self,
objectives: Dict[str, Optional[float]],
constraints: Optional[Dict[str, Optional[float]]] = None,
user_info: Optional[dict] = None,
stacktrace: Optional[str] = None,
finished_unixtime: Optional[float] = None,
):
"""Create a blackboxopt.Evaluation based on this evaluation specification.
Args:
objectives: For each objective name the respective value.
constraints: For each constraint name the float value indicates how much the
constraint was satisfied, with negative values implying a violated and
positive values indicating a satisfied constraint.
user_info: Miscellaneous information provided by the user.
stacktrace: The stacktrace in case an unhandled exception occurred inside
the evaluation function.
finished_unixtime: Timestamp at completion of this evaluation. If none is
provided, the current time is used.
"""
evaluation = Evaluation(
objectives=objectives,
constraints=constraints,
user_info=user_info,
stacktrace=stacktrace,
**self,
)
# Data class default factories like in this case time.time are only triggered
# when the argument is not provided, so in case of it being None we can't just
# pass the argument value in, because it would set it to None instead of
# triggering the default factory for the current time.
if finished_unixtime is not None:
evaluation.finished_unixtime = finished_unixtime
return evaluation
def __getitem__(self, key):
if key not in self.__dataclass_fields__: # pylint: disable=no-member
raise KeyError(
f"Only dataclass fields are accessible via __getitem__, '{key}' is not."
)
return deepcopy(getattr(self, key))
def __iter__(self):
return self.__dataclass_fields__.__iter__ # pylint: disable=no-member
def __len__(self):
return self.__dataclass_fields__.__len__ # pylint: disable=no-member
def to_json(self, **json_dump_kwargs):
return json.dumps(asdict(self), **json_dump_kwargs)
def to_dict(self):
return self.__dict__
create_evaluation(self, objectives, constraints=None, user_info=None, stacktrace=None, finished_unixtime=None)
Create a blackboxopt.Evaluation based on this evaluation specification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objectives |
Dict[str, Optional[float]] |
For each objective name the respective value. |
required |
constraints |
Optional[Dict[str, Union[float, NoneType]]] |
For each constraint name the float value indicates how much the constraint was satisfied, with negative values implying a violated and positive values indicating a satisfied constraint. |
None |
user_info |
Optional[dict] |
Miscellaneous information provided by the user. |
None |
stacktrace |
Optional[str] |
The stacktrace in case an unhandled exception occurred inside the evaluation function. |
None |
finished_unixtime |
Optional[float] |
Timestamp at completion of this evaluation. If none is provided, the current time is used. |
None |
Source code in blackboxopt/evaluation.py
def create_evaluation(
self,
objectives: Dict[str, Optional[float]],
constraints: Optional[Dict[str, Optional[float]]] = None,
user_info: Optional[dict] = None,
stacktrace: Optional[str] = None,
finished_unixtime: Optional[float] = None,
):
"""Create a blackboxopt.Evaluation based on this evaluation specification.
Args:
objectives: For each objective name the respective value.
constraints: For each constraint name the float value indicates how much the
constraint was satisfied, with negative values implying a violated and
positive values indicating a satisfied constraint.
user_info: Miscellaneous information provided by the user.
stacktrace: The stacktrace in case an unhandled exception occurred inside
the evaluation function.
finished_unixtime: Timestamp at completion of this evaluation. If none is
provided, the current time is used.
"""
evaluation = Evaluation(
objectives=objectives,
constraints=constraints,
user_info=user_info,
stacktrace=stacktrace,
**self,
)
# Data class default factories like in this case time.time are only triggered
# when the argument is not provided, so in case of it being None we can't just
# pass the argument value in, because it would set it to None instead of
# triggering the default factory for the current time.
if finished_unixtime is not None:
evaluation.finished_unixtime = finished_unixtime
return evaluation
get(self, key, default=None)
inherited
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
Source code in blackboxopt/evaluation.py
def get(self, key, default=None):
'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
try:
return self[key]
except KeyError:
return default
items(self)
inherited
D.items() -> a set-like object providing a view on D's items
Source code in blackboxopt/evaluation.py
def items(self):
"D.items() -> a set-like object providing a view on D's items"
return ItemsView(self)
keys(self)
D.keys() -> a set-like object providing a view on D's keys
Source code in blackboxopt/evaluation.py
def keys(self):
return self.__dataclass_fields__.keys() # pylint: disable=no-member
values(self)
inherited
D.values() -> an object providing a view on D's values
Source code in blackboxopt/evaluation.py
def values(self):
"D.values() -> an object providing a view on D's values"
return ValuesView(self)