Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
lambda: [Expression.generate_random(max_height=max_height, variables=variables)
for i in range(0, number_of_lymphocytes // 2)])
lambda: [Expression.generate_random(max_height=2, variables=['x'])
for i in range(0, 5)])
"""
Initializes the immune system with the exact_values, list of variables,
exchanger object and config object.
lymphocytes - list that stores current value of the whole system.
"""
self.exact_values = exact_values
self.variables = variables
self.fitness_function = FitnessFunction(exact_values)
self.exchanger = exchanger
#config
self.config = config
self.lymphocytes = []
for i in range(0, self.config.number_of_lymphocytes):
self.lymphocytes.append(Expression.generate_random(
self.config.maximal_height,
variables))
#Initialize Exchanger with the first generated lymphocytes
self.exchanger.set_lymphocytes_to_exchange(self.lymphocytes[:])
random.seed()
def subtree_mutation(self):
"""
Changes one randomly selected node to the randomly generated subtree.
The height of the tree isn't changed.
"""
nodes = self._get_all_nodes_by_filter(lambda n: n.height() > 1 and
n != self.expression.root)
if not nodes: return
selected_node = random.choice(nodes)
max_height = self.expression.root.height() - selected_node.height()
new_subtree = Expression.generate_random(max_height, self.expression.variables)
selected_node.operation = new_subtree.root.operation
selected_node.value = new_subtree.root.value
selected_node.left = new_subtree.root.left
selected_node.right = new_subtree.root.right