def test_rbf_interpolation(self): """Test interpolation conditions. Verify that the RBF interpolates at points. """ settings = RbfoptSettings() for i in range(20): dim = np.random.randint(1, 20) num_points = np.random.randint(dim + 1, 50) node_pos = np.random.uniform(-100, 100, size=(num_points, dim)) node_val = np.random.uniform(0, 100, num_points) # Possible shapes of the matrix for rbf_type in self.rbf_types: settings.rbf = rbf_type mat = ru.get_rbf_matrix(settings, dim, num_points, node_pos) rbf_l, rbf_h = ru.get_rbf_coefficients(settings, dim, num_points, mat, node_val) for i in range(num_points): value = ru.evaluate_rbf(settings, node_pos[i], dim, num_points, node_pos, rbf_l, rbf_h) self.assertAlmostEqual(value, node_val[i], places=3, msg='Interpolation failed' + 'with rbf ' + rbf_type)
def test_minimize_rbf_random(self): """Check solution of RBF minimization problem on random instances. This function verifies that the solution of the RBF minimization problem on small random problems is always no worse than he best interpolation node. """ for i in range(10): n = np.random.randint(4, 10) k = np.random.randint(n + 1, n + 10) var_lower = np.array([-5] * n) var_upper = np.array([5] * n) integer_vars = np.sort(np.random.choice(n, np.random.randint(n))) node_pos = np.random.uniform(-5, 5, size=(k, n)) node_pos[:, integer_vars] = np.around(node_pos[:, integer_vars]) node_val = np.random.uniform(-10, 10, size=k) best_node_pos = np.argmin(node_val) for rbf_type in self.rbf_types: settings = RbfoptSettings(rbf=rbf_type) A = ru.get_rbf_matrix(settings, n, k, node_pos) rbf_l, rbf_h = ru.get_rbf_coefficients(settings, n, k, A, node_val) sol = aux.minimize_rbf(settings, n, k, var_lower, var_upper, integer_vars, None, node_pos, rbf_l, rbf_h, node_pos[best_node_pos]) val = ru.evaluate_rbf(settings, sol, n, k, node_pos, rbf_l, rbf_h) self.assertLessEqual(val, node_val[best_node_pos] + 1.0e-3, msg='The minimize_rbf solution' + ' is worse than starting point' + ' with rbf ' + rbf_type)
def test_get_rbf_matrix(self): """Test basic properties of the RBF matrix (e.g. symmetry, size). Verify that the RBF matrix is symmetric and it has the correct size for all types of RBF. """ settings = RbfoptSettings() for i in range(50): dim = np.random.randint(1, 20) num_points = np.random.randint(10, 50) node_pos = np.random.uniform(-100, 100, size=(num_points, dim)) # Possible shapes of the matrix for rbf_type in self.rbf_types: settings.rbf = rbf_type mat = ru.get_rbf_matrix(settings, dim, num_points, node_pos) self.assertIsInstance(mat, np.ndarray) self.assertEqual(len(mat.shape), 2) self.assertAlmostEqual(np.max(mat - mat.transpose()), 0.0, msg='RBF matrix is not symmetric') size = num_points if (ru.get_degree_polynomial(settings) >= 0): size += 1 if (ru.get_degree_polynomial(settings) > 0): size += dim**ru.get_degree_polynomial(settings) self.assertEqual(mat.shape, (size, size)) # Check that exception is raised for unknown RBF types settings.rbf = 'unknown' self.assertRaises(ValueError, ru.get_rbf_matrix, settings, dim, num_points, node_pos)
def test_get_rbf_matrix(self): """Test basic properties of the RBF matrix (e.g. symmetry, size). Verify that the RBF matrix is symmetric and it has the correct size for all types of RBF. """ settings = RbfoptSettings() for i in range(50): dim = np.random.randint(1, 20) num_points = np.random.randint(10, 50) node_pos = np.random.uniform(-100, 100, size=(num_points,dim)) # Possible shapes of the matrix for rbf_type in self.rbf_types: settings.rbf = rbf_type mat = ru.get_rbf_matrix(settings, dim, num_points, node_pos) self.assertIsInstance(mat, np.matrix) self.assertAlmostEqual(np.max(mat - mat.transpose()), 0.0, msg='RBF matrix is not symmetric') size = num_points if (ru.get_degree_polynomial(settings) >= 0): size += 1 if (ru.get_degree_polynomial(settings) > 0): size += dim ** ru.get_degree_polynomial(settings) self.assertEqual(mat.shape, (size, size)) # Check that exception is raised for unknown RBF types settings.rbf = 'unknown' self.assertRaises(ValueError, ru.get_rbf_matrix, settings, dim, num_points, node_pos)
def evaluateRBF(input): #Single input for parallel processing points, model = input # Write error message if len(points) == 0: sys.stdout.write("No points to evaluate!\n") sys.stdout.flush() # Number of nodes at current iterbfopt_algtion k = len(model.node_pos) # Compute indices of fast node evaluations (sparse format) fast_node_index = (np.nonzero(model.node_is_fast)[0] if model.two_phase_optimization else np.array([])) # Rescale nodes if necessary tfv = rbfopt_utils.transform_function_values(model.l_settings, np.array(model.node_val), model.fmin, model.fmax, fast_node_index) (scaled_node_val, scaled_fmin, scaled_fmax, node_err_bounds, rescale_function) = tfv # Compute the matrices necessary for the algorithm Amat = rbfopt_utils.get_rbf_matrix(model.l_settings, model.n, k, np.array(model.node_pos)) # Get coefficients for the exact RBF rc = rbfopt_utils.get_rbf_coefficients(model.l_settings, model.n, k, Amat, scaled_node_val) if (fast_node_index): # RBF with some fast function evaluations rc = aux.get_noisy_rbf_coefficients(model.l_settings, model.n, k, Amat[:k, :k], Amat[:k, k:], scaled_node_val, fast_node_index, node_err_bounds, rc[0], rc[1]) (rbf_l, rbf_h) = rc # Evaluate RBF if len(points) <= 1: values = [] for point in points: values.append( rbfopt_util.evaluate_rbf(model.l_settings, point, model.n, k, np.array(model.node_pos), rbf_l, rbf_h)) return values else: return rbfopt_utils.bulk_evaluate_rbf(model.l_settings, np.array(points), model.n, k, np.array(model.node_pos), rbf_l, rbf_h)
def test_get_model_quality_estimate(self): """Test the get_model_quality_estimate function. """ for rbf in [ 'cubic', 'thin_plate_spline', 'multiquadric', 'linear', 'gaussian' ]: settings = RbfoptSettings(rbf=rbf) error = ru.get_model_quality_estimate(settings, self.n, self.k, self.node_pos, self.node_val, self.k) # Create a copy of the interpolation nodes and values sorted_idx = self.node_val.argsort() sorted_node_val = self.node_val[sorted_idx] # Initialize the arrays used for the cross-validation cv_node_pos = self.node_pos[sorted_idx[1:]] cv_node_val = self.node_val[sorted_idx[1:]] # The node that was left out rm_node_pos = self.node_pos[sorted_idx[0]] rm_node_val = self.node_val[sorted_idx[0]] # Estimate of the model error loo_error = 0.0 for i in range(self.k): # Compute the RBF interpolant with one node left out Amat = ru.get_rbf_matrix(settings, self.n, self.k - 1, cv_node_pos) rbf_l, rbf_h = ru.get_rbf_coefficients(settings, self.n, self.k - 1, Amat, cv_node_val) # Compute value of the interpolant at the removed node predicted_val = ru.evaluate_rbf(settings, rm_node_pos, self.n, self.k - 1, cv_node_pos, rbf_l, rbf_h) # Update leave-one-out error loc = np.searchsorted(sorted_node_val, predicted_val) loo_error += abs(loc - i) # Update the node left out if (i < self.k - 1): tmp = cv_node_pos[i].copy() cv_node_pos[i] = rm_node_pos rm_node_pos = tmp cv_node_val[i], rm_node_val = rm_node_val, cv_node_val[i] self.assertAlmostEqual(loo_error, error, msg='Model selection procedure ' + 'miscomputed the error')
def test_rbf_interpolation_cat(self): """Test interpolation conditions with categorical variables. Verify that the RBF interpolates at points. """ settings = RbfoptSettings() for i in range(20): dim = np.random.randint(5, 15) cat_dim = 8 # We need enough points to ensure the system is not singular num_points = np.random.randint(2 * (dim + cat_dim), 60) node_pos = np.hstack((np.random.uniform(-100, 100, size=(num_points, dim)), np.zeros(shape=(num_points, cat_dim)))) # Pick random categorical values for j in range(num_points): node_pos[j, dim + np.random.choice(4)] = 1 node_pos[j, dim + 4 + np.random.choice(4)] = 1 categorical_info = (np.array([0, 1]), np.array([j + 2 for j in range(dim)]), [(0, 0, np.array([dim + j for j in range(4)])), (1, 0, np.array([dim + 4 + j for j in range(4)]))]) node_val = np.random.uniform(0, 100, num_points) # Possible shapes of the matrix for rbf_type in self.rbf_types: settings.rbf = rbf_type mat = ru.get_rbf_matrix(settings, dim + cat_dim, num_points, node_pos) rbf_l, rbf_h = ru.get_rbf_coefficients(settings, dim + cat_dim, num_points, mat, node_val, categorical_info) for i in range(num_points): value = ru.evaluate_rbf(settings, node_pos[i], dim + cat_dim, num_points, node_pos, rbf_l, rbf_h) self.assertAlmostEqual(value, node_val[i], places=4, msg='Interpolation failed ' + 'with rbf ' + rbf_type)
def test_get_model_quality_estimate(self): """Test the get_model_quality_estimate function. """ for rbf in ['cubic', 'thin_plate_spline', 'multiquadric', 'linear', 'gaussian']: settings = RbfoptSettings(rbf=rbf) error = ru.get_model_quality_estimate( settings, self.n, self.k, self.node_pos, self.node_val, self.k) # Create a copy of the interpolation nodes and values sorted_idx = self.node_val.argsort() sorted_node_val = self.node_val[sorted_idx] # Initialize the arrays used for the cross-validation cv_node_pos = self.node_pos[sorted_idx[1:]] cv_node_val = self.node_val[sorted_idx[1:]] # The node that was left out rm_node_pos = self.node_pos[sorted_idx[0]] rm_node_val = self.node_val[sorted_idx[0]] # Estimate of the model error loo_error = 0.0 for i in range(self.k): # Compute the RBF interpolant with one node left out Amat = ru.get_rbf_matrix(settings, self.n, self.k-1, cv_node_pos) rbf_l, rbf_h = ru.get_rbf_coefficients( settings, self.n, self.k-1, Amat, cv_node_val) # Compute value of the interpolant at the removed node predicted_val = ru.evaluate_rbf(settings, rm_node_pos, self.n, self.k-1, cv_node_pos, rbf_l, rbf_h) # Update leave-one-out error loc = np.searchsorted(sorted_node_val, predicted_val) loo_error += abs(loc - i) # Update the node left out if (i < self.k - 1): tmp = cv_node_pos[i].copy() cv_node_pos[i] = rm_node_pos rm_node_pos = tmp cv_node_val[i], rm_node_val = rm_node_val, cv_node_val[i] self.assertAlmostEqual(loo_error, error, msg='Model selection procedure ' + 'miscomputed the error')
def test_rbf_interpolation(self): """Test interpolation conditions. Verify that the RBF interpolates at points. """ settings = RbfoptSettings() for i in range(20): dim = np.random.randint(1, 20) num_points = np.random.randint(10, 50) node_pos = np.random.uniform(-100, 100, size=(num_points,dim)) node_val = np.random.uniform(0, 100, num_points) # Possible shapes of the matrix for rbf_type in self.rbf_types: settings.rbf = rbf_type mat = ru.get_rbf_matrix(settings, dim, num_points, node_pos) rbf_l, rbf_h = ru.get_rbf_coefficients( settings, dim, num_points, mat, node_val) for i in range(num_points): value = ru.evaluate_rbf(settings, node_pos[i], dim, num_points, node_pos, rbf_l, rbf_h) self.assertAlmostEqual(value, node_val[i], places=4, msg='Interpolation failed' + 'with rbf ' + rbf_type)