def test_gutmann_parallel_ex8_1_4_infstep(self): """Check solution of ex8_1_4 with Gutmann, infstep. Genetic algorithm.""" bb = tf.TestBlackBox('ex8_1_4') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving ex8_1_4 with infstep and random seed ' + '{:d}'.format(seed)) settings = RbfoptSettings(algorithm='Gutmann', global_search_method='genetic', rbf='multiquadric', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, num_cpus=2, do_infstep=True, refinement_frequency=5, rand_seed=seed) alg = ra.RbfoptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve ex8_1_4 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_state_reload(self): """Check solution of ex8_1_4 after state save/reload.""" bb = tf.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 = RbfoptSettings(algorithm='MSRSM', rbf='linear', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, rand_seed=seed) alg = ra.RbfoptAlgorithm(settings, bb) res = alg.optimize(5) alg.save_to_file(filename) alg_reload = ra.RbfoptAlgorithm.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_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 = tf.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 = RbfoptSettings(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.RbfoptAlgorithm(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_prob03_fixed(self): """Check solution of prob03 with fixed variables.""" for seed in self.rand_seeds: bb = tf.TestBlackBox('prob03') optimum = bb._function.optimum_value eps_opt = 0.0001 print() print('Solving prob03 with random seed ' + '{:d}'.format(seed)) settings = RbfoptSettings(algorithm='MSRSM', target_objval=optimum, eps_opt=eps_opt, max_iterations=200, max_evaluations=300, rand_seed=seed) alg = ra.RbfoptAlgorithm(settings, bb) res = alg.optimize() msg = 'Did not find global optimum in prob03' target = optimum + (abs(optimum) * eps_opt if abs(optimum) > settings.eps_zero else eps_opt) self.assertLessEqual(res[0], target, msg=msg) msg = 'Used more evaluations than necessary in prob03' self.assertLessEqual(res[3], np.prod(bb.get_var_upper() - bb.get_var_lower() + 1), msg=msg) msg = 'Used too many iterations in prob03' self.assertLessEqual( res[2], 2 * np.prod(bb.get_var_upper() - bb.get_var_lower() + 1), msg=msg)
def test_categorical_vars(self): """Check solution of branin_cat with Gutmann. Default parameters. """ bb = tf.TestBlackBox('branin_cat') optimum = bb._function.optimum_value eps_opt = 0.4 for seed in self.rand_seeds: print() print('Solving branin_cat with random seed ' + '{:d}'.format(seed)) settings = RbfoptSettings(algorithm='Gutmann', target_objval=optimum, eps_opt=eps_opt, max_iterations=400, max_evaluations=500, do_infstep=True, rand_seed=seed) alg = ra.RbfoptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve branin_cat with Gutmann\'s algorithm' target = optimum + (abs(optimum) * eps_opt if abs(optimum) > settings.eps_zero else eps_opt) self.assertLessEqual(res[0], target, msg=msg)
def test_gutmann_parallel_ex8_1_4(self): """Check solution of ex8_1_4 with Gutmann's method, solver.""" bb = tf.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 = RbfoptSettings(algorithm='Gutmann', rbf='gaussian', global_search_method='solver', target_objval=optimum, max_stalled_iterations=20, eps_impr=0.05, refinement_frequency=6, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, max_fraction_discarded=0.2, num_cpus=2, max_clock_time=1200, rand_seed=seed) alg = ra.RbfoptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve ex8_1_4 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_ex8_1_4_log(self): """Check solution of ex8_1_4 with Gutmann, log scaling, infstep. Sampling-based global search. """ bb = tf.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 = RbfoptSettings(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.RbfoptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve ex8_1_4 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_distinct_init_points(self): """Verify that distinct points are accepted.""" bb = tf.TestBlackBox('branin') optimum = bb._function.optimum_value init_node_pos = [[0, 0], [1, 1], [0, 10], [5, 0]] settings = RbfoptSettings(target_objval=optimum, max_iterations=10, eps_opt=0.0, rand_seed=89123132) alg = ra.RbfoptAlgorithm(settings, bb, init_node_pos=init_node_pos) res = alg.optimize() for node in init_node_pos: self.assertIn(node, alg.all_node_pos, msg='Initial point not found in all_node_pos')
def test_failed_init_points(self): """Verify that not enough points raises an error.""" bb = tf.TestBlackBox('branin') optimum = bb._function.optimum_value init_node_pos = [[0, 0], [1, 1], [0, 10], [5, 0]] settings = RbfoptSettings(target_objval=optimum, max_iterations=10, eps_opt=0.0) alg = ra.RbfoptAlgorithm(settings, bb, init_node_pos=init_node_pos, do_init_strategy=False) res = alg.optimize() for node in init_node_pos: self.assertIn(node, alg.all_node_pos[:4], msg='Initial point not found in all_node_pos')
def test_duplicate_init_points(self): """Verify that duplicate points are removed.""" bb = tf.TestBlackBox('branin') optimum = bb._function.optimum_value init_node_pos = [[0, 0], [0, 1], [0, 1.0e-12]] settings = RbfoptSettings(target_objval=optimum, max_iterations=10, eps_opt=0.0) alg = ra.RbfoptAlgorithm(settings, bb, init_node_pos=init_node_pos, do_init_strategy=False) res = alg.optimize() for node in init_node_pos[:2]: self.assertIn(node, alg.all_node_pos, msg='Initial point not found in all_node_pos') self.assertNotIn(init_node_pos[-1], alg.all_node_pos[2:], msg='Duplicate initial point found in all_node_pos')
def test_optima_noisy(self): """Ensure that noisy functions keep the optimum.""" for name in self.function_list: bb = tf.TestBlackBox(name) noisybb = tf.TestNoisyBlackBox(bb, 0.0, 0.1) self.assertAlmostEqual( noisybb.evaluate(bb._function.optimum_point), bb._function.optimum_value, delta=0.1, msg='Noisy function ' + name + ': absolute error exceeded') for name in self.function_list: bb = tf.TestBlackBox(name) noisybb = tf.TestNoisyBlackBox(bb, 0.1, 0.0) funval = noisybb.evaluate(bb._function.optimum_point) if (abs(bb._function.optimum_value) > 1.0e-8): error = (abs(funval - bb._function.optimum_value) / abs(bb._function.optimum_value)) else: error = abs(funval - bb._function.optimum_value) self.assertLessEqual(error, 0.1, msg='Noisy function ' + name + ': relative error exceeded') # -- end function
def test_time_limit(self): """Verify that time limits are satisfied (MSRSM).""" bb = tf.TestBlackBox('hartman6') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving hartman6 with random seed ' + '{:d}'.format(seed)) settings = RbfoptSettings(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.RbfoptAlgorithm(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_st_miqp3_noisy(self): """Check solution of noisy st_miqp3 with MSRSM, genetic.""" bb = tf.TestNoisyBlackBox(tf.TestBlackBox('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 = RbfoptSettings(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.RbfoptAlgorithm(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_ex8_1_4(self): """Check solution of ex8_1_4 with Gutmann's method, genetic.""" bb = tf.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 = RbfoptSettings(algorithm='Gutmann', global_search_method='genetic', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, rand_seed=seed) alg = ra.RbfoptAlgorithm(settings, bb) res = alg.optimize() msg = 'Could not solve ex8_1_4 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(self): """Check solution of prob03 with the MSRSM algorithm, genetic.""" bb = tf.TestBlackBox('prob03') optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving prob03 with random seed ' + '{:d}'.format(seed)) settings = RbfoptSettings(algorithm='MSRSM', global_search_method='genetic', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=500, max_evaluations=500, num_cpus=4, rand_seed=seed) alg = ra.RbfoptAlgorithm(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_branin_noisy_with_init(self): """Check solution of noisy branin with MSRSM, sampling.""" bb = tf.TestNoisyBlackBox(tf.TestBlackBox('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 = RbfoptSettings(algorithm='MSRSM', global_search_method='sampling', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, rand_seed=seed) init_node_pos = [[0, 0], [-2, 2], [5, 10], [-2.5, 1]] alg = ra.RbfoptAlgorithm(settings, bb, init_node_pos) res = alg.optimize() msg = ('Could not solve noisy branin with init and ' + '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 = tf.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 = RbfoptSettings(algorithm='MSRSM', global_search_method='sampling', rbf='linear', rbf_shape_parameter=0.01, target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, num_cpus=2, rand_seed=seed) alg = ra.RbfoptAlgorithm(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_st_miqp3_noisy(self): """Check solution of noisy st_miqp3 with Gutmann, solver.""" bb = tf.TestNoisyBlackBox(tf.TestBlackBox('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 = RbfoptSettings(algorithm='Gutmann', global_search_method='solver', rbf_shape_parameter=0.01, rbf='gaussian', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, num_cpus=2, rand_seed=seed) alg = ra.RbfoptAlgorithm(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_gutmann_branin_cat_noisy_with_init(self): """Check solution of noisy branin_cat with Gutmann, solver.""" bb = tf.TestNoisyBlackBox(tf.TestBlackBox('branin_cat'), 0.1, 0.01) optimum = bb._function.optimum_value for seed in self.rand_seeds: print() print('Solving branin_cat with random seed ' + '{:d}'.format(seed)) settings = RbfoptSettings(algorithm='Gutmann', global_search_method='solver', target_objval=optimum, eps_opt=self.eps_opt, max_iterations=200, max_evaluations=300, rand_seed=seed) init_node_pos = [[0, 0, 0], [-2, 2, 1], [5, 10, 2]] init_node_val = [bb._function.evaluate(x) for x in init_node_pos] alg = ra.RbfoptAlgorithm(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)