NumPy parameters#

For NumPy array parameters, the functions pyhopper.float() and pyhopper.int() provide a shape argument. In default case shape = None the parameter created are scalar types (Python int and float types). If shape is a tuple of integers the created parameter values will be of np.ndarray type with dtype=np.int64 and dtype=np.float32 respectively.

import pyhopper

def dummy_of(param):
    print(param)
    return 0

search = pyhopper.Search(
    {
        "scalar": pyhopper.float(-1, 1),
        "1d": pyhopper.float(-1, 1, shape=3),
        "2d": pyhopper.float(-1, 1, shape=(2, 2)),
    }
)
search.run(dummy_of, steps=3, quiet=True)

produces

> {'scalar': -0.430359, '1d': array([0.53367, 0.80678, 0.10515]), '2d': array([[-0.75503,  0.28752],  [ 0.1958 ,  0.53757]])}
> {'scalar': 0.443020, '1d': array([ 0.47137, -0.21797,  0.31202]), '2d': array([[-0.11824,  0.16386], [ 0.57913, -0.34669]])}
> {'scalar': -0.158847, '1d': array([ 0.22458,  0.66483, -0.45764]), '2d': array([[ 0.40102, -0.29829], [-0.35151, -0.16981]])}

Same works for integers and in combination with constraints

search = pyhopper.Search(
    {
        "0d_int": pyhopper.int(0, 10),
        "1d_int": pyhopper.int(2, 16, shape=3, power_of=2),
        "2d_int": pyhopper.int(0, 20, shape=(2, 2), multiple_of=5),
    }
)
search.run(dummy_of, steps=3, quiet=True)
> {'0d_int': 8, '1d_int': array([ 8,  4, 16]), '2d_int': array([[15,  5], [20, 10]])}
> {'0d_int': 9, '1d_int': array([ 8,  4, 16]), '2d_int': array([[15,  0], [15, 15]])}
> {'0d_int': 6, '1d_int': array([16,  2,  8]), '2d_int': array([[20,  5], [15, 15]])}