Esempio 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
Esempio n. 2
0
def test_SpaceOpt_get_random():
    spaceopt = SpaceOpt(search_space=search_space(),
                        target_name='y',
                        objective='min')

    try:
        spaceopt.get_random(num_spoints=1.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.get_random(num_spoints=0)
    except Exception as e:
        assert isinstance(e, ValueError)
        assert str(e) == "num_spoints should be greater than 0."

    try:
        spaceopt.get_random(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.get_random(sample_size=0)
    except Exception as e:
        assert isinstance(e, ValueError)
        assert str(e) == "sample_size should be greater than 0."

    spoints = spaceopt.get_random(num_spoints=5, sample_size=1000)
    assert isinstance(spoints, list)
    assert len(spoints) == 5

    spoint = spaceopt.get_random(num_spoints=1, sample_size=10)
    assert isinstance(spoint, dict)
    assert set(spoint.keys()) == set(spoints[-1].keys())