def test_distance(self): """Verify that distance is 0 iff two points are the same.""" self.assertEqual(ru.distance(np.array([i*5 for i in range(15)]), np.array([i*5 for i in range(15)])), 0.0, msg='Distance is not zero at equal points') self.assertNotEqual(ru.distance(np.array([i*5 for i in range(15)]), np.array([i*5 + 0.001 for i in range(15)])), 0.0, msg='Distance is nonzero at diff points') self.assertNotEqual(ru.distance(np.array([-i*5 for i in range(15)]), np.array([-i*5 + 0.001 for i in range(15)])), 0.0, msg='Distance is nonzero at diff points')
def test_distance(self): """Verify that distance is 0 iff two points are the same.""" self.assertEqual(ru.distance(np.array([i * 5 for i in range(15)]), np.array([i * 5 for i in range(15)])), 0.0, msg='Distance is not zero at equal points') self.assertNotEqual(ru.distance( np.array([i * 5 for i in range(15)]), np.array([i * 5 + 0.001 for i in range(15)])), 0.0, msg='Distance is nonzero at diff points') self.assertNotEqual(ru.distance( np.array([-i * 5 for i in range(15)]), np.array([-i * 5 + 0.001 for i in range(15)])), 0.0, msg='Distance is nonzero at diff points')
def get_integer_candidate(settings, n, k, h, start_point, tr_radius, candidate, integer_vars): """Get integer candidate point from a fractional point. Look for integer points around the given fractional point, trying to find one with a good value of the quadratic model. Parameters ---------- settings : :class:`rbfopt_settings.RbfoptSettings`. Global and algorithmic settings. n : int Dimension of the problem, i.e. the size of the space. k : int Number of interpolation nodes. h : 1D numpy.ndarray[float] Linear coefficients of the model. start_point : 1D numpy.ndarray[float] Starting point for the descent. tr_radius : float Radius of the trust region. candidate : 1D numpy.ndarray[float] Fractional point to being the search. integer_vars : 1D numpy.ndarray[int] Indices of the integer variables. Returns ------- (1D numpy.ndarray[float], float) Next candidate point for the search, and the corresponding change in model value compared to the given point. """ assert(isinstance(candidate, np.ndarray)) assert(len(candidate) == n) assert(isinstance(h, np.ndarray)) assert(len(h) == n) assert(isinstance(integer_vars, np.ndarray)) assert(isinstance(settings, RbfoptSettings)) # Compute the rounding down and up floor = np.floor(candidate[integer_vars]) ceil = np.ceil(candidate[integer_vars]) curr_point = np.copy(candidate) curr_point[integer_vars] = np.where(h[integer_vars] >= 0, ceil, floor) best_value = np.dot(h, curr_point) best_point = np.copy(curr_point) for i in range(n * settings.tr_num_integer_candidates): # We round each integer variable up or down depending on its # fractional value and a uniform random number curr_point[integer_vars] = np.where( np.random.uniform(size=len(integer_vars)) < candidate[integer_vars] - floor, ceil, floor) curr_value = np.dot(h, curr_point) if (ru.distance(curr_point, start_point) <= tr_radius and curr_value < best_value): best_value = curr_value best_point = np.copy(curr_point) return (best_point, np.dot(h, candidate) - best_value)
def get_integer_candidate(settings, n, k, h, start_point, tr_radius, candidate, integer_vars): """Get integer candidate point from a fractional point. Look for integer points around the given fractional point, trying to find one with a good value of the quadratic model. Parameters ---------- settings : :class:`rbfopt_settings.RbfoptSettings`. Global and algorithmic settings. n : int Dimension of the problem, i.e. the size of the space. k : int Number of interpolation nodes. h : 1D numpy.ndarray[float] Linear coefficients of the model. start_point : 1D numpy.ndarray[float] Starting point for the descent. tr_radius : float Radius of the trust region. candidate : 1D numpy.ndarray[float] Fractional point to being the search. integer_vars : 1D numpy.ndarray[int] Indices of the integer variables. Returns ------- (1D numpy.ndarray[float], float) Next candidate point for the search, and the corresponding change in model value compared to the given point. """ assert (isinstance(candidate, np.ndarray)) assert (len(candidate) == n) assert (isinstance(h, np.ndarray)) assert (len(h) == n) assert (isinstance(integer_vars, np.ndarray)) assert (isinstance(settings, RbfoptSettings)) # Compute the rounding down and up floor = np.floor(candidate[integer_vars]) ceil = np.ceil(candidate[integer_vars]) curr_point = np.copy(candidate) curr_point[integer_vars] = np.where(h[integer_vars] >= 0, ceil, floor) best_value = np.dot(h, curr_point) best_point = np.copy(curr_point) for i in range(n * settings.tr_num_integer_candidates): # We round each integer variable up or down depending on its # fractional value and a uniform random number curr_point[integer_vars] = np.where( np.random.uniform(size=len(integer_vars)) < candidate[integer_vars] - floor, ceil, floor) curr_value = np.dot(h, curr_point) if (ru.distance(curr_point, start_point) <= tr_radius and curr_value < best_value): best_value = curr_value best_point = np.copy(curr_point) return (best_point, np.dot(h, candidate) - best_value)
def get_integer_candidate(settings, n, k, h, start_point, ref_radius, candidate, integer_vars, categorical_info): """Get integer candidate point from a fractional point. Look for integer points around the given fractional point, trying to find one with a good value of the linear model. Parameters ---------- settings : :class:`rbfopt_settings.RbfoptSettings`. Global and algorithmic settings. n : int Dimension of the problem, i.e. the size of the space. k : int Number of interpolation nodes. h : 1D numpy.ndarray[float] Linear coefficients of the model. start_point : 1D numpy.ndarray[float] Starting point for the descent. ref_radius : float Radius of the local search. candidate : 1D numpy.ndarray[float] Fractional point to being the search. integer_vars : 1D numpy.ndarray[int] Indices of the integer variables. categorical_info : (1D numpy.ndarray[int], 1D numpy.ndarray[int], List[(int, 1D numpy.ndarray[int])]) or None Information on categorical variables: array of indices of categorical variables in original space, array of indices of noncategorical variables in original space, and expansion of each categorical variable, given as a tuple (original index, indices of expanded variables). Returns ------- (1D numpy.ndarray[float], float) Next candidate point for the search, and the corresponding change in model value compared to the given point. """ assert (isinstance(candidate, np.ndarray)) assert (len(candidate) == n) assert (isinstance(h, np.ndarray)) assert (len(h) == n) assert (isinstance(integer_vars, np.ndarray)) assert (isinstance(settings, RbfoptSettings)) # If there are categorical variables, they have to be dealt with # separately. Exclude them from the set of integer vars. if (categorical_info is not None and categorical_info[2]): categorical, not_categorical, expansion = categorical_info integer_vars = np.array( [i for i in integer_vars if i < len(not_categorical)], dtype=np.int_) # Compute the rounding down and up floor = np.floor(candidate[integer_vars]) ceil = np.ceil(candidate[integer_vars]) curr_point = np.copy(candidate) curr_point[integer_vars] = np.where(h[integer_vars] >= 0, ceil, floor) if (categorical_info is not None and categorical_info[2]): # Round in-place round_categorical(curr_point, categorical, not_categorical, expansion) best_value = np.dot(h, curr_point) best_point = np.copy(curr_point) for i in range(n * settings.ref_num_integer_candidates): # We round each integer variable up or down depending on its # fractional value and a uniform random number curr_point[integer_vars] = np.where( np.random.uniform(size=len(integer_vars)) < candidate[integer_vars] - floor, ceil, floor) if (categorical_info is not None and categorical_info[2]): curr_point[len(not_categorical):] = candidate[len(not_categorical ):] # Round in-place round_categorical(curr_point, categorical, not_categorical, expansion) curr_value = np.dot(h, curr_point) if (ru.distance(curr_point, start_point) <= ref_radius and curr_value < best_value): best_value = curr_value best_point = np.copy(curr_point) return (best_point, np.dot(h, candidate) - best_value)