Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
PasaOpasen committed Apr 13, 2024
1 parent 3c0aa64 commit d5eb20e
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions geneticalgorithm2/data_types/algorithm_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

@dataclass
class AlgorithmParams(DictLikeGetSet):
"""Base optimization parameters container"""

max_num_iteration: Optional[int] = None
max_iteration_without_improv: Optional[int] = None
Expand Down Expand Up @@ -71,8 +72,8 @@ def get_CMS_funcs(self) -> Tuple[
SelectionFunc
]:
"""
returns gotten crossover, mutation, discrete mutation, selection
as necessary functions
Returns:
gotten (crossover, mutation, discrete mutation, selection) as necessary functions
"""

result: List[Callable] = []
Expand All @@ -94,27 +95,20 @@ def get_CMS_funcs(self) -> Tuple[

return tuple(result)

def update(self, dct: Dict[str, Any]):
for name, value in dct.items():
if name not in _algorithm_params_slots:
raise AttributeError(
f"name '{name}' does not exists in AlgorithmParams fields: "
f"{', '.join(sorted(_algorithm_params_slots))}"
)
for name, value in dct.items(): # perform update in separate loop only if all is valid
setattr(self, name, value)

@staticmethod
def from_dict(dct: Dict[str, Any]):

result = AlgorithmParams()

for name, value in dct.items():
if name not in _algorithm_params_slots:
raise AttributeError(f"name '{name}' does not exists in AlgorithmParams fields")

setattr(result, name, value)
result.update(dct)
return result













0 comments on commit d5eb20e

Please sign in to comment.