eckity.genetic_operators.mutations.erc_mutation

 1from typing import Any, List, Tuple
 2
 3from overrides import override
 4import random
 5
 6from eckity.genetic_encodings.gp import Tree
 7from eckity.genetic_operators import FailableOperator
 8
 9
10class ERCMutation(FailableOperator):
11    def __init__(
12        self,
13        probability=1.0,
14        arity=1,
15        mu=0,
16        sigma=1,
17        events=None,
18    ):
19        super().__init__(probability=probability, arity=arity, events=events)
20        self.mu = mu
21        self.sigma = sigma
22
23    @override
24    def attempt_operator(
25        self, payload: Any, attempt_num: int
26    ) -> Tuple[bool, Any]:
27        """
28        Perform ephemeral random constant (ERC) mutation: select an ERC node at random
29        and add Gaussian noise to it.
30
31        Returns
32        -------
33        None.
34        """
35        individuals: List[Tree] = payload
36
37        leaves = [ind.get_random_numeric_node() for ind in individuals]
38
39        if None in leaves:
40            return False, individuals
41
42        mu, sigma = self.mu, self.sigma
43        for terminal in leaves:
44            terminal.value = terminal.value + random.gauss(mu, sigma)
45
46        self.applied_individuals = individuals
47        return True, individuals
11class ERCMutation(FailableOperator):
12    def __init__(
13        self,
14        probability=1.0,
15        arity=1,
16        mu=0,
17        sigma=1,
18        events=None,
19    ):
20        super().__init__(probability=probability, arity=arity, events=events)
21        self.mu = mu
22        self.sigma = sigma
23
24    @override
25    def attempt_operator(
26        self, payload: Any, attempt_num: int
27    ) -> Tuple[bool, Any]:
28        """
29        Perform ephemeral random constant (ERC) mutation: select an ERC node at random
30        and add Gaussian noise to it.
31
32        Returns
33        -------
34        None.
35        """
36        individuals: List[Tree] = payload
37
38        leaves = [ind.get_random_numeric_node() for ind in individuals]
39
40        if None in leaves:
41            return False, individuals
42
43        mu, sigma = self.mu, self.sigma
44        for terminal in leaves:
45            terminal.value = terminal.value + random.gauss(mu, sigma)
46
47        self.applied_individuals = individuals
48        return True, individuals

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
ERCMutation(probability=1.0, arity=1, mu=0, sigma=1, events=None)
12    def __init__(
13        self,
14        probability=1.0,
15        arity=1,
16        mu=0,
17        sigma=1,
18        events=None,
19    ):
20        super().__init__(probability=probability, arity=arity, events=events)
21        self.mu = mu
22        self.sigma = sigma
mu
sigma
@override
def attempt_operator(self, payload: Any, attempt_num: int) -> Tuple[bool, Any]:
24    @override
25    def attempt_operator(
26        self, payload: Any, attempt_num: int
27    ) -> Tuple[bool, Any]:
28        """
29        Perform ephemeral random constant (ERC) mutation: select an ERC node at random
30        and add Gaussian noise to it.
31
32        Returns
33        -------
34        None.
35        """
36        individuals: List[Tree] = payload
37
38        leaves = [ind.get_random_numeric_node() for ind in individuals]
39
40        if None in leaves:
41            return False, individuals
42
43        mu, sigma = self.mu, self.sigma
44        for terminal in leaves:
45            terminal.value = terminal.value + random.gauss(mu, sigma)
46
47        self.applied_individuals = individuals
48        return True, individuals

Perform ephemeral random constant (ERC) mutation: select an ERC node at random and add Gaussian noise to it.

Returns
  • None.