示例#1
0
def test_hammersly():
    h = Hammersly()
    x = h.generate([
        (0., 1.),
    ] * 2, 4)
    y = np.array([[0, 0], [1 / 2, 0.25], [1 / 4, 0.5], [3 / 4, 0.75]])
    assert_almost_equal(x, y)

    samples = h.generate([
        (0., 1.),
    ] * 2, 200)
    assert len(samples) == 200
    assert len(samples[0]) == 2
示例#2
0
def cook_initial_point_generator(generator, **kwargs):
    """Cook a default initial point generator.
    For the special generator called "random" the return value is None.
    Parameters
    ----------
    generator : "lhs", "sobol", "halton", "hammersly", "grid", "random" \
            or InitialPointGenerator instance"
        Should inherit from `skopt.sampler.InitialPointGenerator`.
    kwargs : dict
        Extra parameters provided to the generator at init time.
    """
    if generator is None:
        generator = "random"
    elif isinstance(generator, str):
        generator = generator.lower()
        if generator not in [
                "sobol", "halton", "hammersly", "lhs", "random", "grid",
                "maxpro", "maxpro-gd"
        ]:
            raise ValueError("Valid strings for the generator parameter "
                             " are: 'sobol', 'lhs', 'halton', 'hammersly',"
                             "'random', 'maxpro','maxpro-gd', or 'grid' not "
                             "%s." % generator)
    elif not isinstance(generator, InitialPointGenerator):
        raise ValueError("generator has to be an InitialPointGenerator."
                         "Got %s" % (str(type(generator))))

    if isinstance(generator, str):
        if generator == "sobol":
            generator = Sobol()
        elif generator == "halton":
            generator = Halton()
        elif generator == "hammersly":
            generator = Hammersly()
        elif generator == "lhs":
            generator = Lhs()
        elif generator == "grid":
            generator = Grid()
        elif generator == "random":
            return None
        elif generator == "maxpro":
            generator = MaxPro(use_gradient=False)
        elif generator == "maxpro-gd":
            generator = MaxPro(use_gradient=True)
    generator.set_params(**kwargs)
    return generator
示例#3
0
def test_hammersly():
    h = Hammersly()
    x = h.generate([
        (0., 1.),
    ] * 2, 3)
    y = np.array([[0.75, 0.125, 0.625], [0.25, 0.5, 0.75]]).T
    assert_almost_equal(x, y)
    x = h.generate([
        (0., 1.),
    ] * 2, 4)
    y = np.array([[0.75, 0.125, 0.625, 0.375], [0.2, 0.4, 0.6, 0.8]]).T
    assert_almost_equal(x, y)

    samples = h.generate([
        (0., 1.),
    ] * 2, 200)
    assert len(samples) == 200
    assert len(samples[0]) == 2
#############################################################################
# Halton sampling
# ---------------

halton = Halton()
x = halton.generate(space.dimensions, n_samples)
plot_searchspace(x, 'Halton')
pdist_data.append(pdist(x).flatten())
x_label.append("halton")

#############################################################################
# Hammersly sampling
# ------------------

hammersly = Hammersly()
x = hammersly.generate(space.dimensions, n_samples)
plot_searchspace(x, 'Hammersly')
pdist_data.append(pdist(x).flatten())
x_label.append("hammersly")

#############################################################################
# Grid sampling
# -------------

grid = Grid(border="include", use_full_layout=False)
x = grid.generate(space.dimensions, n_samples)
plot_searchspace(x, 'Grid')
pdist_data.append(pdist(x).flatten())
x_label.append("grid")