Example #1
0
def test_exception_raising_manager_no_abort():
    """Running until fake_MPI tries to send msg to test (mocked) comm.Abort is called

    Manager should raise MPISendException when fakeMPI tries to send message, which
    will be caught by libE and raise MPIAbortException from fakeMPI.Abort"""
    libE_specs['abort_on_exception'] = False
    libE_specs['comm'] = fake_mpi
    with pytest.raises(MPISendException):
        sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
        libE(sim_specs, gen_specs, exit_criteria, libE_specs=libE_specs)
        pytest.fail('Expected MPISendException exception')
def run_tests(mpi_runner, runner_name, test_list_exargs, exp_list):

    # Mock up system
    customizer = {
        'mpi_runner':
        mpi_runner,  # Select runner: mpich, openmpi, aprun, srun, jsrun
        'runner_name':
        runner_name,  # Runner name: Replaces run command if not None
        'cores_on_node': (16, 64),  # Tuple (physical cores, logical cores)
        'node_file': node_file
    }  # Name of file containing a node-list

    exctr = MPIExecutor(central_mode=True,
                        auto_resources=True,
                        custom_info=customizer)
    exctr.register_calc(full_path=sim_app, calc_type='sim')

    test_list = test_list_base + test_list_exargs
    sim_specs['user'] = {'tests': test_list, 'expect': exp_list}

    # Perform the run
    H, pinfo, flag = libE(sim_specs,
                          gen_specs,
                          exit_criteria,
                          persis_info,
                          libE_specs=libE_specs)
Example #3
0
def test_manager_exception():
    try:
        os.remove(fname_abort)
    except OSError as e:
        pass
    with mock.patch('libensemble.libE.manager_main') as managerMock:
        managerMock.side_effect = Exception
        with mock.patch('libensemble.libE.comms_abort') as abortMock:
            abortMock.side_effect = Exception
            with pytest.raises(Exception, message='Expected exception'):
                libE({'out': [('f', float)]}, {'out': [('x', float)]},
                     {'sim_max': 1},
                     libE_specs={'comm': MPI.COMM_WORLD})
            # Check npy file dumped
            assert os.path.isfile(fname_abort), "History file not dumped"
            os.remove(fname_abort)
Example #4
0
def test_exception_raising_check_inputs():
    """Intentionally running without sim_specs['in'] to test exception raising (Fails)"""
    with pytest.raises(KeyError):
        H, _, _ = libE({'out': [('f', float)]}, {'out': [('x', float)]},
                       {'sim_max': 1},
                       libE_specs={'comm': fake_mpi})
        pytest.fail('Expected KeyError exception')
Example #5
0
def test_exception_raising_manager():
    # Intentionally running without sim_specs['in'] to test exception raising (Fails)
    with mock.patch('libensemble.libE.comms_abort') as abortMock:
        abortMock.side_effect = Exception
        with pytest.raises(Exception, message='Expected exception'):
            H, _, _ = libE({'out': [('f', float)]}, {'out': [('x', float)]},
                           {'sim_max': 1},
                           libE_specs={'comm': MPI.COMM_WORLD})
Example #6
0
def test_proc_not_in_communicator():
    """Checking proc not in communicator returns exit status of 3"""
    libE_specs['comm'], mpi_comm_null = mpi_comm_excl()
    H, _, flag = libE({
        'in': ['x'],
        'out': [('f', float)]
    }, {'out': [('x', float)]}, {'sim_max': 1},
                      libE_specs=libE_specs)
    assert flag == 3, "libE return flag should be 3. Returned: " + str(flag)
Example #7
0
def test_manager_exception():
    """Checking dump of history and pickle file on abort"""
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    remove_file_if_exists(hfile_abort)
    remove_file_if_exists(pfile_abort)

    with mock.patch('libensemble.libE.manager_main') as managerMock:
        managerMock.side_effect = Exception
        with mock.patch('libensemble.libE.comms_abort') as abortMock:
            abortMock.side_effect = Exception
            # Need fake MPI to get past the Manager only check and dump history
            with pytest.raises(Exception):
                libE(sim_specs,
                     gen_specs,
                     exit_criteria,
                     libE_specs={'comm': fake_mpi})
                pytest.fail('Expected exception')
            assert os.path.isfile(hfile_abort), "History file not dumped"
            assert os.path.isfile(pfile_abort), "Pickle file not dumped"
            os.remove(hfile_abort)
            os.remove(pfile_abort)
Example #8
0
def test_logging_disabling():
    remove_file_if_exists('ensemble.log')
    remove_file_if_exists('libE_stats.txt')
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    libE_specs = {'comm': fake_mpi, 'comms': 'mpi', 'disable_log_files': True}
    logconfig = LogConfig.config
    logconfig.logger_set = False

    with mock.patch('libensemble.libE_manager.manager_main') as managerMock:
        managerMock.side_effect = Exception
        with mock.patch('libensemble.comms_abort') as abortMock:
            abortMock.side_effect = Exception
            with pytest.raises(Exception):
                libE(sim_specs,
                     gen_specs,
                     exit_criteria,
                     libE_specs=libE_specs)
                pytest.fail('Expected exception')
            assert not os.path.isfile(
                'ensemble.log'), "ensemble.log file dumped"
            assert not os.path.isfile(
                'libE_stats.txt'), "libE_stats.txt file dumped"
Example #9
0
def test_manager_exception():
    """Checking dump of history and pickle file on abort"""
    sim_specs, gen_specs, exit_criteria = setup.make_criteria_and_specs_0()
    remove_file_if_exists(hfile_abort)
    remove_file_if_exists(pfile_abort)

    with mock.patch('libensemble.libE_manager.manager_main') as managerMock:
        managerMock.side_effect = Exception
        # Collision between libE.py and libE() (after mods to __init__.py) means
        #   libensemble.libE.comms_abort tries to refer to the function, not file
        with mock.patch('libensemble.comms_abort') as abortMock:
            abortMock.side_effect = Exception
            # Need fake MPI to get past the Manager only check and dump history
            with pytest.raises(Exception):
                libE(sim_specs,
                     gen_specs,
                     exit_criteria,
                     libE_specs={'comm': fake_mpi})
                pytest.fail('Expected exception')
            assert os.path.isfile(hfile_abort), "History file not dumped"
            assert os.path.isfile(pfile_abort), "Pickle file not dumped"
            os.remove(hfile_abort)
            os.remove(pfile_abort)

            # Test that History and Pickle files NOT created when disabled
            with pytest.raises(Exception):
                libE(sim_specs,
                     gen_specs,
                     exit_criteria,
                     libE_specs={
                         'comm': fake_mpi,
                         'save_H_and_persis_on_abort': False
                     })
                pytest.fail('Expected exception')
            assert not os.path.isfile(hfile_abort), "History file dumped"
            assert not os.path.isfile(pfile_abort), "Pickle file dumped"
Example #10
0
        def run_aposmm(sim_max):
            sim_specs = {
                'sim_f': sim_f,
                'in': ['x'],
                'out': [('f', float), ('grad', float, ndim)]
            }

            gen_out = [('x', float, ndim), ('x_on_cube', float, ndim),
                       ('sim_id', int), ('local_min', bool),
                       ('local_pt', bool)]

            gen_specs = {
                'gen_f': gen_f,
                'in': [],
                'out': gen_out,
                'user': {
                    'initial_sample_size': 100,
                    'localopt_method': 'LD_MMA',
                    # 'opt_return_codes': [0],
                    # 'nu': 1e-6,
                    # 'mu': 1e-6,
                    'xtol_rel': 1e-6,
                    'ftol_rel': 1e-6,
                    # 'run_max_eval':10000,
                    # 'dist_to_bound_multiple': 0.5,
                    'max_active_runs': 6,
                    'lb': self._bounds[:, 0],
                    # This is only for sampling. TAO_NM doesn't honor constraints.
                    'ub': self._bounds[:, 1]
                }
            }
            alloc_specs = {
                'alloc_f': alloc_f,
                'out': [('given_back', bool)],
                'user': {}
            }

            persis_info = add_unique_random_streams({}, nworkers + 1)

            exit_criteria = {'sim_max': sim_max}

            # Perform the run
            # H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info,
            #                             alloc_specs, libE_specs)
            return libE(sim_specs, gen_specs, exit_criteria, persis_info,
                        alloc_specs, libE_specs)
Example #11
0
    def run(self, clean_work_dir=False):
        self.clean_working_directory = clean_work_dir
        self._configure_libE()
        # Check of H0 needs to be run after sim/alloc/gen specs are finalized
        if self.H0 is not None:
            check_inputs(H0=self.H0,
                         sim_specs=self.sim_specs,
                         alloc_specs=self.alloc_specs,
                         gen_specs=self.gen_specs)

        H, persis_info, flag = libE(self.sim_specs,
                                    self.gen_specs,
                                    self.exit_criteria,
                                    self.persis_info,
                                    self.alloc_specs,
                                    self.libE_specs,
                                    H0=self.H0)

        return H, persis_info, flag
def optimize_obj(obj_val, num_parameters, params=None):

    print("Using localopt_method: {}".format(
        params['localopt_method']))

    def sim_func(H, gen_info, sim_specs, libE_info):
        del libE_info  # Ignored parameter

        batch = len(H['x'])
        O = np.zeros(batch, dtype=sim_specs['out'])

        for i, x in enumerate(H['x']):
            O['f'][i] = obj_val(x)

        print(O)
        sys.stdout.flush()
        return O, gen_info

    script_name = os.path.splitext(os.path.basename(__file__))[0]
    #State the objective function, its arguments, output, and necessary parameters (and their sizes)
    sim_specs = {
        'sim_f':
            sim_func,  # This is the function whose output is being minimized
        'in': ['x'],  # These keys will be given to the above function
        'out': [
            ('f',
             float),  # This is the output from the function being minimized
        ],
    }
    gen_out = [
        ('x', float, num_parameters),
        ('x_on_cube', float, num_parameters),
        ('sim_id', int),
        ('priority', float),
        ('local_pt', bool),
        ('known_to_aposmm',
         bool),  # Mark known points so fewer updates are needed.
        ('dist_to_unit_bounds', float),
        ('dist_to_better_l', float),
        ('dist_to_better_s', float),
        ('ind_of_better_l', int),
        ('ind_of_better_s', int),
        ('started_run', bool),
        ('num_active_runs', int),  # Number of active runs point is involved in
        ('local_min', bool),
        ('paused', bool),
    ]

    if params['ansatz'] == 'QAOA':
        lb = np.array([0, 0] * params['ansatz_depth'])
        ub = np.array([np.pi, 2*np.pi] * params['ansatz_depth'])
    elif params['ansatz'] == 'RYRZ':
        lb = np.array([-np.pi] * num_parameters)
        ub = np.array([np.pi] * num_parameters)

    # State the generating function, its arguments, output, and necessary parameters.
    gen_specs = {
        'gen_f': aposmm_logic,
        'in': [o[0] for o in gen_out] + ['f', 'returned'],
        #'mu':0.1,   # limit on dist_to_bound: everything closer to bound than mu is thrown out
        'out': gen_out,
        'lb': lb,
        'ub': ub,
        'initial_sample_size': params[
            'init_points'],  # num points sampled before starting opt runs
        'localopt_method': params['localopt_method'],
        'xtol_rel': params['xtol_rel'], # TODO: give these two to local methods
        'ftol_rel': params['ftol_rel'],
        'min_batch_size': 1,
        'batch_mode': True,
        'num_active_gens': 1,
        'high_priority_to_best_localopt_runs': True,
        'sample_points': params['sample_points'],
        'max_active_runs': params['max_active_runs'],
        'dist_to_bound_multiple':
            1.0,  # NEVER more than 1! (increase max_active_runs instead)
    }

    # Tell libEnsemble when to stop
    exit_criteria = {'sim_max': params['n_iter'] + params['init_points']}

    persis_info = {'next_to_give': 0}
    persis_info['total_gen_calls'] = 0
    persis_info['last_worker'] = 0
    persis_info[0] = {
        'active_runs': set(),
        'run_order': {},
        'old_runs': {},
        'total_runs': 0,
        'rand_stream': np.random.RandomState(1)
    }

    for i in range(1, MPI.COMM_WORLD.Get_size()):
        persis_info[i] = {'rand_stream': np.random.RandomState(i)}

    alloc_specs = {'out': [('allocated', bool)], 'alloc_f': alloc_f}

    H, persis_info, flag = libE(
        sim_specs,
        gen_specs,
        exit_criteria,
        persis_info=persis_info,
        alloc_specs=alloc_specs)
    if MPI.COMM_WORLD.Get_rank() == 0:
        return (H, persis_info)
from libensemble.sim_funcs.six_hump_camel import six_hump_camel as sim_f
from libensemble.alloc_funcs.give_pregenerated_work import give_pregenerated_sim_work as alloc_f
from libensemble.utils import parse_args, save_libE_output

nworkers, is_master, libE_specs, _ = parse_args()

sim_specs = {'sim_f': sim_f, 'in': ['x'], 'out': [('f', float)]}

gen_specs = {}

H0 = np.zeros(100, dtype=[('x', float, 2)])
np.random.seed(0)
H0['x'] = np.random.uniform(0, 1, (100, 2))

alloc_specs = {'alloc_f': alloc_f, 'out': [('x', float, 2)]}

exit_criteria = {'sim_max': len(H0)}

# Perform the run
H, persis_info, flag = libE(sim_specs,
                            gen_specs,
                            exit_criteria,
                            alloc_specs=alloc_specs,
                            libE_specs=libE_specs,
                            H0=H0)

if is_master:
    assert len(H) == len(H0)
    print("\nlibEnsemble correctly didn't add anything to initial sample")
    save_libE_output(H, persis_info, __file__, nworkers)
Example #14
0
def optimize_obj(obj_val, num_parameters, ub=None, lb=None, sim_max=None):
    def sim_func(H, gen_info, sim_specs, libE_info):
        del libE_info  # Ignored parameter

        batch = len(H['x'])
        O = np.zeros(batch, dtype=sim_specs['out'])

        for i, x in enumerate(H['x']):
            O['f'][i] = obj_val(x)

        return O, gen_info

    script_name = os.path.splitext(os.path.basename(__file__))[0]
    #State the objective function, its arguments, output, and necessary parameters (and their sizes)
    sim_specs = {
        'sim_f':
        sim_func,  # This is the function whose output is being minimized
        'in': ['x'],  # These keys will be given to the above function
        'out': [
            ('f',
             float),  # This is the output from the function being minimized
        ],
    }
    gen_out = [
        ('x', float, num_parameters),
        ('x_on_cube', float, num_parameters),
        ('sim_id', int),
        ('local_pt', bool),
        ('local_min', bool),
    ]

    np.random.seed(0)
    # State the generating function, its arguments, output, and necessary parameters.
    gen_specs = {
        'gen_f':
        gen_f,
        'in':
        ['x', 'f', 'local_pt', 'sim_id', 'returned', 'x_on_cube', 'local_min'],
        #'mu':0.1,   # limit on dist_to_bound: everything closer to bound than mu is thrown out
        'out':
        gen_out,
        'user': {
            'lb':
            lb,
            'ub':
            ub,
            'initial_sample_size':
            20,  # num points sampled before starting opt runs, one per worker
            # 'localopt_method': 'scipy_COBYLA',
            # 'scipy_kwargs': {'tol': 1e-10, 'options': {'disp':True, 'maxiter': 100}},
            'localopt_method':
            'LN_COBYLA',
            'sample_points':
            np.atleast_2d(np.random.uniform(lb, ub, (20, len(lb)))),
            'run_max_eval':
            100,
            'ftol_rel':
            1e-10,
            'xtol_rel':
            1e-10,
            'num_pts_first_pass':
            nworkers - 1,
            'max_active_runs':
            2,
            'periodic':
            True,
        }
    }

    # Tell libEnsemble when to stop
    exit_criteria = {'sim_max': sim_max}

    persis_info = add_unique_random_streams({}, nworkers + 1)

    alloc_specs = {
        'alloc_f': alloc_f,
        'out': [('given_back', bool)],
        'user': {}
    }

    H, persis_info, flag = libE(sim_specs,
                                gen_specs,
                                exit_criteria,
                                persis_info=persis_info,
                                libE_specs=libE_specs,
                                alloc_specs=alloc_specs)
    if MPI.COMM_WORLD.Get_rank() == 0:
        return (H, persis_info)
Example #15
0
             'in': ['sim_id'],
             'out': [('x',float,2),
                    ],
             'lb': np.array([-3,-2]),
             'ub': np.array([ 3, 2]),
             'gen_batch_size': sim_max,
             'batch_mode': True,
             'num_active_gens':1,
             'save_every_k': 300
             }

#sim_max = num_workers
exit_criteria = {'sim_max': sim_max}


np.random.seed(1)
persis_info = {}
for i in range(MPI.COMM_WORLD.Get_size()):
    persis_info[i] = {'rand_stream': np.random.RandomState(i)}

## Perform the run
H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info)


if MPI.COMM_WORLD.Get_rank() == 0:
    #import pdb; pdb.set_trace()
    for w in range(1, num_workers+1):
        x = w * 1000.0
        assert np.all(H['arr_vals'][w-1] == x), "Array values do not all match"
        assert H['scal_val'][w-1] == x + x/1e7, "Scalar values do not all match"
    'user': {
        'stop_on_NaNs': True,  # Should alloc preempt evals
        'batch_mode': True,  # Wait until all sim evals are done
        'num_active_gens': 1,  # Only allow one active generator
        'stop_partial_fvec_eval': True
    }  # Should alloc preempt evals
}
# end_alloc_specs_rst_tag

persis_info = add_unique_random_streams(persis_info, nworkers + 1)
persis_info_safe = deepcopy(persis_info)

exit_criteria = {'sim_max': budget, 'elapsed_wallclock_time': 300}

# Perform the run
H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info,
                            alloc_specs, libE_specs)
if is_master:
    assert flag == 0
    save_libE_output(H, persis_info, __file__, nworkers)

# Perform the run but not stopping on NaNs
alloc_specs['user'].pop('stop_on_NaNs')
persis_info = deepcopy(persis_info_safe)
H, persis_info, flag = libE(sim_specs, gen_specs, exit_criteria, persis_info,
                            alloc_specs, libE_specs)
if is_master:
    assert flag == 0

# Perform the run also not stopping on partial fvec evals
alloc_specs['user'].pop('stop_partial_fvec_eval')
persis_info = deepcopy(persis_info_safe)
    def optimize(self):
        """Minimize the objective
        """
        obj_val = self.obj_w.get_obj()
        num_parameters = self.obj_w.num_parameters
        lb = np.array([(l if l is not None else -2 * np.pi)
                       for (l, u) in self.variable_bounds])
        ub = np.array([(u if u is not None else 2 * np.pi)
                       for (l, u) in self.variable_bounds])

        def sim_func(H, gen_info, sim_specs, libE_info):
            del libE_info  # Ignored parameter

            batch = len(H['x'])
            O = np.zeros(batch, dtype=sim_specs['out'])

            for i, x in enumerate(H['x']):
                O['f'][i] = obj_val(x)

            return O, gen_info

        script_name = os.path.splitext(os.path.basename(__file__))[0]
        #State the objective function, its arguments, output, and necessary parameters (and their sizes)
        sim_specs = {
            'sim_f':
            sim_func,  # This is the function whose output is being minimized
            'in': ['x'],  # These keys will be given to the above function
            'out': [
                ('f', float
                 ),  # This is the output from the function being minimized
            ],
        }
        gen_out = [
            ('x', float, num_parameters),
            ('x_on_cube', float, num_parameters),
            ('sim_id', int),
            ('local_pt', bool),
            ('local_min', bool),
        ]

        np.random.seed(0)
        # State the generating function, its arguments, output, and necessary parameters.
        # This is the default dictionary, we update it below
        gen_specs = {
            'gen_f':
            gen_f,
            'in': [
                'x', 'f', 'local_pt', 'sim_id', 'returned', 'x_on_cube',
                'local_min'
            ],
            #'mu':0.1,   # limit on dist_to_bound: everything closer to bound than mu is thrown out
            'out':
            gen_out,
            'user': {
                'lb': lb,
                'ub': ub,
                'initial_sample_size': nworkers -
                1,  # num points sampled before starting opt runs, one per worker
                'localopt_method': self.optimizer_name,
                'sample_points': np.atleast_2d(self.initial_point),
                'run_max_eval': 100,
                'num_pts_first_pass': nworkers - 1,
                'max_active_runs': 2,
                'periodic': self.obj_w.is_periodic,
            }
        }
        gen_specs['user'].update(self.gen_specs_user)

        persis_info = add_unique_random_streams({}, nworkers + 1)

        alloc_specs = {
            'alloc_f': alloc_f,
            'out': [('given_back', bool)],
            'user': {}
        }

        H, persis_info, flag = libE(sim_specs,
                                    gen_specs,
                                    self.exit_criteria,
                                    persis_info=persis_info,
                                    libE_specs=libE_specs,
                                    alloc_specs=alloc_specs)

        if MPI.COMM_WORLD.Get_rank() == 0:
            self.res['num_optimizer_evals'] = len(H['f'])
            min_index = np.argmin(H['f'])
            self.res['min_val'] = H['f'][min_index]
            self.res['opt_params'] = H['x'][min_index]
            self.res['H'] = H
            self.res['persis_info'] = persis_info
            return self.res