コード例 #1
0
def main():
    # Create M-LOOP optmiser interface with desired parameters
    interface = LoopInterface()
    # interface.daemon = True

    # Instantiate experiment controller
    controller = create_controller(interface, **interface.config)

    # Define the M-LOOP session ID and initialise the mloop_iteration
    set_globals_mloop(controller.start_datetime.strftime('%Y%m%dT%H%M%S'), 0)

    # Run the optimiser using the constructed interface
    controller.optimize()

    # Reset the M-LOOP session and index to None
    logger.info('Optimisation ended.')
    set_globals_mloop()

    # Set the optimisation globals to their best results
    logger.info('Setting best parameters in runmanager.')
    globals_dict = dict(
        zip(interface.config['mloop_params'], controller.best_params))
    set_globals(globals_dict)

    # Return the results in a dictionary
    opt_results = {}
    opt_results['best_params'] = controller.best_params
    opt_results['best_cost'] = controller.best_cost
    opt_results['best_uncer'] = controller.best_uncer
    opt_results['best_index'] = controller.best_index
    return opt_results
コード例 #2
0
ファイル: floop.py プロジェクト: JKalnins/Fourier_MLOOP
def RunOnce(
    max_allowed_runs,
    tcost,
    n_ab,
    y_target,
    noise_type="None",
    noise_scale=0.0,
    learner="gaussian_process",
):
    """Runs M-LOOP once and returns the cost of each run, and the number of runs

    Args:
        max_allowed_runs (int): Max runs for the learner
        tcost (float): Target cost for the learner
        n_ab (int): number of parameters / 2 (length of a,b for FS)
        y_target (np.ndarray): y-values of target function
        noise_type (str, optional): type of noise to combine with costs. Defaults to "None".
        noise_scale (float, optional): std dev of noise to combine with costs. Defaults to 0.0.
        learner (str, optional): Learner type for M-LOOP. Defaults to "gaussian_process".

    Returns:
        np.ndarray: costs of each run
        int: number of runs taken
        list: parameters of each run (list of np.ndarrays)
        float: process_time of controller.optimize()
    """
    if learner not in [
            "gaussian_process",
            "neural_net",
            "differential_evolution",
            "nelder_mead",
            "random",
    ]:
        raise ValueError("learner was not one of the valid choices.")
    interface = _FourierInterface(n_ab, y_target, noise_type, noise_scale)
    controller = mlc.create_controller(
        interface,
        controller_type=learner,
        max_num_runs=max_allowed_runs,
        target_cost=tcost,
        num_params=n_ab * 2,
        min_boundary=np.full(n_ab * 2, -1),  # range [-1, 1]
        max_boundary=np.full(n_ab * 2, 1),
        no_delay=False,
        controller_archive_file_type="pkl",
        learner_archive_file_type="pkl",
    )
    t_init = perf_counter()
    controller.optimize()
    t_fin = perf_counter()
    time_taken = t_fin - t_init
    costs = controller.in_costs
    runs = controller.num_in_costs
    out_params = controller.out_params
    return costs, runs, out_params, time_taken
コード例 #3
0
ファイル: test_units.py プロジェクト: michaelhush/M-LOOP
 def test_bad(self):
     cost_list = [1., float('nan'),2.,float('nan'),-1.]
     interface = CostListInterface(cost_list)
     controller = mlc.create_controller(interface, 
                                        max_num_runs = 10, 
                                        target_cost = -1,
                                        max_num_runs_without_better_params = 4)
     controller.optimize()
     self.assertTrue(controller.best_cost == -1.)
     for x,y in zip(controller.in_costs,cost_list):
         self.assertTrue(x==y or (math.isnan(x) and math.isnan(y)))
コード例 #4
0
ファイル: test_units.py プロジェクト: michaelhush/M-LOOP
 def test_target_cost(self):
     cost_list = [1.,2.,-1.]
     interface = CostListInterface(cost_list)
     controller = mlc.create_controller(interface, 
                                        max_num_runs = 10, 
                                        target_cost = -1,
                                        max_num_runs_without_better_params = 4)
     controller.optimize()
     self.assertTrue(controller.best_cost == -1.)
     self.assertTrue(np.array_equiv(np.array(controller.in_costs),
                                     np.array(cost_list)))
コード例 #5
0
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)
コード例 #6
0
 def test_bad(self):
     cost_list = [1., float('nan'), 2., float('nan'), -1.]
     interface = CostListInterface(cost_list)
     controller = mlc.create_controller(
         interface,
         max_num_runs=10,
         target_cost=-1,
         max_num_runs_without_better_params=4)
     controller.optimize()
     self.assertTrue(controller.best_cost == -1.)
     for x, y in zip(controller.in_costs, cost_list):
         self.assertTrue(x == y or (math.isnan(x) and math.isnan(y)))
コード例 #7
0
 def test_target_cost(self):
     cost_list = [1., 2., -1.]
     interface = CostListInterface(cost_list)
     controller = mlc.create_controller(
         interface,
         max_num_runs=10,
         target_cost=-1,
         max_num_runs_without_better_params=4)
     controller.optimize()
     self.assertTrue(controller.best_cost == -1.)
     self.assertTrue(
         np.array_equiv(np.array(controller.in_costs), np.array(cost_list)))
コード例 #8
0
def main():
    ''' This is the code that excutes the machine learning online optimization
process (MLOOP). '''

    #create the interface
    interface = CustomInterface()
    #create the controller and provide the interface and options
    controller = mlc.create_controller(interface)
    #run the mloop optimization
    controller.optimize()

    #access the optimized parameters (the results will be saved to a file
    #somewhere)
    print(f"Best parameters found: {controller.best_params}")
コード例 #9
0
ファイル: main.py プロジェクト: shinich1/tenhou-python-bot
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)
コード例 #10
0
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)
コード例 #11
0
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)
コード例 #12
0
def launch_from_file(config_filename, **kwargs):
    '''
    Launch M-LOOP using a configuration file. See configuration file documentation.
    
    Args:
        config_filename (str): Filename of configuration file 
        **kwargs : keywords that override the keywords in the file.
       
    Returns:
        controller (Controller): Controller for optimization.
    '''
    try:
        file_kwargs = mlu.get_dict_from_file(config_filename, 'txt')
    except (IOError, OSError):
        print('Unable to open M-LOOP configuration file:' +
              repr(config_filename))
        raise

    file_kwargs.update(kwargs)
    #Main run sequence
    #Create interface and extract unused keywords
    interface = mli.create_interface(**file_kwargs)
    file_kwargs = interface.remaining_kwargs
    #Create controller and extract unused keywords
    controller = mlc.create_controller(interface, **file_kwargs)
    file_kwargs = controller.remaining_kwargs
    #Extract keywords for post processing extras, and raise an error if any keywords were unused.
    extras_kwargs = _pop_extras_kwargs(file_kwargs)
    if file_kwargs:
        logging.getLogger(__name__).error('Unused extra options provided:' +
                                          repr(file_kwargs))
        raise ValueError
    #Run the actual optimization
    controller.optimize()
    #Launch post processing extras
    launch_extras(controller, **extras_kwargs)

    return 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
    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()
コード例 #14
0
ファイル: launchers.py プロジェクト: michaelhush/M-LOOP
def launch_from_file(config_filename, 
                     **kwargs):
    '''
    Launch M-LOOP using a configuration file. See configuration file documentation.
    
    Args:
        config_filename (str): Filename of configuration file 
        **kwargs : keywords that override the keywords in the file.
       
    Returns:
        controller (Controller): Controller for optimization.
    '''
    try:
        file_kwargs = mlu.get_dict_from_file(config_filename,'txt')
    except (IOError, OSError):
        print('Unable to open M-LOOP configuration file:' + repr(config_filename))
        raise
    file_kwargs.update(kwargs)
    #Main run sequence
    #Create interface and extract unused keywords
    interface = mli.create_interface(**file_kwargs)
    file_kwargs = interface.remaining_kwargs
    #Create controller and extract unused keywords
    controller = mlc.create_controller(interface, **file_kwargs)
    file_kwargs = controller.remaining_kwargs
    #Extract keywords for post processing extras, and raise an error if any keywords were unused. 
    extras_kwargs = _pop_extras_kwargs(file_kwargs)
    if file_kwargs:
        logging.getLogger(__name__).error('Unused extra options provided:' + repr(file_kwargs))
        raise ValueError
    #Run the actual optimization
    controller.optimize()
    #Launch post processing extras
    launch_extras(controller, **extras_kwargs)  
    
    return controller 
コード例 #15
0
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)
コード例 #16
0
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)
    '''