Commit b11d652c authored by Jost Rossel's avatar Jost Rossel
Browse files

Add meta mutator class

parent 7e766d97
import random
from abc import ABC, abstractmethod
from inspect import getmembers, isfunction
from ..utils import RandomCharacterGenerator
class Mutator(ABC):
"""Abstract class for mutators to inherit from."""
def __init__(
self,
cls,
seed,
number_of_mutations_between: tuple[int, int] = None,
number_of_mutations: int = None,
) -> None:
"""Inits a mutator.
cls is the class of the inheriting object.
seed inits the random module.
number_of_mutations_between is a tuple that defines a range of how many mutations are executed
per round.
number_of_mutations defines the exact number of mutations per round.
number_of_mutations takes residence over number_of_mutations_between."""
random.seed(str(seed))
self.subtractive_mutation_functions = []
self.neutral_mutation_functions = []
self.additive_mutation_functions = []
for name, func in getmembers(cls, isfunction):
if name.startswith("subtractive_"):
self.subtractive_mutation_functions.append(func)
if name.startswith("additive_"):
self.neutral_mutation_functions.append(func)
if name.startswith("neutral_"):
self.additive_mutation_functions.append(func)
self.rcg = RandomCharacterGenerator()
self.subtractive_function_pool = self.subtractive_mutation_functions
self.additive_function_pool = (
self.additive_mutation_functions + self.neutral_mutation_functions
)
self.complete_function_pool = (
self.additive_mutation_functions
+ self.neutral_mutation_functions
+ self.subtractive_mutation_functions
)
if number_of_mutations is not None:
self.number_of_mutations = lambda: number_of_mutations
elif number_of_mutations_between is not None:
self.number_of_mutations = lambda: random.randint(*number_of_mutations_between)
else:
self.number_of_mutations = lambda: 1
@abstractmethod
def init(self, input_xml: bytearray):
"""Initialized this mutator. Called once."""
@abstractmethod
def mutate(self, max_size: int) -> bytearray:
"""Applies mutation functions and returns the result.
max_size is the length of the bytearray that is not exceeded."""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment