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)
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))) update_progress(int((i + 1) / iterations * 100)) end = time.clock() print('\n{0} seconds'.format(end - start)) for result in sorted(results): print(result, sep='\n')
__author__ = 'Stanislav Ushakov' from immune import ExpressionsImmuneSystem, DataFileStorageHelper, ExpressionsImmuneSystemConfig from exchanger import PeerToPeerExchanger, LocalhostNodesManager import sys #start as "python node_main.py node_num number_of_nodes" if __name__ == '__main__': number = int(sys.argv[1]) number_of_nodes = int(sys.argv[2]) nodes_manager = LocalhostNodesManager(number, number_of_nodes) config = ExpressionsImmuneSystemConfig() config.number_of_lymphocytes = 200 config.number_of_iterations = 200 config.number_of_iterations_to_exchange = 30 config.maximal_height = 5 variables, values = DataFileStorageHelper.load_from_file('test_x_y.txt') exchanger = PeerToPeerExchanger(nodes_manager) results = [] immuneSystem = ExpressionsImmuneSystem(exact_values=values, variables=variables, exchanger=exchanger, config=config) best = immuneSystem.solve() print(best)