Exemplo n.º 1
0
def main():

    spaceopt = SpaceOpt(search_space=search_space(),
                        target_name='y',
                        objective='min')
    print(spaceopt)

    best_spoint = None
    best_y = 123456789.0

    for iteration in range(1, spaceopt.space.size + 1):

        if iteration <= 10:
            spoint = spaceopt.get_random(num_spoints=1, sample_size=1000)
            spoint_type = 'random'
        else:
            spoint = spaceopt.fit_predict(num_spoints=1, sample_size=1000)
            spoint_type = 'fit_predict'

        spoint['y'] = evaluation_function(spoint)
        spaceopt.append_evaluated_spoint(spoint)

        if spoint['y'] < best_y:
            best_y = spoint['y']
            best_spoint = spoint
            print(
                f'{iteration}, y={round(best_y, 6)}, {spoint_type}, {best_spoint}'
            )

        if best_y < -6.289:
            print('global minimum has been found\n'
                  f'{iteration}/{spaceopt.space.size} = '
                  f'{iteration / spaceopt.space.size}')
            return
Exemplo n.º 2
0
def test_SpaceOpt__sample_unevaluated_unique_spoints():
    spaceopt = SpaceOpt(search_space=search_space(),
                        target_name='y',
                        objective='min')

    try:
        spaceopt._sample_unevaluated_unique_spoints(sample_size=100.0)
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "sample_size is of type <class 'float'>, but it should be of type <class 'int'>."

    try:
        spaceopt._sample_unevaluated_unique_spoints(sample_size=0)
    except Exception as e:
        assert isinstance(e, ValueError)
        assert str(e) == "sample_size should be greater than 0."

    try:
        spaceopt._sample_unevaluated_unique_spoints(sample_size=100,
                                                    max_num_retries=10.0)
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "max_num_retries is of type <class 'float'>, but it should be of type <class 'int'>."

    try:
        spaceopt._sample_unevaluated_unique_spoints(sample_size=1,
                                                    max_num_retries=0)
    except Exception as e:
        assert isinstance(e, ValueError)
        assert str(e) == "max_num_retries should be greater than 0."

    spaceopt = SpaceOpt(search_space={
        'w': ['W1', 'W2'],
        'b': [5]
    },
                        target_name='y',
                        objective='min')
    spaceopt.append_evaluated_spoint({'w': 'W1', 'b': 5, 'y': 0.1})
    spoints = spaceopt._sample_unevaluated_unique_spoints(sample_size=100,
                                                          max_num_retries=100)
    assert len(spoints) == 1
    assert spoints[0]['w'] == 'W2'

    spaceopt.append_evaluated_spoint({'w': 'W2', 'b': 5, 'y': 0.2})
    try:
        spaceopt._sample_unevaluated_unique_spoints(sample_size=100,
                                                    max_num_retries=100)
    except Exception as e:
        assert isinstance(e, RuntimeError)
        assert str(
            e
        ) == 'could not sample any new spoints - search_space is fully explored or random sampling was unfortunate.\nsearch_space.size = 2\nnum evaluated spoints = 2\nnum unevaluated spoints = 0'
Exemplo n.º 3
0
def test_SpaceOpt_fit_predict():
    spaceopt = SpaceOpt(search_space=search_space(),
                        target_name='y',
                        objective='min')

    try:
        spaceopt.fit_predict(num_spoints=2.0)
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "num_spoints is of type <class 'float'>, but it should be of type <class 'int'>."

    try:
        spaceopt.fit_predict(num_spoints=0)
    except Exception as e:
        assert isinstance(e, ValueError)
        assert str(e) == "num_spoints should be greater than 0."

    try:
        spaceopt.fit_predict(num_boost_round=2048.0)
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "num_boost_round is of type <class 'float'>, but it should be of type <class 'int'>."

    try:
        spaceopt.fit_predict(num_boost_round=0)
    except Exception as e:
        assert isinstance(e, ValueError)
        assert str(e) == "num_boost_round should be greater than 0."

    try:
        spaceopt.fit_predict(sample_size=100.0)
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "sample_size is of type <class 'float'>, but it should be of type <class 'int'>."

    try:
        spaceopt.fit_predict(sample_size=0)
    except Exception as e:
        assert isinstance(e, ValueError)
        assert str(e) == "sample_size should be greater than 0."

    for i in range(10):
        spoint = spaceopt.fit_predict(num_spoints=1,
                                      num_boost_round=100,
                                      sample_size=100)
        assert isinstance(spoint, dict)
        spoint['y'] = random.uniform(-1, 1)
        spaceopt.append_evaluated_spoint(spoint)
Exemplo n.º 4
0
def test_SpaceOpt_append_evaluated_spoint():
    spaceopt = SpaceOpt(search_space=search_space(),
                        target_name='y',
                        objective='min')

    try:
        spaceopt.append_evaluated_spoint(['a', 'b', 'c', 'd', 'e'])
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "evaluated_spoint is of type <class 'list'>, but it should be of type <class 'dict'>."

    try:
        spaceopt.append_evaluated_spoint({
            'a': 16,
            'b': 3.3,
            'c': 512,
            'd': 'typeX',
            'e': False,
            'f': 10000
        })
    except Exception as e:
        assert isinstance(e, ValueError)
        assert str(
            e
        ) == "spoint={'a': 16, 'b': 3.3, 'c': 512, 'd': 'typeX', 'e': False, 'f': 10000} is not evaluated, target_name='y' is not found."

    try:
        spaceopt.append_evaluated_spoint({
            'a': 16,
            'b': 3.3,
            'c': 512,
            'd': 'typeX',
            'e': False,
            'f': 10000,
            'y': 1
        })
    except Exception as e:
        assert isinstance(e, TypeError)
        assert str(
            e
        ) == "evaluated_spoint has 'y' with value=1 of type <class 'int'>, but it should be of type <class 'float'>."

    spaceopt.append_evaluated_spoint({
        'a': 16,
        'b': 3.3,
        'c': 512,
        'd': 'typeX',
        'e': False,
        'f': 10000,
        'y': 1.0
    })