def plot_from_pickle(beamBendingPkl): """ Generates a plot of the trial results named "best_individual.png" within the trial folder. Copies the plot into the plots folder inside the testing parent directory :param beamBendingPkl: file name of the .pkl file containing test results """ if beamBendingPkl is None: # handles missing .pkl file return None # read bingo pickle file and retrieve best model archipelago = load_parallel_archipelago_from_file(beamBendingPkl) best_ind = archipelago.get_best_individual() L = 10. k = 5.e-5 x = np.linspace(0., L, 64).reshape([64, 1]) yan = analytic_solution(x, k, L) y = best_ind.evaluate_equation_at(x) # plot the training data X_a = np.linspace(0, 10, 5) Y_a = analytic_solution(X_a, 5e-5, 10) plt.figure() plt.plot(x, yan, 'r-', label='Analytical Solution') plt.plot(X_a, Y_a, 'gx', label='Training Data Points') plt.plot(x, y, 'b-', label='Best GPSR Model') plt.xlabel('x') plt.ylabel('displacement') plt.legend() plt.savefig('best_individual') plt.close()
def write_results(self, beamBendingPkl): """ Writes the test results to the .csv in the test parent directory :param beamBendingPkl: Filename of the .pkl file containing test results. """ if beamBendingPkl is None: # handles missing .pkl file return None # simplify equation archipelago = load_parallel_archipelago_from_file(beamBendingPkl) best_ind = archipelago.get_best_individual() polynomial = Simplification(str(best_ind)).result # write test results to the csv with open(self.csvPath, 'a') as csvfile: fieldNames = [ 'binary', 'fitness', 'complexity', 'generations', 'f(X_0)' ] w = csv.DictWriter(csvfile, dialect='excel', fieldnames=fieldNames) w.writerow({ 'binary': self.binary, 'fitness': best_ind.fitness, 'complexity': best_ind.get_complexity(), 'generations': archipelago.generational_age, 'f(X_0)': polynomial })
def main(): """ main """ print() # blank line to make terminal more legible testPath = "/uufs/chpc.utah.edu/common/home/u1008557/tests/bnd_tdata/" csvPath = '/uufs/chpc.utah.edu/common/home/u1008557/tests/bnd_tdata/results.csv' create_csv(csvPath) t_list = ['t2', 't4', 't4-new', 't8', 't16', 't32', 't64'] for t in t_list: tPath = testPath + t + '/' os.chdir(tPath) for i in range(1, 31): trialPath = '{0}trial{1}/'.format(tPath, i) os.chdir(trialPath) beamBendingPkl = get_pkl_input() if beamBendingPkl is None: # handles missing .pkl file print('pkl file missing from ' + os.getcwd()) continue #plot_from_pickle(beamBendingPkl) # comment out when troubleshooting. Plotting takes a long time # simplify equation try: archipelago = load_parallel_archipelago_from_file( beamBendingPkl) except: print( 'change your python path to:\nPYTHONPATH=/uufs/chpc.utah.edu/common/home/u6019587/bin/bingo_fork:/uufs/chpc.utah.edu/common/home/u6019587/src/bingo_diffeq_tf' ) sys.exit() best_ind = archipelago.get_best_individual() polynomial = str(Simplification(str(best_ind)).result) polynomial = polynomial.replace('X_0', 'x') # polynomial = str(best_ind) # use if polynomial is broken # Did the test converge and exit successfully? success = 'False' if float(best_ind.fitness) <= 1e-7: success = 'True' # write test results to the csv with open(csvPath, 'a') as csvfile: fieldNames = [ 'Training Data', 'success', 'fitness', 'generations', 'solution' ] w = csv.DictWriter(csvfile, dialect='excel', fieldnames=fieldNames) w.writerow({ 'Training Data': t[1:], 'success': success, 'fitness': best_ind.fitness, 'generations': archipelago.generational_age, 'solution': polynomial })
def test_dump_then_load_equal_procs(): island = num_island(COMM_RANK) archipelago = ParallelArchipelago(island, sync_frequency=10, non_blocking=True) file_name = "testing_pa_dump_and_load_eq.pkl" archipelago.dump_to_file(file_name) archipelago = \ load_parallel_archipelago_from_file(file_name) if COMM_RANK == 0: os.remove(file_name) origin_proc = archipelago._island.population[0].values[0] return mpi_assert_equal(origin_proc, COMM_RANK)
def test_dump_then_load(one_island): archipelago = ParallelArchipelago(one_island) archipelago.evolve(1) file_name = "testing_pa_dump_and_load.pkl" archipelago.dump_to_file(file_name) archipelago.evolve(1) archipelago = \ load_parallel_archipelago_from_file(file_name) assert 1 == archipelago.generational_age archipelago.evolve(1) assert 2 == archipelago.generational_age os.remove(file_name)
def test_dump_then_load_more_procs(): island = num_island(COMM_RANK) archipelago = ParallelArchipelago(island, sync_frequency=10, non_blocking=True) file_name = "testing_pa_dump_and_load_gt.pkl" archipelago.dump_to_file(file_name) _remove_proc_from_pickle(file_name) archipelago = \ load_parallel_archipelago_from_file(file_name) if COMM_RANK == 0: os.remove(file_name) origin_proc = archipelago._island.population[0].values[0] expected_origin = COMM_RANK + 1 if COMM_RANK == COMM_SIZE - 1: expected_origin = 1 return mpi_assert_equal(origin_proc, expected_origin)
def plot_from_pickle(self, beamBendingPkl): """ Generates a plot of the trial results named "best_individual.png" within the trial folder. Copies the plot into the plots folder inside the testing parent directory :param beamBendingPkl: file name of the .pkl file containing test results """ if beamBendingPkl is None: # handles missing .pkl file raise FileNotFoundError("Missing beam_bending_#####.pkl file in " + os.getcwd()) # read bingo pickle file and retrieve best model archipelago = load_parallel_archipelago_from_file(beamBendingPkl) best_ind = archipelago.get_best_individual() L = 10. k = 5.e-2 x = np.linspace(0., L, 64).reshape([64, 1]) yan = analytic_solution(x, k, L) y = best_ind.evaluate_equation_at(x) # plot the training data X_a = np.linspace(0, 10, 5) Y_a = analytic_solution(X_a, 5e-2, 10) plt.figure() plt.plot(x, yan, 'r-', label='Analytical Solution') plt.plot(X_a, Y_a, 'gx', label='Training Data Points') plt.plot(x, y, 'b-', label='Best GPSR Model') plt.xlabel('x') plt.ylabel('displacement') plt.legend() plt.savefig('best_individual') plt.close() # copy plot to 'plots' directory in testing dir cwd = os.getcwd() plotName = self.binary + "_" + cwd[-1] src = cwd + "/best_individual.png" dst = self.plotsPath + plotName + ".png" shutil.copyfile(src, dst)
def main(): """ main """ print() # blank line to make terminal more legible testPath = "/uufs/chpc.utah.edu/common/home/u1008557/tests/bnd_study/" csvPath = '/uufs/chpc.utah.edu/common/home/u1008557/tests/bnd_study/results.csv' create_csv(csvPath) # arrays containg fitness values for each test containing a certain hyperparameter pop0, pop1, stack0, stack1, dif0, dif1, cross0, cross1, mut0, mut1 = [], [], [], [], [], [], [], [], [], [] binaries = create_binaries() for binary in binaries: binaryPath = testPath + binary + '/' os.chdir(binaryPath) beamBendingPkl = get_pkl_input() if beamBendingPkl is None: # handles missing .pkl file print('pkl file missing from ' + os.getcwd()) continue plot_from_pickle(beamBendingPkl) # comment out when troubleshooting. Plotting takes a long time # simplify equation archipelago = load_parallel_archipelago_from_file(beamBendingPkl) best_ind = archipelago.get_best_individual() polynomial = str(Simplification(str(best_ind)).result) polynomial = polynomial.replace('X_0', 'x') # polynomial = str(best_ind) # use if polynomial is broken # write test results to the csv with open(csvPath, 'a') as csvfile: fieldNames = ['binary', 'fitness', 'generations', 'solution'] w = csv.DictWriter(csvfile, dialect='excel', fieldnames=fieldNames) w.writerow({'binary': binary, 'fitness': best_ind.fitness, 'generations': archipelago.generational_age, 'solution': polynomial}) # sort fitness into their parameter arrays pop, stack, dif, cross, mut = binary[:] pop, stack, dif, cross, mut = int(pop), int(stack), int(dif), int(cross), int(mut) fit = '{:.3e}'.format(best_ind.fitness) if pop: pop1.append(fit) elif not pop: pop0.append(fit) if stack: stack1.append(fit) elif not stack: stack0.append(fit) if dif: dif1.append(fit) elif not dif: dif0.append(fit) if cross: cross1.append(fit) elif not cross: cross0.append(fit) if mut: mut1.append(fit) elif not mut: mut0.append(fit) # write param arrays to a csv csv_param_path = '/uufs/chpc.utah.edu/common/home/u1008557/tests/bnd_study/param_fit.csv' if os.path.exists(csv_param_path): os.remove(csv_param_path) """ pop_size = [64, 128] stack_size = [40, 50] differential_weight = [0.1, 0.4] crossover_rate = [0.4, 0.6] mutation_rate = [0.8, 1.0] """ # write fitness data headers to a .csv file with open(csv_param_path, 'a') as csv_param: fieldsnames = ['pop 64', 'pop 128', 'stack 40', 'stack 50', 'dif 0.1', 'dif 0.4', 'cross 0.4', 'cross 0.6', 'mut 0.8', 'mut 1.0'] w = csv.DictWriter(csv_param, dialect='excel', fieldnames=fieldsnames) w.writeheader() for i in range(len(pop0)): # write param arrays to csv w.writerow({'pop 64': pop0[i], 'pop 128': pop1[i], 'stack 40': stack0[i], 'stack 50': stack1[i], 'dif 0.1': dif0[i], 'dif 0.4': dif1[i], 'cross 0.4': cross0[i], 'cross 0.6': cross1[i], 'mut 0.8': mut0[i], 'mut 1.0': mut1[i]})
import pandas as pd import numpy as np import sys from bingo.evolutionary_optimizers.parallel_archipelago import load_parallel_archipelago_from_file if __name__ == "__main__": checkpoint_num = sys.argv[1] pickle_file = "".join(['checkpoint_', str(checkpoint_num), '.pkl']) pickle = load_parallel_archipelago_from_file(pickle_file) for individual in pickle.hall_of_fame: equation = individual.get_console_string() print(individual.get_complexity(), ': ', equation) print('Fitness: ', individual.fitness)