def test_transform_domain(self): """Check that affine transformation does not hang on limit case. Further check that 'off' transformation returns the point as is, and unimplemented strategies raise a ValueError. """ settings = RbfSettings() settings.domain_scaling = 'affine' var_lower = np.array([i for i in range(5)] + [i for i in range(5)]) var_upper = np.array([i for i in range(5)] + [i + 10 for i in range(5)]) point = np.array([i for i in range(5)] + [i + 2 * i for i in range(5)]) # Test what happend when lower and upper bounds coincide transf_point = ru.transform_domain(settings, var_lower, var_upper, point) orig_point = ru.transform_domain(settings, var_lower, var_upper, transf_point, True) for i in range(10): msg = 'Exceeding lower bound on affine domain scaling' self.assertLessEqual(0.0, transf_point[i], msg=msg) msg = 'Exceeding upper bound on affine domain scaling' self.assertLessEqual(transf_point[i], 1.0, msg=msg) msg = 'Doubly transformed point does not match original' self.assertAlmostEqual(point[i], orig_point[i], 12, msg=msg) # Check that 'off' scaling does not do anything settings.domain_scaling = 'off' transf_point = ru.transform_domain(settings, var_lower, var_upper, point) for i in range(10): msg = 'Transformed point with \'off\' does not match original' self.assertEqual(point[i], transf_point[i], msg=msg) # Check that unimplemented strategies are rejected settings.domain_scaling = 'test' self.assertRaises(ValueError, ru.transform_domain, settings, var_lower, var_upper, point)
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 = RbfSettings() for i in range(50): dim = random.randint(1, 20) num_points = random.randint(10, 50) node_pos = np.array( [[random.uniform(-100, 100) for j in range(dim)] for k in range(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) self.assertIsInstance(mat, np.matrix) self.assertAlmostEqual(np.max(mat - mat.transpose()), 0.0, msg='RBF matrix is not symmetric') size = num_points + 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_degree_polynomial(self): """Verify that the degree is always between 0 and 1.""" settings = RbfSettings() for rbf_type in self.rbf_types: settings.rbf = rbf_type degree = ru.get_degree_polynomial(settings) self.assertTrue(degree == 0 or degree == 1)
def test_get_size_P_matrix(self): """Verify that the size is always between 0 and n+1.""" settings = RbfSettings() for rbf_type in self.rbf_types: settings.rbf = rbf_type for n in range(20): size = ru.get_size_P_matrix(settings, n) self.assertTrue(0 <= size <= n + 1)
def test_transform_domain_bounds(self): """Check that domain bounds are consistent.""" list_scaling = [ val for val in RbfSettings._allowed_domain_scaling if val != 'auto' ] for scaling in list_scaling: settings = RbfSettings(domain_scaling=scaling) # Test limit case with empty bounds vl, vu = ru.transform_domain_bounds(settings, np.array([]), np.array([])) msg = 'Failed transform_domain_bounds on empty bounds' self.assertEqual(len(vl), 0, msg=msg) self.assertEqual(len(vu), 0, msg=msg) msg = 'Bounds inconsistent with random bounds' for i in range(10): dim = random.randint(0, 20) var_lower = np.array( [random.uniform(-100, 100) for j in range(dim)]) var_upper = np.array([ var_lower[j] + random.uniform(0, 100) for j in range(dim) ]) vl, vu = ru.transform_domain_bounds(settings, var_lower, var_upper) self.assertEqual(len(vl), len(var_lower), msg=msg) self.assertEqual(len(vu), len(var_upper), msg=msg) for j in range(dim): self.assertLessEqual(vl[j], vu[j], msg=msg)
def setUp(self): """Generate data to simulate an optimization problem.""" self.settings = RbfSettings(rbf = 'multiquadric') self.n = 3 self.k = 5 self.var_lower = np.array([i for i in range(self.n)]) self.var_upper = np.array([i + 10 for i in range(self.n)]) self.node_pos = np.array([self.var_lower, self.var_upper, [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12]]) self.node_val = np.array([2*i for i in range(self.k)]) Amat = [[1.0, 17.349351572897476, 1.9999999999999998, 12.009995836801943, 12.932517156377562, 1.0], [17.349351572897476, 1.0, 15.620499351813308, 6.945502141674135, 6.103277807866851, 1.0], [1.9999999999999998, 15.620499351813308, 1.0, 10.374969879474351, 11.280514172678478, 1.0], [12.009995836801943, 6.945502141674135, 10.374969879474351, 1.0, 5.243090691567331, 1.0], [12.932517156377562, 6.103277807866851, 11.280514172678478, 5.243090691567331, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.0]] self.Amat = np.matrix(Amat) self.Amatinv = self.Amat.getI() self.rbf_lambda = np.array([1.981366489986409, 0.6262004309283905, -1.8477896263093248, -0.10028069928913483, -0.65949659531634]) self.rbf_h = np.array([0.5833631458309435]) self.integer_vars = np.array([1])
def test_gutmann_ex8_1_4_log(self): """Check solution of ex8_1_4 with Gutmann, log scaling, infstep. Sampling-based global search. """ bb = ti.TestBlackBox('ex8_1_4') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving branin with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='Gutmann', rbf='multiquadric', global_search_method='sampling', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, function_scaling='log', do_infstep=True, rand_seed=seed) alg = ra.OptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve branin with Gutmann\'s algorithm' target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg)
def setUp(self): """Generate data to simulate an optimization problem.""" self.settings = RbfSettings(rbf = 'linear') self.n = 3 self.k = 5 self.var_lower = np.array([i for i in range(self.n)]) self.var_upper = np.array([i + 10 for i in range(self.n)]) self.node_pos = np.array([self.var_lower, self.var_upper, [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12]]) self.node_val = np.array([2*i for i in range(self.k)]) Amat = [[0.0, 17.320508075688775, 1.7320508075688772, 11.968291440301744, 12.893796958227627, 1.0], [17.320508075688775, 0.0, 15.588457268119896, 6.873136110975833, 6.020797289396148, 1.0], [1.7320508075688772, 15.588457268119896, 0.0, 10.32666451474047, 11.236102527122116, 1.0], [11.968291440301744, 6.873136110975833, 10.32666451474047, 0.0, 5.146843692983108, 1.0], [12.893796958227627, 6.020797289396148, 11.236102527122116, 5.146843692983108, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.0]] self.Amat = np.matrix(Amat) self.Amatinv = self.Amat.getI() self.rbf_lambda = np.array([1.1704846814048488, 0.5281643269521171, -0.9920149389974761, -0.1328847504999134, -0.5737493188595765]) self.rbf_h = np.array([1.5583564301976252]) self.integer_vars = np.array([1])
def test_msrsm_parallel_st_miqp3_no_local_search(self): """Check solution of st_miqp3 with MSRSM and no local search. Solver solution of global search problems.""" bb = ti.TestBlackBox('st_miqp3') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving st_miqp3 with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(rbf='cubic', global_search_method='genetic', algorithm='MSRSM', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, num_cpus=4, local_search_box_scaling=10000, rand_seed=seed) alg = ra.OptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve st_miqp3 with MSRSM algorithm' target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg)
def test_gutmann_branin_noisy_with_init(self): """Check solution of noisy braning with Gutmann, solver.""" bb = ti.TestNoisyBlackBox('branin', 0.1, 0.01) optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving branin with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='Gutmann', global_search_method='solver', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, fast_objfun_rel_error=0.1, fast_objfun_abs_error=0.01, rand_seed=seed) init_node_pos = [[0, 0], [-2, 2], [5, 10]] init_node_val = [bb._function.evaluate(x) for x in init_node_pos] alg = ra.OptAlgorithm(settings, bb, init_node_pos, init_node_val) res = alg.optimize() msg = ('Could not solve noisy branin with init and ' + 'Gutmann\'s algorithm') target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg)
def test_state_reload(self): """Check solution of ex8_1_4 after state save/reload.""" bb = ti.TestBlackBox('ex8_1_4') optimum = bb._function.optimum_value handle, filename = tempfile.mkstemp() for seed in self.rand_seeds: print() print('Solving ex8_1_4 with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='MSRSM', rbf='linear', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, rand_seed=seed) alg = ra.OptAlgorithm(settings, bb) res = alg.optimize(5) alg.save_to_file(filename) alg_reload = ra.OptAlgorithm.load_from_file(filename) res = alg_reload.optimize() msg = 'Could not solve ex8_1_4 after reload' target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg) os.close(handle) os.remove(filename)
def test_get_rbf_function(self): """Check that all RBFs are properly computed at 0 and at 1.""" settings = RbfSettings() # Set up values of the RBF at 0 and at 1 rbf_values = dict() rbf_values['linear'] = (0.0, 1.0) rbf_values['multiquadric'] = (1.0, math.sqrt(1 + 1.0)) rbf_values['cubic'] = (0.0, 1.0) rbf_values['thin_plate_spline'] = (0.0, 0.0) for rbf_type in self.rbf_types: settings.rbf = rbf_type rbf = ru.get_rbf_function(settings) rbf_at_0, rbf_at_1 = rbf_values[rbf_type] msg = 'RBF {:s} is not {:f} at 0'.format(rbf_type, rbf_at_0) self.assertEqual(rbf_at_0, rbf(0.0), msg=msg) msg = 'RBF {:s} is not {:f} at 1'.format(rbf_type, rbf_at_1) self.assertEqual(rbf_at_1, rbf(1.0), msg=msg)
def test_get_model_quality_estimate(self): """Test the get_model_quality_estimate function. """ settings = RbfSettings(model_selection_solver = 'numpy') res = ms.get_model_quality_estimate(settings, self.n, self.k, self.node_pos, self.node_val, self.k) self.assertAlmostEqual(res, 7.95598007028)
def test_get_model_quality_estimate_cpx(self): """Test the get_model_quality_estimate_cpx function. """ if cpx_available: settings = RbfSettings(model_selection_solver = 'cplex') res = ms.get_model_quality_estimate_cpx(settings, self.n, self.k, self.node_pos, self.node_val, self.k) self.assertAlmostEqual(res, 7.95598007028)
def rbfopt_cl_interface(args, black_box): """Command-line interface. Optimize the specified objective function using the algorithmic options given on the command line. Parameters ---------- args : Dict[string] A dictionary containing the values of the parameters in a format args['name'] = value. black_box : black_box.BlackBox An object containing the function to be optimized and its main characteristics. It is possible to pass an object of a different class, provided that it as the same public attributes. """ assert(hasattr(black_box, 'dimension')) assert(hasattr(black_box, 'var_lower')) assert(hasattr(black_box, 'var_upper')) assert(hasattr(black_box, 'integer_vars')) assert(hasattr(black_box, 'evaluate')) assert(hasattr(black_box, 'evaluate_fast')) # Open output stream if necessary if (args['output_stream'] is None): output_stream = sys.stdout else: try: output_stream = open(args['output_stream'], 'w') except IOError as e: print('Exception in opening log file', file = sys.stderr) print(e, file = sys.stderr) # Make a copy of parameters and adjust them, deleting keys # that are not recognized as valid by RbfSettings. local_args = args.copy() del local_args['output_stream'] settings = RbfSettings.from_dictionary(local_args) settings.print(output_stream = output_stream) result = rbfopt.rbf_optimize(settings = settings, dimension = black_box.dimension, var_lower = black_box.var_lower, var_upper = black_box.var_upper, objfun = black_box.evaluate, objfun_fast = black_box.evaluate_fast, integer_vars = black_box.integer_vars, output_stream = output_stream) print('rbf_optimize returned function value {:.15f}'.format(result[0]), file = output_stream) output_stream.close()
def test_get_best_rbf_model(self): """Test the get_best_rbf_model function. This is the main function of the module, which employs all other functions. We test it on a set of pre-generated data to verify that we get the expected response. """ for solver in self.solver: settings = RbfSettings(model_selection_solver = solver) res = ms.get_best_rbf_model(settings, self.n, self.k, self.node_pos, self.node_val, self.k) self.assertEqual(res, 'linear')
def test_transform_function_values(self): """Test all codomain transformation strategies. This will verify that the transformation strategies always produce valid results and can handle extreme cases. """ settings = RbfSettings() settings.fast_objfun_rel_error = 0.01 settings.fast_objfun_abs_error = 0.01 list_scaling = [ val for val in RbfSettings._allowed_function_scaling if val != 'auto' ] list_clipping = [ val for val in RbfSettings._allowed_dynamism_clipping if val != 'auto' ] transf = ru.transform_function_values # Create list of values to test: node_val and corresponding # fast_node_index to_test = [(np.array([0, -100, settings.dynamism_threshold * 10]), np.array([])), (np.array([0.0]), np.array([0])), (np.array([0.0 for i in range(10)]), np.array([8, 9])), (np.array([100.0 for i in range(10)]), np.array([i for i in range(10)])), (np.array([10.0**i for i in range(-20, 20)]), np.array([])), (np.append(np.array([-10.0**i for i in range(-20, 20)]), np.array([10.0**i for i in range(-20, 20)])), np.array([i for i in range(50, 60)]))] for scaling in list_scaling: for clipping in list_clipping: header = '({:s}, {:s}):'.format(scaling, clipping) for (node_val, fast_node_index) in to_test: settings.function_scaling = scaling settings.dynamism_clipping = clipping (scaled, minval, maxval, errbounds) = transf(settings, node_val, min(node_val), max(node_val), fast_node_index) # Check that the number of scaled values is the # same as the number of input values msg = 'Number of output values is different from input' self.assertEqual(len(scaled), len(node_val), msg=header + msg) msg = 'Dynamism threshold was not enforced' v1 = abs(min(scaled)) v2 = abs(max(scaled)) c1 = v1 > 1.0e-10 and v2 / v1 <= settings.dynamism_threshold c2 = v1 <= 1.0e-10 and v2 <= settings.dynamism_threshold self.assertTrue(clipping == 'off' or c1 or c2, msg=header + msg) for (i, j) in enumerate(fast_node_index): msg = 'Fast_node_index have wrong sign' self.assertLessEqual(errbounds[i][0], 0, msg=msg) self.assertGreaterEqual(errbounds[i][1], 0, msg=msg) msg = ('Min/Max of scaled values inconsistent with ' + 'returned scaled_min and scaled_max') self.assertEqual(min(scaled), minval, msg=header + msg) self.assertEqual(max(scaled), maxval, msg=header + msg)
def test_get_bump_new_node(self): """Verify bumpiness is constant under the right conditions. This function tests the bumpiness of the interpolant model, when adding an interpolation point at a location with function value increasing very fast. This should give increasing bumpiness, and that's what we check. """ settings = RbfSettings(rbf='cubic') n = 3 k = 5 var_lower = np.array([i for i in range(n)]) var_upper = np.array([i + 10 for i in range(n)]) node_pos = np.array([var_lower, var_upper, [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12]]) node_val = np.array([2*i for i in range(k)]) fast_node_index = np.array([i for i in range(k)]) fast_node_err_bounds = [(-1, +1) for i in range(k)] Amat = [[0.0, 5196.152422706633, 5.196152422706631, 1714.338065908822, 2143.593744305343, 0.0, 1.0, 2.0, 1.0], [5196.152422706633, 0.0, 3787.995116153135, 324.6869498824983, 218.25390174061036, 10.0, 11.0, 12.0, 1.0], [5.196152422706631, 3787.995116153135, 0.0, 1101.235503851924, 1418.557944049167, 1.0, 2.0, 3.0, 1.0], [1714.338065908822, 324.6869498824983, 1101.235503851924, 0.0, 136.3398894271225, 9.0, 5.0, 8.8, 1.0], [2143.593744305343, 218.25390174061036, 1418.557944049167, 136.3398894271225, 0.0, 5.5, 7.0, 12.0, 1.0], [0.0, 10.0, 1.0, 9.0, 5.5, 0.0, 0.0, 0.0, 0.0], [1.0, 11.0, 2.0, 5.0, 7.0, 0.0, 0.0, 0.0, 0.0], [2.0, 12.0, 3.0, 8.8, 12.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]] Amat = np.matrix(Amat) fast_node_index = np.array([0, 1]) fast_node_err_bounds = [(-1, 1), (-1, 1)] # Previous bumpiness new_node = np.array([(var_lower[i] + var_upper[i])/2 for i in range(n)]) bump = 0.0 for i in range(5): # Set increasing target values target_val = 5 + i*10000 # Compute new bumpiness nbump = aux.get_bump_new_node(settings, n, k, node_pos, node_val, new_node, fast_node_index, fast_node_err_bounds, target_val) self.assertGreaterEqual(nbump, bump, msg='Bumpiness not increasing') # Store new bumpiness bump = nbump
def test_ga_optimize(self): """Verify that the genetic algorithm can solve simple problems. """ var_lower = np.array([-1] * 3) var_upper = np.array([1] * 3) integer_vars = np.array([]) settings = RbfSettings(ga_base_population_size = 100) point = aux.ga_optimize(settings, 3, var_lower, var_upper, integer_vars, quadratic) self.assertLessEqual(quadratic([point])[0], 0.05, msg = 'Could not solve quadratic with GA') point = aux.ga_optimize(settings, 3, var_lower, var_upper, integer_vars, shifted_quadratic) self.assertLessEqual(shifted_quadratic([point])[0], 0.05, msg = 'Could not solve shifted quadratic with GA')
def test_get_fmax_current_iter(self): """Verify get_fmax_current_iter is resilient to limit cases. This function tests whether correct values are returned when there is a single-element list of node values, and when the list of node values is exactly the minimum required k + 1. """ settings = RbfSettings() fun = ru.get_fmax_current_iter self.assertEqual(fun(settings, 0, 1, 1, np.array([1])), 1, msg='Failed on single-element list') self.assertEqual(fun(settings, 10, 11, 5, np.array([i for i in range(11)])), 10, msg='Failed on n == k + 1')
def setUp(self): """Generate data to simulate an optimization problem.""" self.settings = RbfSettings(rbf='cubic') self.n = 3 self.k = 5 self.var_lower = np.array([i for i in range(self.n)]) self.var_upper = np.array([i + 10 for i in range(self.n)]) self.node_pos = np.array([ self.var_lower, self.var_upper, [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12] ]) self.node_val = np.array([2 * i for i in range(self.k)]) Amat = [[ 0.0, 5196.152422706633, 5.196152422706631, 1714.338065908822, 2143.593744305343, 0.0, 1.0, 2.0, 1.0 ], [ 5196.152422706633, 0.0, 3787.995116153135, 324.6869498824983, 218.25390174061036, 10.0, 11.0, 12.0, 1.0 ], [ 5.196152422706631, 3787.995116153135, 0.0, 1101.235503851924, 1418.557944049167, 1.0, 2.0, 3.0, 1.0 ], [ 1714.338065908822, 324.6869498824983, 1101.235503851924, 0.0, 136.3398894271225, 9.0, 5.0, 8.8, 1.0 ], [ 2143.593744305343, 218.25390174061036, 1418.557944049167, 136.3398894271225, 0.0, 5.5, 7.0, 12.0, 1.0 ], [0.0, 10.0, 1.0, 9.0, 5.5, 0.0, 0.0, 0.0, 0.0], [1.0, 11.0, 2.0, 5.0, 7.0, 0.0, 0.0, 0.0, 0.0], [2.0, 12.0, 3.0, 8.8, 12.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]] self.Amat = np.matrix(Amat) self.Amatinv = self.Amat.getI() self.rbf_lambda = np.array([ -0.02031417613815348, -0.0022571306820170587, 0.02257130682017054, 6.74116235140294e-18, -1.0962407017011667e-18 ]) self.rbf_h = np.array([ -0.10953754862932995, 0.6323031632900591, 0.5216788297837124, 9.935450288253636 ]) self.integer_vars = np.array([1])
def test_get_min_bump_node(self): """Verify get_min_bump_node is resilient to limit cases. Verify that when fast_node_index is empty, (None, +inf) is returned, and for a small problem with 3 variables and 5 nodes, the corect answer is reached when there is only one possible point that could be replaced. """ settings = RbfSettings(rbf = 'cubic') ind, bump = aux.get_min_bump_node(settings, 1, 10, np.matrix((1,1)), np.array([0] * 10), np.array([]), [], 0) self.assertIsNone(ind, msg = 'Failed whith empty list') self.assertEqual(bump, float('+inf'), msg = 'Failed whith empty list') n = 3 k = 5 var_lower = np.array([i for i in range(n)]) var_upper = np.array([i + 10 for i in range(n)]) node_pos = np.array([var_lower, var_upper, [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12]]) node_val = np.array([2*i for i in range(k)]) fast_node_index = np.array([i for i in range(k)]) fast_node_err_bounds = [(-1, +1) for i in range(k)] Amat = [[0.0, 5196.152422706633, 5.196152422706631, 1714.338065908822, 2143.593744305343, 0.0, 1.0, 2.0, 1.0], [5196.152422706633, 0.0, 3787.995116153135, 324.6869498824983, 218.25390174061036, 10.0, 11.0, 12.0, 1.0], [5.196152422706631, 3787.995116153135, 0.0, 1101.235503851924, 1418.557944049167, 1.0, 2.0, 3.0, 1.0], [1714.338065908822, 324.6869498824983, 1101.235503851924, 0.0, 136.3398894271225, 9.0, 5.0, 8.8, 1.0], [2143.593744305343, 218.25390174061036, 1418.557944049167, 136.3398894271225, 0.0, 5.5, 7.0, 12.0, 1.0], [0.0, 10.0, 1.0, 9.0, 5.5, 0.0, 0.0, 0.0, 0.0], [1.0, 11.0, 2.0, 5.0, 7.0, 0.0, 0.0, 0.0, 0.0], [2.0, 12.0, 3.0, 8.8, 12.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]] Amat = np.matrix(Amat) for j in range(k): ind, bump = aux.get_min_bump_node(settings, n, k, Amat, node_val, fast_node_index, fast_node_err_bounds, node_val[j] - 0.5) self.assertEqual(ind, j, msg = 'Only one point is a candidate' + 'for replacement, but it was not returned!')
def setUp(self): """Generate data to simulate an optimization problem.""" self.settings = RbfSettings(rbf='thin_plate_spline') self.n = 3 self.k = 5 self.var_lower = np.array([i for i in range(self.n)]) self.var_upper = np.array([i + 10 for i in range(self.n)]) self.node_pos = np.array([ self.var_lower, self.var_upper, [1, 2, 3], [9, 5, 8.8], [5.5, 7, 12] ]) self.node_val = np.array([2 * i for i in range(self.k)]) Amat = [[ 0.0, 855.5673711984304, 1.6479184330021641, 355.55903306222723, 425.059078986427, 0.0, 1.0, 2.0, 1.0 ], [ 855.5673711984304, 0.0, 667.4069653658767, 91.06079221519477, 65.07671378607489, 10.0, 11.0, 12.0, 1.0 ], [ 1.6479184330021641, 667.4069653658767, 0.0, 248.97553659741263, 305.415419302314, 1.0, 2.0, 3.0, 1.0 ], [ 355.55903306222723, 91.06079221519477, 248.97553659741263, 0.0, 43.40078293199628, 9.0, 5.0, 8.8, 1.0 ], [ 425.059078986427, 65.07671378607489, 305.415419302314, 43.40078293199628, 0.0, 5.5, 7.0, 12.0, 1.0 ], [0.0, 10.0, 1.0, 9.0, 5.5, 0.0, 0.0, 0.0, 0.0], [1.0, 11.0, 2.0, 5.0, 7.0, 0.0, 0.0, 0.0, 0.0], [2.0, 12.0, 3.0, 8.8, 12.0, 0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]] self.Amat = np.matrix(Amat) self.Amatinv = self.Amat.getI() self.rbf_lambda = np.array([ -0.1948220562664489, -0.02164689514071656, 0.21646895140716543, 2.4492621453325443e-18, 3.4694803106897584e-17 ]) self.rbf_h = np.array([ -0.047916449337864896, -0.42012611088687196, 1.072728018406163, 16.43832406902896 ]) self.integer_vars = np.array([1])
def test_initialize_nodes(self): """Test initialization methods for the sample points. This method verifies that returned sets of points have at least n+1 points, and integer variables are integer. """ var_lower = np.array([-1, 0, 1]) var_upper = np.array([1, 2, 3]) integer_vars = np.array([1, 2]) for method in RbfSettings._allowed_init_strategy: settings = RbfSettings(init_strategy=method) points = ru.initialize_nodes(settings, var_lower, var_upper, integer_vars) msg = ('Number of points returned by {:s}'.format(method) + ' is insufficient') self.assertGreaterEqual(len(points), 4, msg=msg) for point in points: for index in integer_vars: self.assertEqual(point[index] - round(point[index]), 0)
def test_time_limit(self): """Verify that time limits are satisfied.""" bb = ti.TestBlackBox('hartman6') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving hartman6 with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='MSRSM', global_search_method='solver', target_objval=optimum, eps_opt=0.0, max_clock_time=2.0, rand_seed=seed) start_time = time.time() alg = ra.OptAlgorithm(settings, bb) res = alg.optimize() tot_time = time.time() - start_time msg = 'Time limit exceeded with MSRSM algorithm' self.assertLessEqual(tot_time, 5.0, msg=msg)
def test_msrsm_prob03(self): """Check solution of prob03 with the MSRSM algorithm, genetic.""" bb = ti.TestBlackBox('prob03') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving prob03 with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='MSRSM', global_search_method='genetic', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, rand_seed=seed) alg = ra.OptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve prob03 with MSRSM algorithm' target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg)
def test_msrsm_parallel_ex8_1_4(self): """Check solution of ex8_1_4 with the MSRSM algorithm, sampling.""" bb = ti.TestBlackBox('ex8_1_4') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving ex8_1_4 with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='MSRSM', global_search_method='sampling', rbf='linear', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, num_cpus=2, rand_seed=seed) alg = ra.OptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve ex8_1_4 with MSRSM algorithm' target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg)
def test_gutmann_parallel_prob03(self): """Check solution of prob03 with Gutmann's method, sampling.""" bb = ti.TestBlackBox('prob03') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving prob03 with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='Gutmann', global_search_method='sampling', rbf='cubic', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, num_cpus=2, rand_seed=seed) alg = ra.OptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve prob03 with Gutmann\'s algorithm' target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg)
def test_gutmann_st_miqp3_noisy(self): """Check solution of noisy st_miqp3 with Gutmann, genetic.""" bb = ti.TestNoisyBlackBox('st_miqp3', 0.1, 0.01) optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving st_miqp3 with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='Gutmann', global_search_method='genetic', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, fast_objfun_rel_error=0.1, fast_objfun_abs_error=0.01, rand_seed=seed) alg = ra.OptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve st_miqp3 with Gutmann\'s algorithm' target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg)
def test_msrsm_parallel_prob03_noisy(self): """Check solution of noisy prob03 with MSRSM, sampling.""" bb = ti.TestNoisyBlackBox('prob03', 0.1, 0.01) optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving prob03 with random seed ' + '{:d}'.format(seed)) settings = RbfSettings(algorithm='MSRSM', global_search_method='sampling', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, num_cpus=4, fast_objfun_rel_error=0.1, fast_objfun_abs_error=0.01, rand_seed=seed) alg = ra.OptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve prob03 with MSRSM algorithm' target = optimum + (abs(optimum) * self.eps_opt if abs(optimum) > settings.eps_zero else self.eps_opt) self.assertLessEqual(res[0], target, msg=msg)