def launch_extras(controller, visualizations=True, **kwargs): ''' Launch post optimization extras. Including visualizations. Keyword Args: visualizations (Optional [bool]): If true run default visualizations for the controller. Default false. ''' if visualizations: mlv.show_all_default_visualizations(controller)
def launch_extras(controller,visualizations=True, **kwargs): ''' Launch post optimization extras. Including visualizations. Keyword Args: visualizations (Optional [bool]): If true run default visualizations for the controller. Default false. ''' if visualizations: mlv.show_all_default_visualizations(controller)
def main(): #M-LOOP can be run with three commands #First create your interface interface = CustomInterface() #Next create the controller. Provide it with your interface and any options you want to set n = 10 d = 25 ''' para=np.linspace(0,interface.l/2-r,n+1) low=para[0:n] up=para[1:n+1] ''' low = np.array([0] * n) up = np.array([interface.l / 2 - d / 2] * n) ''' #z=np.array([0.003, 0.009, 0.009, 0.009, 0.019, 0.021, 0.032, 0.038, 0.043, 0.061, 0.078, 0.079, 0.1, 0.103, 0.103]) z=np.array([0.0, 0.008, 0.019, 0.034, 0.035, 0.042, 0.063, 0.089, 0.099, 0.099]) half=0.03 low=z-half low=np.maximum(0,low) up=z+half up=np.minimum(interface.l/200-r,up) ''' controller = mlc.create_controller( interface, #training_type='random', controller_type='neural_net', max_num_runs=2000, #target_cost = 0, num_params=n, min_boundary=low.tolist(), max_boundary=up.tolist(), keep_prob=0.8) #To run M-LOOP and find the optimal parameters just use the controller method optimize controller.optimize() #The results of the optimization will be saved to files and can also be accessed as attributes of the controller. print('Best parameters found:') print(controller.best_params) #You can also run the default sets of visualizations for the controller with one command mlv.show_all_default_visualizations(controller, max_parameters_per_plot=2) ''' mlv.create_neural_net_learner_visualizations(controller.ml_learner.total_archive_filename, file_type=controller.ml_learner.learner_archive_file_type, plot_cross_sections=True) plt.show() ''' cal_grad(params=controller.best_params, l=interface.l, r=interface.r) cal_grad(params=controller.predicted_best_parameters, l=interface.l, r=interface.r)
def mloop(): #First create your interface interface = CustomInterface() #Next create the controller, provide it with your controller and any options you want to set controller = mlc.create_controller(interface, max_num_runs=50, target_cost=-100, num_params=1, min_boundary=[3], max_boundary=[68]) #To run M-LOOP and find the optimal parameters just use the controller method optimize controller.optimize() #The results of the optimization will be saved to files and can also be accessed as attributes of the controller. print('Best parameters found:') print(controller.best_params) #You can also run the default sets of visualizations for the controller with one command mlv.show_all_default_visualizations(controller)
def main(): #M-LOOP can be run with three commands #First create your interface interface = CustomInterface() #Next create the controller, provide it with your controller and any options you want to set controller = mlc.create_controller(interface, max_num_runs = 1000, target_cost = -2.99, num_params = 3, min_boundary = [-2,-2,-2], max_boundary = [2,2,2]) #To run M-LOOP and find the optimal parameters just use the controller method optimize controller.optimize() #The results of the optimization will be saved to files and can also be accessed as attributes of the controller. print('Best parameters found:') print(controller.best_params) #You can also run the default sets of visualizations for the controller with one command mlv.show_all_default_visualizations(controller)
def main(): #M-LOOP can be run with three commands #First create your interface interface = CustomInterface() #Next create the controller. Provide it with your interface and any options you want to set n = 10 para = np.linspace(0, 0.135, n + 1) low = para[0:n] up = para[1:n + 1] controller = mlc.create_controller( interface, #training_type='random', controller_type='neural_net', max_num_runs=5000, target_cost=0, num_params=n, min_boundary=low.tolist(), max_boundary=up.tolist(), keep_prob=0.9) #To run M-LOOP and find the optimal parameters just use the controller method optimize controller.optimize() #The results of the optimization will be saved to files and can also be accessed as attributes of the controller. print('Best parameters found:') print(controller.best_params) #You can also run the default sets of visualizations for the controller with one command mlv.show_all_default_visualizations(controller) mlv.create_neural_net_learner_visualizations( controller.ml_learner.total_archive_filename, file_type=controller.ml_learner.learner_archive_file_type, plot_cross_sections=True) plt.show() cal_grad(controller.best_params) cal_grad(controller.predicted_best_parameters)
def main(): #M-LOOP can be run with three commands #First create your interface interface = CustomInterface() #Next create the controller. Provide it with your interface and any options you want to set controller = mlc.create_controller(interface, max_num_runs = 10000, controller_type='neural_net', #target_cost = -2.99, num_params = 3, min_boundary = [-2,-2,-2], max_boundary = [2,2,2]) #To run M-LOOP and find the optimal parameters just use the controller method optimize controller.optimize() #The results of the optimization will be saved to files and can also be accessed as attributes of the controller. print('Best parameters found:') print(controller.best_params) #You can also run the default sets of visualizations for the controller with one command mlv.show_all_default_visualizations(controller) plt.show()
def main(): # create a file folder to save today's data today = str(datetime.date.today()) path_today = 'E:/python/code/data/' + today if not os.path.exists(path_today): os.makedirs(path_today) n = 7 interface_2 = CustomInterface_2(n) controller = mlc.create_controller( interface_2, #training_type='differential_evolution', controller_type='neural_net', max_num_runs=100, keep_prob=0.8, num_params=n, min_boundary=[0.2] * n, max_boundary=[1] * n) # start the second optimization controller.optimize() # create the file folder of this optimization start_datetime = str(controller.start_datetime)[11:19] start_time = sf.change_colon_into_dash(start_datetime) path_today += '/' + str(start_time) os.makedirs(path_today) # save the best wave res = sf.add_zero(controller.best_params) filename = path_today + '/best_paras_2_' + str(start_time) + '.csv' sf.array_to_csv(filename=filename, array=res) # save the predicted wave res = sf.add_zero(controller.predicted_best_parameters) filename = path_today + '/pre_best_paras_2_' + str(start_time) + '.csv' sf.array_to_csv(filename=filename, array=res) # save the seed light of the predicted wave res = sf.wave_normalization(res) filename = 'E:/python/documents/wave_opt/gausswave.csv' sf.array_to_csv(filename=filename, array=res) siganl = [1] gain = sf.convey_params_get_cost(siganl) res = res / gain * interface_2.paras_scale filename = 'E:/python/documents/wave_opt/gausswave.csv' sf.array_to_csv(filename=filename, array=res) signal = [-1] gate = sf.convey_params_get_cost(signal) filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv' filename_ASQ = 'D:/shot noise/' + sf.today_str() + '/try/ASQ.csv' df_SQ = pd.read_csv(filename_SQ, header=None).values df_ASQ = pd.read_csv(filename_ASQ, header=None).values res = df_SQ - df_ASQ df_SQ = df_SQ[np.where(np.abs(res - np.mean(res)) < 3 * gate)] res = res[np.where(np.abs(res - np.mean(res)) < 3 * gate)] cost = np.var(res) / np.mean(df_SQ) print('predicted best cost_2 is:', controller.predicted_best_cost) print('actual cost_2 is:', 10 * np.log10(cost)) filename_seedlight = 'D:/shot noise/' + sf.today_str() + '/try2/seed.csv' filename = path_today + '/seedlight_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_seedlight, header=None) df.to_csv(filename, header=0, index=0) # save the excited stokes light filename_excited = 'D:/shot noise/' + sf.today_str() + '/try/excited.csv' filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv' filename = path_today + '/excited_light_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_excited, header=None) df.to_csv(filename, header=0, index=0) filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv' filename = path_today + '/SQ_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_SQ, header=None) df.to_csv(filename, header=0, index=0) # save the anti-stokes light filename_excited = 'D:/shot noise/' + sf.today_str( ) + '/try/antistokes.csv' filename = path_today + '/antistokes_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_excited, header=None) df.to_csv(filename, header=0, index=0) filename_ASQ = 'D:/shot noise/' + sf.today_str() + '/try/ASQ.csv' filename = path_today + '/ASQ_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_ASQ, header=None) df.to_csv(filename, header=0, index=0) # show visualizations mlv.show_all_default_visualizations(controller)
def main(): # create a file folder to save today's data today = str(datetime.date.today()) path_today = 'E:/python/code/data/' + today if not os.path.exists(path_today): os.makedirs(path_today) # interface to optimize gain interface_1 = CustomInterface_1(nums_params=7) controller = mlc.create_controller( interface_1, #training_type='random', controller_type='neural_net', max_num_runs=100, # target_cost = -2.99, keep_prob=0.8, num_params=interface_1.nums_params, min_boundary=[0.2] * interface_1.nums_params, max_boundary=[1] * interface_1.nums_params) # To run M-LOOP and find the optimal parameters just use the controller method optimize controller.optimize() # create the file folder of this optimization start_datetime = str(controller.start_datetime)[11:19] start_time = sf.change_colon_into_dash(start_datetime) path_today += '/' + str(start_time) os.makedirs(path_today) # save the best wave res = sf.add_zero(controller.best_params) filename = path_today + '/best_paras_' + str(start_time) + '.csv' sf.array_to_csv(filename=filename, array=res) # save the predicted wave res = sf.add_zero(controller.predicted_best_parameters) filename = path_today + '/pre_best_paras_' + str(start_time) + '.csv' sf.array_to_csv(filename=filename, array=res) # save the seed light of the predicted wave filename = 'E:/python/documents/wave_opt/gausswave.csv' sf.array_to_csv(filename=filename, array=res) signal = [1] gain = sf.convey_params_get_cost(signal) print('predicted best cost is:', controller.predicted_best_cost) print('actual cost is:', 10 * np.log10(gain)) filename_seedlight = 'D:/shot noise/' + sf.today_str() + '/try2/seed.csv' filename = path_today + '/seedlight_' + str(start_time) + '.csv' df = pd.read_csv(filename_seedlight, header=None) df.to_csv(filename, header=0, index=0) # save the excited stokes light filename_excited = 'D:/shot noise/' + sf.today_str() + '/try/excited.csv' filename = path_today + '/excited_light_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_excited, header=None) df.to_csv(filename, header=0, index=0) filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv' filename = path_today + '/SQ_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_SQ, header=None) df.to_csv(filename, header=0, index=0) # save the anti-stokes light filename_excited = 'D:/shot noise/' + sf.today_str( ) + '/try/antistokes.csv' filename = path_today + '/antistokes_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_excited, header=None) df.to_csv(filename, header=0, index=0) filename_ASQ = 'D:/shot noise/' + sf.today_str() + '/try/ASQ.csv' filename = path_today + '/ASQ_2_' + str(start_time) + '.csv' df = pd.read_csv(filename_ASQ, header=None) df.to_csv(filename, header=0, index=0) # show visualizations mlv.show_all_default_visualizations(controller) '''