def test_evaluate_explicit(): print("-----test_evaluate_explicit-----") n_lin = int(math.pow(500, 1.0 / 3)) + 1 x_1 = np.linspace(0, 5, n_lin) x_2 = np.linspace(0, 5, n_lin) x_3 = np.linspace(0, 5, n_lin) x = np.array(np.meshgrid(x_1, x_2, x_3)).T.reshape(-1, 3) x = x[np.random.choice(x.shape[0], 500, replace=False), :] # make solution y_t = (x[:, 0] * x[:, 0] + 3.5 * x[:, 1]) y = y_t.reshape(-1, 1) py_training_data = ExplicitTrainingData(x, y) py_explicit_regressor = StandardRegression() c_explicit_regressor = bingocpp.StandardRegression() c_training_data = bingocpp.ExplicitTrainingData(x, y) py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) py_1 = py_manip.generate() c_1 = c_manip.generate() c_1.stack = np.copy(py_1.command_array) c_manip.simplify_stack(c_1) py_fit = py_explicit_regressor.evaluate_fitness(py_1, py_training_data) c_fit = c_explicit_regressor.evaluate_fitness(c_1, c_training_data) assert py_fit == pytest.approx(c_fit)
def test_dump(): print("-----test_dump-----") constants = np.array([1, 5, 6, 7, 8, 32, 54, 68]) py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) py_1 = py_manip.generate() py_1.constants = np.copy(constants) orig_stack = np.copy(py_1.command_array) orig_const = np.copy(py_1.constants) orig_age = py_1.genetic_age c_1 = c_manip.generate() c_1.stack = np.copy(orig_stack) c_1.constants = np.copy(orig_const) c_manip.simplify_stack(c_1) py_stack, py_con, py_age = py_manip.dump(py_1) c_pair, c_age = c_manip.dump(c_1) c_stack = c_pair[0] c_con = c_pair[1] assert py_stack.all() == c_stack.all() assert py_con.all() == c_con.all() assert py_age == c_age
def test_count_constants(): print("-----test_count_constants-----") py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) py_1 = py_manip.generate() c_1 = c_manip.generate() py_1.command_array[0] = (0, 0, 0) py_1.command_array[1] = (1, 0, 0) py_1.command_array[1] = (1, 1, 1) py_1.command_array[-1] = (2, 2, 1) c_1.stack = np.copy(py_1.command_array) c_manip.simplify_stack(c_1) constants = np.array([1, 5, 6, 7, 8, 32, 54, 68]) py_1.set_constants(constants) c_1.set_constants(constants) py_con = py_1.count_constants() c_con = c_1.count_constants() assert py_con == c_con
def test_evaluate_fitness_vector_implicit(): print("-----test_evaluate_fitness_vector_implicit-----") x_t = snake_walk() y = (x_t[:, 0] + x_t[:, 1]) x = np.hstack((x_t, y.reshape([-1, 1]))) py_training_data = ImplicitTrainingData(x) py_implicit_regressor = ImplicitRegression() c_training_data = bingocpp.ImplicitTrainingData(x) c_implicit_regressor = bingocpp.ImplicitRegression() py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) temp = np.array([[1, 0, 0], [0, 0, 0], [0, 1, 1], [3, 2, 2], [4, 2, 3], [4, 4, 0], [3, 2, 0], [3, 0, 0], [3, 4, 2], [3, 1, 4], [2, 6, 0], [3, 8, 7], [2, 3, 11], [3, 7, 7], [3, 2, 7], [2, 9, 5], [4, 4, 14], [3, 9, 1], [3, 4, 5], [0, 1, 1], [4, 8, 4], [1, -1, -1], [3, 20, 5], [2, 9, 17], [1, -1, -1], [4, 23, 4], [4, 25, 19], [2, 26, 14], [4, 22, 10], [0, 0, 0], [2, 0, 17], [2, 29, 16], [4, 23, 14], [4, 3, 22], [0, 2, 2], [2, 31, 27], [2, 35, 28], [2, 25, 29], [2, 36, 28], [3, 8, 29], [3, 4, 24], [2, 14, 9], [4, 25, 9], [0, 2, 2], [3, 26, 10], [3, 12, 6], [0, 1, 1], [4, 42, 26], [3, 41, 6], [3, 13, 1], [3, 42, 36], [3, 15, 34], [2, 14, 23], [2, 13, 12], [0, 2, 2], [2, 28, 45], [3, 1, 12], [3, 15, 30], [3, 34, 38], [3, 43, 50], [2, 31, 9], [2, 54, 46], [1, -1, -1], [4, 60, 29]]) py_1 = py_manip.generate() c_1 = c_manip.generate() py_1.command_array = np.copy(temp) c_1.stack = np.copy(temp) c_manip.simplify_stack(c_1) assert py_training_data.x.all() == pytest.approx(c_training_data.x.all()) assert py_training_data.dx_dt.all() == pytest.approx( c_training_data.dx_dt.all()) py_fit = py_implicit_regressor.evaluate_fitness(py_1, py_training_data) py_fit = py_implicit_regressor.evaluate_fitness_vector( py_1, py_training_data) c_fit = c_implicit_regressor.evaluate_fitness(c_1, c_training_data) c_fit = c_implicit_regressor.evaluate_fitness_vector(c_1, c_training_data) assert py_fit.all() == pytest.approx(c_fit.all())
def test_agcpp_evaluate_deriv(): print("-----test_agcpp_evaluate_deriv-----") n_lin = int(math.pow(500, 1.0 / 3)) + 1 x_1 = np.linspace(0, 5, n_lin) x_2 = np.linspace(0, 5, n_lin) x_3 = np.linspace(0, 5, n_lin) x = np.array(np.meshgrid(x_1, x_2, x_3)).T.reshape(-1, 3) x = x[np.random.choice(x.shape[0], 500, replace=False), :] py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) py_1 = py_manip.generate() c_1 = c_manip.generate() py_1.command_array[0] = (0, 0, 0) py_1.command_array[1] = (0, 1, 1) py_1.command_array[2] = (1, 0, 0) py_1.command_array[3] = (1, 1, 1) py_1.command_array[4] = (2, 3, 1) py_1.command_array[-1] = (2, 4, 2) c_1.stack = np.copy(py_1.command_array) c_manip.simplify_stack(c_1) constants = np.array([1, 5, 6, 7, 8, 32, 54, 68]) py_1.set_constants(constants) c_1.set_constants(constants) py_fit = py_1.evaluate_deriv(x) c_fit = c_1.evaluate_deriv(x) py_fit_const = py_1.evaluate_with_const_deriv(x) c_fit_const = c_1.evaluate_with_const_deriv(x) assert py_fit[0].all() == pytest.approx(c_fit[0].all()) assert py_fit[1].all() == pytest.approx(c_fit[1].all()) assert py_fit_const[0].all() == pytest.approx(c_fit_const[0].all()) assert py_fit_const[1].all() == pytest.approx(c_fit_const[1].all())
def test_complexity(): print("-----test_complexity-----") py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) py_1 = py_manip.generate() c_1 = c_manip.generate() c_1.stack = np.copy(py_1.command_array) c_manip.simplify_stack(c_1) py_complexity = py_1.complexity() c_complexity = c_1.complexity() assert py_complexity == c_complexity
def test_load(): print("-----test_load-----") constants = np.array([1, 5, 6, 7, 8, 32, 54, 68]) py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) py_1 = py_manip.generate() orig_stack = np.copy(py_1.command_array) py_1 = py_manip.load([orig_stack, constants, 0]) c_1 = c_manip.load([[orig_stack, constants], 0]) assert py_1.command_array.all() == c_1.stack.all() assert py_1.constants.all() == c_1.constants.all() assert py_1.genetic_age == c_1.genetic_age
def test_needs_optimization(): print("-----test_needs_optimization-----") py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) py_1 = py_manip.generate() c_1 = c_manip.generate() py_1.command_array[0] = (0, 0, 0) py_1.command_array[1] = (1, -1, -1) py_1.command_array[-1] = (2, 1, 0) c_1.stack = np.copy(py_1.command_array) c_manip.simplify_stack(c_1) py_opt = py_1.needs_optimization() c_opt = c_1.needs_optimization() assert py_opt == c_opt
def test_distance(): print("-----test_distance-----") py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2) py_manip.add_node_type(2) py_manip.add_node_type(3) py_manip.add_node_type(4) c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2) c_manip.add_node_type(2) c_manip.add_node_type(3) c_manip.add_node_type(4) py_1 = py_manip.generate() py_2 = py_manip.generate() c_1 = c_manip.generate() c_2 = c_manip.generate() c_1.stack = np.copy(py_1.command_array) c_2.stack = np.copy(py_2.command_array) c_manip.simplify_stack(c_1) c_manip.simplify_stack(c_2) py_dist = py_manip.distance(py_1, py_2) c_dist = c_manip.distance(c_1, c_2) assert py_dist == c_dist
def main(max_steps, epsilon, data_size, n_islands): """main regression function""" # -------------------------------------------- # MAKE DATA (uncomment one of the below lines) # -------------------------------------------- # data for standard regression # (use with ExplicitTrainingData and StandardRegression) # x_true, y_true = make_1d_data(data_size, 1) x_true, y_true = make_1d_data(data_size, 2) # x_true, y_true = make_1d_data(data_size, 3) # x_true, y_true = make_1d_data(data_size, 4) # x_true, y_true = make_norm_data(data_size) # data for implicit regression # (use with ImplicitTrainingData and ImplicitRegression) # x_true = make_circle_data(data_size) # ------------------------------------------------ # MAKE TRAINING DATA (uncomment one of the below) # ------------------------------------------------ training_data = ExplicitTrainingData(x_true, y_true) # training_data = ImplicitTrainingData(x_true) # ------------------------------------------------------------ # MAKE SOLUTION MANIPULATOR (uncomment one of the below blocks) # ------------------------------------------------------------ # using AGraph.py for the solution manipulator # sol_manip = agm(x_true.shape[1], 32, nloads=2) # sol_manip.add_node_type(AGNodes.Add) # sol_manip.add_node_type(AGNodes.Subtract) # sol_manip.add_node_type(AGNodes.Multiply) # sol_manip.add_node_type(AGNodes.Divide) # # sol_manip.add_node_type(AGNodes.Exp) # # sol_manip.add_node_type(AGNodes.Log) # # sol_manip.add_node_type(AGNodes.Sin) # # sol_manip.add_node_type(AGNodes.Cos) # # sol_manip.add_node_type(AGNodes.Abs) # sol_manip.add_node_type(AGNodes.Sqrt) # using AGraphCpp.py for the solution manipulator sol_manip = AGraphCpp.AGraphCppManipulator(x_true.shape[1], 32, nloads=2) sol_manip.add_node_type(2) # + sol_manip.add_node_type(3) # - sol_manip.add_node_type(4) # * sol_manip.add_node_type(5) # / sol_manip.add_node_type(12) # sqrt # ---------------------------------- # MAKE FITNESS PREDICTOR MANIPULATOR # ---------------------------------- pred_manip = fpm(128, data_size) # ------------------------------------------------ # MAKE FITNESS METRIC (uncomment one of the below) # ------------------------------------------------ regressor = StandardRegression() # regressor = ImplicitRegression() # -------------------------------------- # MAKE SERIAL ISLAND MANAGER THEN RUN IT # -------------------------------------- islmngr = SerialIslandManager(n_islands, solution_training_data=training_data, solution_manipulator=sol_manip, predictor_manipulator=pred_manip, solution_pop_size=64, fitness_metric=regressor, solution_age_fitness=True ) islmngr.run_islands(max_steps, epsilon, step_increment=100)
def main(max_steps, epsilon, data_size): """main function which runs regression""" comm = MPI.COMM_WORLD rank = comm.Get_rank() # load data on rank 0 and send it to all ranks x_true = None y_true = None if rank == 0: # -------------------------------------------- # MAKE DATA (uncomment one of the below lines) # -------------------------------------------- # standard regression # (use with ExplicitTrainingData and StandardRegression) # x_true, y_true = make_1d_data(data_size, 1) x_true, y_true = make_1d_data(data_size, 2) # x_true, y_true = make_1d_data(data_size, 3) # x_true, y_true = make_1d_data(data_size, 4) # x_true, y_true = make_norm_data(data_size) # implicit regression # (use with ImplicitTrainingData and ImplicitRegression) # x_true = make_circle_data(data_size) # then broadcast to all ranks x_true = MPI.COMM_WORLD.bcast(x_true, root=0) y_true = MPI.COMM_WORLD.bcast(y_true, root=0) # ------------------------------------------------ # MAKE TRAINING DATA (uncomment one of the below) # ------------------------------------------------ training_data = ExplicitTrainingData(x_true, y_true) # training_data = ImplicitTrainingData(x_true) # ------------------------------------------------------------ # MAKE SOLUTION MANIPULATOR (uncomment one of the below blocks) # ------------------------------------------------------------ # using AGraph.py for the solution manipulator # sol_manip = agm(x_true.shape[1], 32, nloads=2) # sol_manip.add_node_type(AGNodes.Add) # sol_manip.add_node_type(AGNodes.Subtract) # sol_manip.add_node_type(AGNodes.Multiply) # sol_manip.add_node_type(AGNodes.Divide) # # sol_manip.add_node_type(AGNodes.Exp) # # sol_manip.add_node_type(AGNodes.Log) # # sol_manip.add_node_type(AGNodes.Sin) # # sol_manip.add_node_type(AGNodes.Cos) # # sol_manip.add_node_type(AGNodes.Abs) # sol_manip.add_node_type(AGNodes.Sqrt) # using AGraphCpp.py for the solution manipulator sol_manip = AGraphCpp.AGraphCppManipulator(x_true.shape[1], 32, nloads=2) sol_manip.add_node_type(2) # + sol_manip.add_node_type(3) # - sol_manip.add_node_type(4) # * sol_manip.add_node_type(5) # / sol_manip.add_node_type(12) # sqrt # ---------------------------------- # MAKE FITNESS PREDICTOR MANIPULATOR # ---------------------------------- pred_manip = fpm(128, data_size) # ------------------------------------------------ # MAKE FITNESS METRIC (uncomment one of the below) # ------------------------------------------------ regressor = StandardRegression(const_deriv=True) # regressor = ImplicitRegression() # -------------------------------------- # MAKE SERIAL ISLAND MANAGER THEN RUN IT # -------------------------------------- islmngr = ParallelIslandManager(solution_training_data=training_data, solution_manipulator=sol_manip, predictor_manipulator=pred_manip, solution_pop_size=64, fitness_metric=regressor, solution_age_fitness=True) islmngr.run_islands(max_steps, epsilon, min_steps=1000, step_increment=1000)
def main(max_steps, epsilon, data_size, parallel): # STEP 1 # Create your x and y data, on parallel, broadcast it to all other ranks ################################################## ##################### SINGLE ##################### ################################################## if not parallel: n_lin = int(math.pow(data_size, 1.0 / 3)) + 1 x_1 = np.linspace(0, 5, n_lin) x_2 = np.linspace(0, 5, n_lin) x_3 = np.linspace(0, 5, n_lin) x = np.array(np.meshgrid(x_1, x_2, x_3)).T.reshape(-1, 3) x = x[np.random.choice(x.shape[0], data_size, replace=False), :] # make solution y = (x[:, 0] * x[:, 0] + 3.5 * x[:, 1]) x_true = x y_true = y y_true = y_true.reshape(-1, 1) ################################################## ################################################## ################################################## ################################################## #################### PARALLEL #################### ################################################## if parallel: comm = MPI.COMM_WORLD rank = comm.Get_rank() if rank == 0: #n_lin = int(math.pow(data_size, 1.0/3)) + 1 #x_1 = np.linspace(0, 5, n_lin) #x_2 = np.linspace(0, 5, n_lin) #x_3 = np.linspace(0, 5, n_lin) #x = np.array(np.meshgrid(x_1, x_2, x_3)).T.reshape(-1, 3) #x = x[np.random.choice(x.shape[0], data_size, replace=False), :] x_true = np.array([i for i in range(25, 425, 25)]) y_true = np.array([ 5.38, 2.91, 2.07, 1.71, 1.46, 1.35, 1.29, 1.24, 1.2, 1.19, 1.22, 1.23, 1.23, 1.23, 1.26, 1.26 ]) #y = (x[:,0]*x[:,0]+3.5*x[:,1]) #x_true = x #y_true = y y_true = y_true.reshape(-1, 1) else: x_true = None y_true = None # then broadcast to all ranks x_true = MPI.COMM_WORLD.bcast(x_true, root=0) y_true = MPI.COMM_WORLD.bcast(y_true, root=0) ################################################## ################################################## ################################################## # STEP 2 # Create solution manipulators. The solution manipulator is what creates # the representations of the functions as the acyclic graph ####### SOLUTION MANIPULATOR ####### # nvars - number of independent variables # ag_size - length of the command stack # nloads - number of load operation which are required at the # start of stack - Default 1 # float_lim - (0, max) of floats which are generated - Default 10.0 # terminal_prob: probability that a new node will be a terminal -Default .1 sol_manip = AGraphCpp.AGraphCppManipulator(1, 64, nloads=2) ####### OPERATIONS ####### sol_manip.add_node_type(2) # + sol_manip.add_node_type(3) # - sol_manip.add_node_type(4) # * sol_manip.add_node_type(5) # / sol_manip.add_node_type(6) # sin sol_manip.add_node_type(7) # cos sol_manip.add_node_type(8) # exp sol_manip.add_node_type(9) # log sol_manip.add_node_type(10) # pow sol_manip.add_node_type(11) # abs sol_manip.add_node_type(12) # sqrt ####### PREDICTION MANIPULATOR ####### pred_manip = fpm(16, data_size) # STEP 3 # Create the training data from your x and y data, # and create the fitness metric. # For this example, we are using explicit (standard) ####### TRAINING DATA ####### training_data = ExplicitTrainingData(x_true, y_true) ####### FITNESS METRIC ####### explicit_regressor = StandardRegression() # STEP 4 # Create the island manager, this will run the steps on the population, and # determine when to stop running ####### ISLAND MANAGER ####### islmngr = ParallelIslandManager( #restart_file='test.p', solution_training_data=training_data, solution_manipulator=sol_manip, predictor_manipulator=pred_manip, solution_pop_size=64, fitness_metric=explicit_regressor) ####### RUN ISLAND MANAGER ####### ################################################## ##################### SINGLE ##################### ################################################## # max_steps - Max amount to go if no convergence happens # epsilon - error which defines convergence # min_steps - minimum number of steps required - Default 0 # step_increment - number of steps between convergence # checks / migration - Default 1000 # make_plots - bool whether or not to produce plots - Default True # checkpoint_file - base file name for checkpoint files if not parallel: islmngr.run_islands(max_steps, epsilon, min_steps=500, step_increment=500) ################################################## ################################################## ################################################## ################################################## #################### PARALLEL #################### ################################################## # when_update - how often rank 0 gets updated on ages - Default 10 # non_block - bool to determine to run nonblocking - Default True if parallel: islmngr.run_islands(max_steps, epsilon, min_steps=500, step_increment=500, when_update=50)