def test_localopt_error_saving():
    _, sim_specs_0, gen_specs_0, _, _ = setup.hist_setup1()

    H = np.zeros(4,
                 dtype=gen_out + [('f', float), ('fvec', float, 2),
                                  ('returned', bool)])
    H['x'] = np.random.uniform(0, 1, (4, 2))
    H['f'] = np.random.uniform(0, 1, 4)
    H['returned'] = True
    H['local_pt'][1:] = True
    gen_specs_0['user']['initial_sample_size'] = 1
    gen_specs_0['user']['localopt_method'] = 'scipy_COBYLA'
    gen_specs_0['user']['tol'] = 0.1
    gen_specs_0['user']['ub'] = np.ones(2)
    gen_specs_0['user']['lb'] = np.zeros(2)

    persis_info_1 = {
        'run_order': {
            0: [1, 2, 3]
        },
        'old_runs': {},
        'total_runs': 0,
        'rand_stream': np.random.RandomState(1)
    }

    try:
        al.aposmm_logic(H, persis_info_1, gen_specs_0, _)
    except Exception as e:
        assert e.args[
            0] == 'Exit code is 0, but x_new was not updated in local opt run 0 after 3 evaluations.\nSaving run information to: run_0_abort.pickle\nWorker crashing!'
    else:
        assert 0
示例#2
0
def test_trim_H():
    hist, _, _, _, _ = setup.hist_setup1(13)
    hist.index = 10
    H = hist.trim_H()
    assert np.array_equal(H, wrs), "Array does not match expected"
    assert hist.given_count == 0
    assert hist.index == 10
    assert hist.sim_count == 0
示例#3
0
def test_grow_H():
    hist, _, _, _, _ = setup.hist_setup1(3)
    new_rows = 7
    hist.grow_H(k=new_rows)
    assert np.array_equal(hist.H, wrs), "Array does not match expected"
    assert hist.given_count == 0
    assert hist.index == 0
    assert hist.sim_count == 0
def test_term_test_1():
    # termination_test should be True when we want to stop

    # sh - Should separate the tests
    # Test 1
    hist, sim_specs, gen_specs, exit_criteria, al = setup.hist_setup1()
    mgr = man.Manager(hist, libE_specs, al, sim_specs, gen_specs,
                      exit_criteria)
    assert not mgr.term_test()
示例#5
0
def test_exception_raising():
    hist, sim_specs_0, gen_specs_0, exit_criteria_0, alloc = setup.hist_setup1(n=2)
    hist.H['returned'] = 1

    for method in ['LN_SBPLX', 'pounders', 'scipy_COBYLA']:
        gen_specs_0['user']['localopt_method'] = method

        out = al.advance_local_run(hist.H, gen_specs_0['user'], 0, 0, {'run_order': {0: [0, 1]}})

        assert out[0] == 0, "Failed like it should have"
示例#6
0
def test_exception_raising():
    hist, sim_specs_0, gen_specs_0, exit_criteria_0, alloc  = setup.hist_setup1()
    hist.H['returned'] = 1

    for method in ['LN_SBPLX','pounders']:
        gen_specs_0['localopt_method'] = method
        try:
            al.advance_localopt_method(hist.H, gen_specs_0,  0, 0, {'run_order': {0:[0,1]}})
        except:
            assert 1, "Failed like it should have"
        else:
            assert 0, "Failed like it should have"
示例#7
0
def test_failing_localopt_method():
    hist, sim_specs_0, gen_specs_0, exit_criteria_0, alloc = setup.hist_setup1()

    hist.H['returned'] = 1

    gen_specs_0['user']['localopt_method'] = 'BADNAME'

    try:
        al.advance_local_run(hist.H, gen_specs_0['user'], 0, 0, {'run_order': {0: [0, 1]}})
    except al.APOSMMException:
        assert 1, "Failed like it should have"
    else:
        assert 0, "Didn't fail like it should have"
示例#8
0
def test_hist_init_1A_H0():
    hist, _, _, _, _ = setup.hist_setup1(sim_max=2, H0_in=wrs_H0)

    # Compare by column
    for field in exp_H0_H.dtype.names:
        np.array_equal(hist.H[field], exp_H0_H[field])
    #These dont work for numpy structured arrays
    #assert np.array_equiv(hist.H, exp_H0_H), "Array does not match expected"
    #assert np.array_equal(hist.H, exp_H0_H), "Array does not match expected"
    assert hist.given_count == 3
    assert hist.index == 3
    assert hist.sim_count == 3
    assert len(hist.H) == 5
示例#9
0
def test_persis_apossm_localopt_test():
    _, _, gen_specs_0, _, _ = setup.hist_setup1()

    H = np.zeros(4, dtype=[('f', float), ('returned', bool)])
    gen_specs_0['user']['localopt_method'] = 'BADNAME'
    gen_specs_0['user']['ub'] = np.ones(2)
    gen_specs_0['user']['lb'] = np.zeros(2)

    try:
        al.aposmm(H, {}, gen_specs_0, libE_specs)
    except NotImplementedError:
        assert 1, "Failed because method is unknown."
    else:
        assert 0
示例#10
0
def test_update_history_x_out():
    hist, _, _, _, _ = setup.hist_setup1()

    # First update a single point
    hist.update_history_x_out(q_inds=0, sim_worker=2)

    # Check updated values for point and counts
    assert hist.H['given'][0] == True
    assert hist.H['sim_worker'][0] == 2
    assert hist.given_count == 1

    # Check some unchanged values for point and counts
    assert hist.index == 0
    assert hist.sim_count == 0
    hist.H['returned'][0] == False
    hist.H['allocated'][0] == False
    hist.H['f'][0] == 0.0
    hist.H['sim_id'][0] == -1

    # Check the rest of H is unaffected
    assert np.array_equal(hist.H[1:10],
                          wrs[1:10]), "H Array slice does not match expected"

    # Update two further consecutive points
    my_qinds = np.arange(1, 3)
    hist.update_history_x_out(q_inds=my_qinds, sim_worker=3)

    # Check updated values for point and counts
    assert np.all(hist.H['given'][0:3])  # Include previous point
    assert np.all(hist.H['sim_worker'][my_qinds] == 3)
    assert hist.given_count == 3

    # Update three further non-consecutive points
    my_qinds = np.array([4, 7, 9])
    hist.update_history_x_out(q_inds=my_qinds, sim_worker=4)

    #Try to avoid tautological testing - compare columns
    assert np.array_equal(
        hist.H['given'],
        np.array(
            [True, True, True, False, True, False, False, True, False, True]))
    assert np.array_equal(hist.H['sim_worker'],
                          np.array([2, 3, 3, 0, 4, 0, 0, 4, 0, 4]))
    assert np.all(hist.H['returned'] == False)  # Should still be unaffected.

    # Check counts
    assert hist.given_count == 6
    assert hist.index == 0  # In real case this would be ahead.....
    assert hist.sim_count == 0
示例#11
0
def test_declare_opt():
    hist, sim_specs_0, gen_specs_0, exit_criteria_0, alloc = setup.hist_setup1(n=2)

    try:
        al.update_history_optimal(hist.H['x_on_cube'][0]+1, hist.H, np.arange(0, 10))
    except AssertionError:
        assert 1, "Failed because the best point is not in H"
    else:
        assert 0

    hist.H['x_on_cube'][1] += np.finfo(float).eps
    hist.H['f'][1] -= np.finfo(float).eps

    # Testing case where point near x_opt is slightly better.
    al.update_history_optimal(hist.H['x_on_cube'][0], hist.H, np.arange(0, 10))
    assert np.sum(hist.H['local_min']) == 2
示例#12
0
def test_update_history_optimal():
    hist, _, _, _, _ = setup.hist_setup1(n=2)

    H = hist.H

    H['returned'] = True
    H['sim_id'] = range(len(H))
    H['f'][0] = -1e-8
    H['x_on_cube'][-1] = 1e-10

    # Perturb x_opt point to test the case where the reported minimum isn't
    # exactly in H. Also, a point in the neighborhood of x_opt has a better
    # function value.
    opt_ind = al.update_history_optimal(H['x_on_cube'][-1]+1e-12, 1, H, np.arange(len(H)))

    assert opt_ind == 9, "Wrong point declared minimum"
def test_initialize_APOSMM():
    hist, sim_specs_0, gen_specs_0, exit_criteria_0, alloc = setup.hist_setup1(
    )

    al.initialize_APOSMM(hist.H, gen_specs_0)
示例#14
0
def test_hist_init_1():
    hist, _, _, _, _ = setup.hist_setup1()
    assert np.array_equal(hist.H, wrs), "Array does not match expected"
    assert hist.given_count == 0
    assert hist.index == 0
    assert hist.sim_count == 0
示例#15
0
def test_update_history_f_vec():
    hist, sim_specs, _, _, _ = setup.hist_setup1()
    exp_fs = [0.0] * 10
    exp_fvecs = [[0.0, 0.0, 0.0]] * 10

    # First update a single point
    size = 1
    sim_ids = 0  # First row to be filled
    sim_ids = np.atleast_1d(sim_ids)
    calc_out = np.zeros(size, dtype=sim_specs['out'])
    a = np.array([[1, 2, 3], [-1, 1, 4]])
    calc_out['f'] = sim_specs['sim_f'](a)  #np.linalg.norm
    calc_out['fvec'] = sim_specs['sim_f'](a, axis=0)  #np.linalg.norm
    exp_fs[0] = calc_out['f'][0]
    exp_fvecs[0] = calc_out['fvec'][0]
    D_recv = {
        'calc_out': calc_out,
        'persis_info': {},
        'libE_info': {
            'H_rows': sim_ids
        },
        'calc_status': WORKER_DONE,
        'calc_type': 2
    }

    hist.update_history_f(D_recv)

    assert isclose(exp_fs[0], hist.H['f'][0])
    assert np.allclose(exp_fvecs[0], hist.H['fvec'][0])
    assert np.all(hist.H['returned'][0:1])
    assert np.all(hist.H['returned'][1:10] == False)  #Check the rest
    assert hist.sim_count == 1
    assert hist.given_count == 0  # In real case this would be ahead.....
    assert hist.index == 0  # In real case this would be ahead....

    # Update two further consecutive points
    size = 2
    sim_ids = [1, 2]  # First row to be filled
    sim_ids = np.atleast_1d(sim_ids)
    calc_out = np.zeros(size, dtype=sim_specs['out'])

    a = np.array([[1, 3, 4], [-1, 2, 4]])
    calc_out['f'][0] = sim_specs['sim_f'](a)  #np.linalg.norm
    exp_fs[1] = calc_out['f'][0]
    calc_out['fvec'][0] = sim_specs['sim_f'](a, axis=0)  #np.linalg.norm
    exp_fvecs[1] = calc_out['fvec'][0]

    a = np.array([[2, 4, 4], [-1, 3, 4]])
    calc_out['f'][1] = sim_specs['sim_f'](a)  #np.linalg.norm
    exp_fs[2] = calc_out['f'][1]
    calc_out['fvec'][1] = sim_specs['sim_f'](a, axis=0)  #np.linalg.norm
    exp_fvecs[2] = calc_out['fvec'][1]

    D_recv = {
        'calc_out': calc_out,
        'persis_info': {},
        'libE_info': {
            'H_rows': sim_ids
        },
        'calc_status': WORKER_DONE,
        'calc_type': 2
    }

    hist.update_history_f(D_recv)

    assert np.allclose(exp_fs, hist.H['f'])
    assert np.allclose(exp_fvecs, hist.H['fvec'])
    assert np.all(hist.H['returned'][0:3])
    assert np.all(hist.H['returned'][3:10] == False)  #Check the rest
    assert hist.sim_count == 3
    assert hist.given_count == 0  # In real case this would be ahead.....
    assert hist.index == 0  # In real case this would be ahead....

    # Update two further consecutive points but with sub_array fvec componenets
    size = 2
    sim_ids = [3, 4]  # First row to be filled
    sim_ids = np.atleast_1d(sim_ids)
    calc_out = np.zeros(size, dtype=[('f', float),
                                     ('fvec', float, 2)])  # Only two values

    a = np.array([[1, 3, 4], [-1, 2, 4]])
    calc_out['f'][0] = sim_specs['sim_f'](a)  #np.linalg.norm
    exp_fs[3] = calc_out['f'][0]
    calc_out['fvec'][0][0], calc_out['fvec'][0][1], _ = sim_specs['sim_f'](
        a, axis=0)  #np.linalg.norm
    exp_fvecs[3] = [0.0, 0.0,
                    0.0]  # Point to a new array - so can fill in elements
    exp_fvecs[3][:2] = calc_out['fvec'][0]  # Change first two values

    a = np.array([[2, 4, 4], [-1, 3, 4]])
    calc_out['f'][1] = sim_specs['sim_f'](a)  #np.linalg.norm
    exp_fs[4] = calc_out['f'][1]
    calc_out['fvec'][1][0], calc_out['fvec'][1][1], _ = sim_specs['sim_f'](
        a, axis=0)  #np.linalg.norm
    exp_fvecs[4] = [0.0, 0.0,
                    0.0]  # Point to a new array - so can fill in elements
    exp_fvecs[4][:2] = calc_out['fvec'][1]  # Change first two values

    D_recv = {
        'calc_out': calc_out,
        'persis_info': {},
        'libE_info': {
            'H_rows': sim_ids
        },
        'calc_status': WORKER_DONE,
        'calc_type': 2
    }

    hist.update_history_f(D_recv)

    assert np.allclose(exp_fs, hist.H['f'])
    assert np.allclose(exp_fvecs, hist.H['fvec'])
    assert np.all(hist.H['returned'][0:5])
    assert np.all(hist.H['returned'][5:10] == False)  #Check the rest
    assert hist.sim_count == 5
    assert hist.given_count == 0  # In real case this would be ahead.....
    assert hist.index == 0  # In real case this would be ahead....