def main(argv):
    from experiments.util import AsyncExecutor, generate_launch_commands
    import experiments.meta_overfitting_v2.neural_processes_overfitting_base

    command_list = []

    for dataset in FLAGS.datasets.split(','):
        if dataset == 'sin':
            n_context_samples = [5]
        elif dataset == 'cauchy':
            n_context_samples = [20]
        else:
            raise AssertionError('dataset must be either of [sin, cauchy]')

        exp_config = {
            'exp_name': ['meta-overfitting-v2-nps-%s' % dataset],
            'dataset': [dataset],
            'n_threads': [N_THREADS],
            'seed': list(range(30, 55)),
            'data_seed': [28],
            'weight_decay': [0.0, 1e-3, 1e-2, 1e-1, 2e-1, 4e-1],
            'r_dim': [256, 512],
            'n_iter_fit': [30000],
            'lr': [1e-3],
            'lr_decay': [0.97],
            'n_train_tasks': [2, 4, 8, 16, 32, 64, 128, 256, 512],
            'n_test_tasks': [200],
            'n_context_samples': n_context_samples,
            'n_test_samples': [100],
        }

        command_list.extend(
            generate_launch_commands(
                experiments.meta_overfitting_v2.
                neural_processes_overfitting_base, exp_config))

    if FLAGS.cluster:
        cluster_cmds = []
        for python_cmd in command_list:
            cmd_hash = hashlib.md5(str.encode(python_cmd)).hexdigest()

            bsub_cmd = 'bsub -oo /cluster/project/infk/krause/rojonas/stdout/gp-priors/meta-overfitting/%s.out' \
                       ' -W 03:59'\
                       ' -R "rusage[mem=1048]"' \
                       ' -n %i '% (cmd_hash, N_THREADS)
            cluster_cmds.append(bsub_cmd + ' ' + python_cmd)
        answer = input(
            "About to submit %i compute jobs to the cluster. Proceed? [yes/no]\n"
            % len(cluster_cmds))
        if answer == 'yes':
            for cmd in cluster_cmds:
                os.system(cmd)
    else:
        answer = input("About to run %i compute jobs locally on %i workers. "
                       "Proceed? [yes/no]\n" %
                       (len(command_list), FLAGS.n_workers))
        if answer == 'yes':
            exec_fn = lambda cmd: os.system(cmd)
            executor = AsyncExecutor(n_jobs=FLAGS.n_workers)
            executor.run(exec_fn, command_list)
    'dataset': ['swissfel'],
    'n_threads': [N_THREADS],
    'seed': [31, 32, 33, 34, 35],
    'weight_decay': list(np.arange(0.05, 1.1, step=0.05)),
    'covar_module': ['NN'],
    'mean_module': ['NN'],
    'num_layers': [4],
    'layer_size': [32],
    'n_iter_fit': [30000],
}

command_list = generate_launch_commands(experiments.meta_GPR_mll_base_exp, exp_config)

if cluster :
    cluster_cmds = []
    for python_cmd in command_list:
        cmd_hash = hashlib.md5(str.encode(python_cmd)).hexdigest()

        bsub_cmd = 'bsub -oo /cluster/project/infk/krause/rojonas/stdout/gp-priors/meta-overfitting/%s.out' \
                   ' -W 3:59'\
                   ' -R "rusage[mem=8048]"' \
                   ' -n %i '% (cmd_hash, N_THREADS)
        cluster_cmds.append(bsub_cmd + ' ' + python_cmd)
    answer = input("About to submit %i compute jobs to the cluster. Proceed? [yes/no]\n"%len(cluster_cmds))
    if answer == 'yes':
        for cmd in cluster_cmds:
            os.system(cmd)
else:
    exec_fn = lambda cmd: os.system(cmd)
    executor = AsyncExecutor(n_jobs=-1)
    executor.run(exec_fn, command_list)