eckity.event_based_operator

This module implements the Operator class

 1"""
 2This module implements the Operator class
 3"""
 4
 5from abc import abstractmethod
 6
 7from eckity.before_after_publisher import BeforeAfterPublisher
 8
 9
10class Operator(BeforeAfterPublisher):
11    def __init__(self, arity=1, events=None, event_names=None):
12        super().__init__(events=events, event_names=event_names)
13        self.applied_individuals = None
14        self.arity = arity
15
16    @abstractmethod
17    def apply_operator(self, payload):
18        pass
19
20    def initialize(self):
21        pass
22
23    def act(self, payload=None):
24        """
25        Applies the subclass-specific operator on the given payload,
26        and publishing events before and after the operator execution
27
28        Parameters
29        ----------
30        payload:
31            operands to apply the operator on
32
33        Returns
34        -------
35        the return value of the operator implemented in the sub-class
36        """
37        return self.act_and_publish_before_after(lambda: self.apply_operator(payload))
38
39    def get_operator_arity(self):
40        """
41        Getter method for the number of operands this operator is applied on
42        For example, a crossover that exchanges subtrees of 2 individuals will have an arity of 2
43
44        Returns
45        -------
46        int
47            number of operands this operator is applied on
48        """
49        return self.arity
50
51    def event_name_to_data(self, event_name):
52        if event_name == "after_operator":
53            return {"applied_individuals": self.applied_individuals}
54        if event_name == "before_operator":
55            return {}
56        else:
57            return {}
11class Operator(BeforeAfterPublisher):
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        return self.act_and_publish_before_after(lambda: self.apply_operator(payload))
39
40    def get_operator_arity(self):
41        """
42        Getter method for the number of operands this operator is applied on
43        For example, a crossover that exchanges subtrees of 2 individuals will have an arity of 2
44
45        Returns
46        -------
47        int
48            number of operands this operator is applied on
49        """
50        return self.arity
51
52    def event_name_to_data(self, event_name):
53        if event_name == "after_operator":
54            return {"applied_individuals": self.applied_individuals}
55        if event_name == "before_operator":
56            return {}
57        else:
58            return {}
Operator(arity=1, events=None, event_names=None)
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
applied_individuals
arity
@abstractmethod
def apply_operator(self, payload):
17    @abstractmethod
18    def apply_operator(self, payload):
19        pass
def initialize(self):
21    def initialize(self):
22        pass
def act(self, payload=None):
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        return self.act_and_publish_before_after(lambda: self.apply_operator(payload))

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):
40    def get_operator_arity(self):
41        """
42        Getter method for the number of operands this operator is applied on
43        For example, a crossover that exchanges subtrees of 2 individuals will have an arity of 2
44
45        Returns
46        -------
47        int
48            number of operands this operator is applied on
49        """
50        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
def event_name_to_data(self, event_name):
52    def event_name_to_data(self, event_name):
53        if event_name == "after_operator":
54            return {"applied_individuals": self.applied_individuals}
55        if event_name == "before_operator":
56            return {}
57        else:
58            return {}