Beispiel #1
0
    def generate_points(self, lb=None, ub=None, int_var=None):
        """Generate a two factorial design in the unit hypercube.

        You can specify lb, ub, int_var to have the design mapped to a
        specific domain. These inputs are ignored if one of lb
        or ub is None. The design is generated in [0, 1]^d in this case.

        :param lb: Lower bounds
        :type lb: numpy.array
        :param ub: Upper bounds
        :type ub: numpy.array
        :param int_var: Indices of integer variables. If None, [], or
                        np.array([]) we assume all variables are continuous.
        :type int_var: numpy.array

        :return: Two factorial design in unit hypercube of size num_pts x dim
        :rtype: numpy.array
        """
        if int_var is None or len(int_var) == 0:
            int_var = np.array([])

        X = np.array(list(itertools.product([0, 1], repeat=self.dim)))
        if all([x is not None for x in [lb, ub]]):  # Map and round
            X = round_vars(from_unit_box(X, lb, ub), int_var, lb, ub)
        return X
def _expdes_dist(gen, iterations, lb, ub, int_var):
    """Helper method for picking the best experimental design.
    We generate iterations designs and picks the one the maximizes the
    minimum distance between points. This isn't a perfect criterion, but
    it will help avoid rank-defficient designs such as y=x.
    :param lb: Lower bounds
    :type lb: numpy.array
    :param ub: Upper bounds
    :type ub: numpy.array
    :param int_var: Indices of integer variables.
    :type int_var: numpy.array
    :return: Experimental design of size num_pts x dim
    :rtype: numpy.ndarray
    """

    X = None
    best_score = 0
    for _ in range(iterations):
        cand = gen()  # Generate a new design
        if all([x is not None for x in [lb, ub]]):  # Map and round
            cand = round_vars(from_unit_box(cand, lb, ub), int_var, lb, ub)

        dists = cdist(cand, cand)
        np.fill_diagonal(dists, np.inf)  # Since these are zero
        score = dists.min().min()

        if score > best_score and rank(cand) == cand.shape[1]:
            best_score = score
            X = cand.copy()

    if X is None:
        raise ValueError("No valid design found, increase num_pts?")
    return X
Beispiel #3
0
    def generate_points(self, lb=None, ub=None, int_var=None):
        """Generate a two factorial design in the unit hypercube.

        You can specify lb, ub, int_var to have the design mapped to a
        specific domain. These inputs are ignored if one of lb
        or ub is None. The design is generated in [0, 1]^d in this case.

        :param lb: Lower bounds
        :type lb: numpy.array
        :param ub: Upper bounds
        :type ub: numpy.array
        :param int_var: Indices of integer variables. If None, [], or
                        np.array([]) we assume all variables are continuous.
        :type int_var: numpy.array

        :return: Two factorial design in unit hypercube of size num_pts x dim
        :rtype: numpy.array
        """
        if int_var is None or len(int_var) == 0:
            int_var = np.array([])

        X = np.array(list(itertools.product([0, 1], repeat=self.dim)))
        if all([x is not None for x in [lb, ub]]):  # Map and round
            X = round_vars(from_unit_box(X, lb, ub), int_var, lb, ub)
        return X
Beispiel #4
0
def test_unit_box_map():
    X = np.random.rand(5, 3)
    lb = -1 * np.ones((3, ))
    ub = 2 * np.ones((3, ))

    X1 = to_unit_box(X, lb, ub)
    np.testing.assert_equal(X.shape, X1.shape)
    np.testing.assert_almost_equal(
        X1, to_unit_box(X, np.atleast_2d(lb), np.atleast_2d(ub)))
    assert (X.max() <= 1.0 and X.min() >= 0)

    # Try to map back to what we started with
    X2 = from_unit_box(X1, lb, ub)
    np.testing.assert_equal(X.shape, X2.shape)
    np.testing.assert_almost_equal(
        X2, from_unit_box(X1, np.atleast_2d(lb), np.atleast_2d(ub)))
    np.testing.assert_almost_equal(X2, X)
Beispiel #5
0
def test_unit_box_map():
    X = np.random.rand(5, 3)
    lb = -1 * np.ones((3,))
    ub = 2 * np.ones((3,))

    X1 = to_unit_box(X, lb, ub)
    np.testing.assert_equal(X.shape, X1.shape)
    np.testing.assert_almost_equal(
        X1, to_unit_box(X, np.atleast_2d(lb), np.atleast_2d(ub)))
    assert(X.max() <= 1.0 and X.min() >= 0)

    # Try to map back to what we started with
    X2 = from_unit_box(X1, lb, ub)
    np.testing.assert_equal(X.shape, X2.shape)
    np.testing.assert_almost_equal(
        X2, from_unit_box(X1, np.atleast_2d(lb), np.atleast_2d(ub)))
    np.testing.assert_almost_equal(X2, X)
Beispiel #6
0
    def get_x(self):
        """Get the list of data points

        :return: List of data points
        :rtype: numpy.array
        """

        return from_unit_box(self.model.get_x(), self.data)
Beispiel #7
0
def _expdes_dist(gen, iterations, lb, ub, int_var):
    """Helper method for picking the best experimental design.

    We generate iterations designs and picks the one the maximizes the
    minimum distance between points. This isn't a perfect criterion, but
    it will help avoid rank-defficient designs such as y=x.

    :param lb: Lower bounds
    :type lb: numpy.array
    :param ub: Upper bounds
    :type ub: numpy.array
    :param int_var: Indices of integer variables.
    :type int_var: numpy.array

    :return: Experimental design of size num_pts x dim
    :rtype: numpy.ndarray
    """

    X = None
    best_score = 0
    for _ in range(iterations):
        cand = gen()  # Generate a new design
        if all([x is not None for x in [lb, ub]]):  # Map and round
            cand = round_vars(from_unit_box(cand, lb, ub), int_var, lb, ub)

        dists = cdist(cand, cand)
        np.fill_diagonal(dists, np.inf)  # Since these are zero
        score = dists.min().min()

        if score > best_score and rank(cand) == cand.shape[1]:
            best_score = score
            X = cand.copy()

    if X is None:
        raise ValueError("No valid design found, increase num_pts?")
    return X