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
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
def test_solve_is_not_crashing(self): values = [] for i in range(0,5): x = i values.append(({'x': x}, x * x )) f = FitnessFunction(values) exchanger = SimpleRandomExchanger( lambda: [Expression.generate_random(max_height=2, variables=['x']) for i in range(0, 5)]) config = ExpressionsImmuneSystemConfig() config.number_of_lymphocytes = 10 config.number_of_iterations = 5 immuneSystem = ExpressionsImmuneSystem(exact_values=values, variables=['x'], exchanger=exchanger, config=config) best = immuneSystem.solve() self.assertGreaterEqual(f.expression_value(best), 0)
def __init__(self, exact_values, variables, exchanger, config): """ 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 __init__(self, exact_values, variables, exchanger, config): """ 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()
print('\r[{0}] {1}%'.format('#' * (progress // 10), progress), end='') if __name__ == "__main__": number_of_lymphocytes = 100 max_height = 4 DataFileStorageHelper.save_to_file( 'test_x_y.txt', ['x', 'y'], lambda x, y: x * x + x * y * math.sin(x * y), 100) variables, values = DataFileStorageHelper.load_from_file('test_x_y.txt') f = FitnessFunction(values) exchanger = SimpleRandomExchanger(lambda: [ Expression.generate_random(max_height=max_height, variables=variables) for i in range(0, number_of_lymphocytes // 2) ]) config = ExpressionsImmuneSystemConfig() results = [] iterations = 5 start = time.clock() for i in range(0, iterations): immuneSystem = ExpressionsImmuneSystem(exact_values=values, variables=variables, exchanger=exchanger, config=config) best = immuneSystem.solve() results.append((f.expression_value(best), str(best)))
Shows progress bar. Progress is passed in percent. """ print '\r[{0}] {1}%'.format('#' * (progress // 10), progress) if __name__ == "__main__": number_of_lymphocytes = 100 max_height = 4 DataFileStorageHelper.save_to_file('test_x_y.txt', ['x', 'y'], lambda x, y: x * x + x * y * math.sin(x * y), 100) variables, values = DataFileStorageHelper.load_from_file('test_x_y.txt') f = FitnessFunction(values) exchanger = SimpleRandomExchanger( lambda: [Expression.generate_random(max_height=max_height, variables=variables) for i in range(0, number_of_lymphocytes // 2)]) config = ExpressionsImmuneSystemConfig() results = [] iterations = 5 start = time.clock() for i in range(0, iterations): immuneSystem = ExpressionsImmuneSystem(exact_values=values, variables=variables, exchanger=exchanger, config=config) best = immuneSystem.solve() results.append((f(best), str(best))) update_progress(int((i + 1) / iterations * 100))