eckity.event_based_operator
This module implements the Operator class
1""" 2This module implements the Operator class 3""" 4 5from abc import ABC, abstractmethod 6from typing import Any 7 8from eckity.before_after_publisher import BeforeAfterPublisher 9 10 11class Operator(BeforeAfterPublisher, ABC): 12 def __init__(self, arity=1, events=None, event_names=None): 13 super().__init__(events=events, event_names=event_names) 14 self.applied_individuals = None 15 self.arity = arity 16 17 @abstractmethod 18 def apply_operator(self, payload): 19 pass 20 21 def initialize(self): 22 pass 23 24 def act(self, payload=None): 25 """ 26 Applies the subclass-specific operator on the given payload, 27 and publishing events before and after the operator execution 28 29 Parameters 30 ---------- 31 payload: 32 operands to apply the operator on 33 34 Returns 35 ------- 36 the return value of the operator implemented in the sub-class 37 """ 38 self._assert_arity(payload) 39 return self.act_and_publish_before_after( 40 lambda: self.apply_operator(payload) 41 ) 42 43 def _assert_arity(self, payload: Any) -> None: 44 if not isinstance(payload, list): 45 if self.arity != 1: 46 raise ValueError( 47 f"Received payload of type {type(payload)}, " 48 f"expected arity of 1 but got {self.arity}." 49 ) 50 51 elif (n_individuals := len(payload)) != self.arity: 52 raise ValueError( 53 f"Expected {self.arity} individuals," 54 f"but received {n_individuals}." 55 ) 56 57 def get_operator_arity(self): 58 """ 59 Getter method for the number of operands this operator is applied on 60 For example, a crossover that exchanges subtrees of 2 individuals will have an arity of 2 61 62 Returns 63 ------- 64 int 65 number of operands this operator is applied on 66 """ 67 return self.arity 68 69 def event_name_to_data(self, event_name): 70 if event_name == "after_operator": 71 return {"applied_individuals": self.applied_individuals} 72 if event_name == "before_operator": 73 return {} 74 else: 75 return {}
12class Operator(BeforeAfterPublisher, ABC): 13 def __init__(self, arity=1, events=None, event_names=None): 14 super().__init__(events=events, event_names=event_names) 15 self.applied_individuals = None 16 self.arity = arity 17 18 @abstractmethod 19 def apply_operator(self, payload): 20 pass 21 22 def initialize(self): 23 pass 24 25 def act(self, payload=None): 26 """ 27 Applies the subclass-specific operator on the given payload, 28 and publishing events before and after the operator execution 29 30 Parameters 31 ---------- 32 payload: 33 operands to apply the operator on 34 35 Returns 36 ------- 37 the return value of the operator implemented in the sub-class 38 """ 39 self._assert_arity(payload) 40 return self.act_and_publish_before_after( 41 lambda: self.apply_operator(payload) 42 ) 43 44 def _assert_arity(self, payload: Any) -> None: 45 if not isinstance(payload, list): 46 if self.arity != 1: 47 raise ValueError( 48 f"Received payload of type {type(payload)}, " 49 f"expected arity of 1 but got {self.arity}." 50 ) 51 52 elif (n_individuals := len(payload)) != self.arity: 53 raise ValueError( 54 f"Expected {self.arity} individuals," 55 f"but received {n_individuals}." 56 ) 57 58 def get_operator_arity(self): 59 """ 60 Getter method for the number of operands this operator is applied on 61 For example, a crossover that exchanges subtrees of 2 individuals will have an arity of 2 62 63 Returns 64 ------- 65 int 66 number of operands this operator is applied on 67 """ 68 return self.arity 69 70 def event_name_to_data(self, event_name): 71 if event_name == "after_operator": 72 return {"applied_individuals": self.applied_individuals} 73 if event_name == "before_operator": 74 return {} 75 else: 76 return {}
Helper class that provides a standard way to create an ABC using inheritance.
def
act(self, payload=None):
25 def act(self, payload=None): 26 """ 27 Applies the subclass-specific operator on the given payload, 28 and publishing events before and after the operator execution 29 30 Parameters 31 ---------- 32 payload: 33 operands to apply the operator on 34 35 Returns 36 ------- 37 the return value of the operator implemented in the sub-class 38 """ 39 self._assert_arity(payload) 40 return self.act_and_publish_before_after( 41 lambda: self.apply_operator(payload) 42 )
Applies the subclass-specific operator on the given payload, and publishing events before and after the operator execution
Parameters
- payload:: operands to apply the operator on
Returns
- the return value of the operator implemented in the sub-class
def
get_operator_arity(self):
58 def get_operator_arity(self): 59 """ 60 Getter method for the number of operands this operator is applied on 61 For example, a crossover that exchanges subtrees of 2 individuals will have an arity of 2 62 63 Returns 64 ------- 65 int 66 number of operands this operator is applied on 67 """ 68 return self.arity
Getter method for the number of operands this operator is applied on For example, a crossover that exchanges subtrees of 2 individuals will have an arity of 2
Returns
- int: number of operands this operator is applied on