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