eckity.genetic_operators.failable_operator

 1from eckity.genetic_operators.genetic_operator import GeneticOperator
 2from abc import abstractmethod
 3
 4
 5class FailableOperator(GeneticOperator):
 6    """
 7    Genetic operator that has a chance of failing.
 8
 9    For example, adding a gaussian noise to a FloatVector cell might exceed the legal bounds of the vector.
10    In that case, the Gauss Mutation fails.
11
12    Parameters
13    -------
14    probability: float
15        the probability of the operator to be applied
16
17    arity: int
18        number of individuals to be applied on
19
20    events: list of strings
21        events to be published before, after and during the operator
22
23    attempts: int
24        number of attempts to be made during the operator execution
25    """
26    def __init__(self, probability=0.05, arity=0, events=None, attempts=1):
27        super().__init__(probability, arity, events)
28        if attempts <= 1:
29            raise ValueError("Number of attempts must be at least 1")
30        self.attempts = attempts
31
32    # TODO add event of on fail or on fail all retries
33    def apply(self, payload):
34        """
35        Apply the operator, with a chance of failing.
36
37        Attempt to apply the operator `attempts` times, finish by succeeding in one of the attempts or by failing
38        all attempts and executing `on_fail` method.
39
40        Parameters
41        -------
42        payload: object
43            relevant data for the applied operator (usually a list of individuals)
44        """
45        for i in range(self.attempts):
46            # attempt to execute the operator
47            succeeded, result = self.attempt_operator(payload, i)
48
49            # return if succeeded
50            if succeeded:
51                return result
52        # after all attempts failed, execute the `on_fail` mechanism
53        return self.on_fail(payload)
54
55    @abstractmethod
56    def attempt_operator(self, payload, attempt_num):
57        """
58        A single attempt of the operator
59
60        Parameters
61        -------
62        payload: object
63            relevant data for the applied operator (usually a list of individuals)
64
65        attempt_num: int
66            current attempt number
67
68        Returns
69        -------
70        (bool, object)
71            tuple of (succeeded or not, result value)
72        """
73        pass
74
75    @abstractmethod
76    def on_fail(self, payload):
77        """
78        What to do when all operator attempts failed
79
80        This method is called once all operator attempts have failed
81
82        Parameters
83        -------
84        payload: object
85            relevant data for the failure handling mechanism (usually a list of individuals)
86
87        Returns
88        -------
89        object
90            result value
91        """
92        pass
class FailableOperator(eckity.genetic_operators.genetic_operator.GeneticOperator):
 6class FailableOperator(GeneticOperator):
 7    """
 8    Genetic operator that has a chance of failing.
 9
10    For example, adding a gaussian noise to a FloatVector cell might exceed the legal bounds of the vector.
11    In that case, the Gauss Mutation fails.
12
13    Parameters
14    -------
15    probability: float
16        the probability of the operator to be applied
17
18    arity: int
19        number of individuals to be applied on
20
21    events: list of strings
22        events to be published before, after and during the operator
23
24    attempts: int
25        number of attempts to be made during the operator execution
26    """
27    def __init__(self, probability=0.05, arity=0, events=None, attempts=1):
28        super().__init__(probability, arity, events)
29        if attempts <= 1:
30            raise ValueError("Number of attempts must be at least 1")
31        self.attempts = attempts
32
33    # TODO add event of on fail or on fail all retries
34    def apply(self, payload):
35        """
36        Apply the operator, with a chance of failing.
37
38        Attempt to apply the operator `attempts` times, finish by succeeding in one of the attempts or by failing
39        all attempts and executing `on_fail` method.
40
41        Parameters
42        -------
43        payload: object
44            relevant data for the applied operator (usually a list of individuals)
45        """
46        for i in range(self.attempts):
47            # attempt to execute the operator
48            succeeded, result = self.attempt_operator(payload, i)
49
50            # return if succeeded
51            if succeeded:
52                return result
53        # after all attempts failed, execute the `on_fail` mechanism
54        return self.on_fail(payload)
55
56    @abstractmethod
57    def attempt_operator(self, payload, attempt_num):
58        """
59        A single attempt of the operator
60
61        Parameters
62        -------
63        payload: object
64            relevant data for the applied operator (usually a list of individuals)
65
66        attempt_num: int
67            current attempt number
68
69        Returns
70        -------
71        (bool, object)
72            tuple of (succeeded or not, result value)
73        """
74        pass
75
76    @abstractmethod
77    def on_fail(self, payload):
78        """
79        What to do when all operator attempts failed
80
81        This method is called once all operator attempts have failed
82
83        Parameters
84        -------
85        payload: object
86            relevant data for the failure handling mechanism (usually a list of individuals)
87
88        Returns
89        -------
90        object
91            result value
92        """
93        pass

Genetic operator that has a chance of failing.

For example, adding a gaussian noise to a FloatVector cell might exceed the legal bounds of the vector. In that case, the Gauss Mutation fails.

Parameters
  • probability (float): the probability of the operator to be applied
  • arity (int): number of individuals to be applied on
  • events (list of strings): events to be published before, after and during the operator
  • attempts (int): number of attempts to be made during the operator execution
FailableOperator(probability=0.05, arity=0, events=None, attempts=1)
27    def __init__(self, probability=0.05, arity=0, events=None, attempts=1):
28        super().__init__(probability, arity, events)
29        if attempts <= 1:
30            raise ValueError("Number of attempts must be at least 1")
31        self.attempts = attempts
attempts
def apply(self, payload):
34    def apply(self, payload):
35        """
36        Apply the operator, with a chance of failing.
37
38        Attempt to apply the operator `attempts` times, finish by succeeding in one of the attempts or by failing
39        all attempts and executing `on_fail` method.
40
41        Parameters
42        -------
43        payload: object
44            relevant data for the applied operator (usually a list of individuals)
45        """
46        for i in range(self.attempts):
47            # attempt to execute the operator
48            succeeded, result = self.attempt_operator(payload, i)
49
50            # return if succeeded
51            if succeeded:
52                return result
53        # after all attempts failed, execute the `on_fail` mechanism
54        return self.on_fail(payload)

Apply the operator, with a chance of failing.

Attempt to apply the operator attempts times, finish by succeeding in one of the attempts or by failing all attempts and executing on_fail method.

Parameters
  • payload (object): relevant data for the applied operator (usually a list of individuals)
@abstractmethod
def attempt_operator(self, payload, attempt_num):
56    @abstractmethod
57    def attempt_operator(self, payload, attempt_num):
58        """
59        A single attempt of the operator
60
61        Parameters
62        -------
63        payload: object
64            relevant data for the applied operator (usually a list of individuals)
65
66        attempt_num: int
67            current attempt number
68
69        Returns
70        -------
71        (bool, object)
72            tuple of (succeeded or not, result value)
73        """
74        pass

A single attempt of the operator

Parameters
  • payload (object): relevant data for the applied operator (usually a list of individuals)
  • attempt_num (int): current attempt number
Returns
  • (bool, object): tuple of (succeeded or not, result value)
@abstractmethod
def on_fail(self, payload):
76    @abstractmethod
77    def on_fail(self, payload):
78        """
79        What to do when all operator attempts failed
80
81        This method is called once all operator attempts have failed
82
83        Parameters
84        -------
85        payload: object
86            relevant data for the failure handling mechanism (usually a list of individuals)
87
88        Returns
89        -------
90        object
91            result value
92        """
93        pass

What to do when all operator attempts failed

This method is called once all operator attempts have failed

Parameters
  • payload (object): relevant data for the failure handling mechanism (usually a list of individuals)
Returns
  • object: result value