def test_judge_match(self): size = 3 regs = [[1, 5], [-1, 1], [1, 2]] tys = [True, True, True] assert Dimension2.judge_match(size, regs, tys) == True tys = [True, True] assert Dimension2.judge_match(size, regs, tys) == False
def test_set_regions(self): dim = Dimension2([(ValueType.CONTINUOUS, [1, 2], 1e-6), (ValueType.CONTINUOUS, [2, 3], 1e-6)]) dim.set_regions([[-1, 1], [-1, 1]], [True, True]) assert dim.equal( Dimension2([(ValueType.CONTINUOUS, [-1, 1], 1e-6), (ValueType.CONTINUOUS, [-1, 1], 1e-6)]))
def test_limited_space(self): dim1 = Dimension2([(ValueType.CONTINUOUS, [-1, 1], 1e-6), (ValueType.CONTINUOUS, [-1, 1], 1e-6)]) limited, number = dim1.limited_space() assert limited is False and number == 0 dim2 = Dimension2([(ValueType.DISCRETE, [-1, 1], False), (ValueType.DISCRETE, [-1, 1], False)]) limited, number = dim2.limited_space() assert limited is True and number == 9
def test_merge_dim(self): dim_list1 = [(ValueType.CONTINUOUS, [1, 2], 1e-6)] dim_list2 = [(ValueType.CONTINUOUS, [1, 2], 1e-6), (ValueType.CONTINUOUS, [2, 3], 1e-6)] dim1 = Dimension2(dim_list1) dim2 = Dimension2(dim_list2) dim3 = Dimension2.merge_dim(dim1, dim2) assert dim3.equal( Dimension2([(ValueType.CONTINUOUS, [1, 2], 1e-6), (ValueType.CONTINUOUS, [1, 2], 1e-6), (ValueType.CONTINUOUS, [2, 3], 1e-6)]))
def test_performance(self): ackley_noise_func = ackley_noise_creator(0, 0.1) dim_size = 100 # dimensions one_dim = (ValueType.CONTINUOUS, [-1, 1], 1e-6) dim_list = [(one_dim)] * dim_size dim = Dimension2(dim_list) # form up the dimension object objective = Objective(ackley_noise_func, dim) # form up the objective function budget = 20000 # 20*dim_size # number of calls to the objective function # suppression=True means optimize with value suppression, which is a noise handling method # resampling=True means optimize with re-sampling, which is another common used noise handling method # non_update_allowed=500 and resample_times=100 means if the best solution doesn't change for 500 budgets, # the best solution will be evaluated repeatedly for 100 times # balance_rate is a parameter for exponential weight average of several evaluations of one sample. parameter = Parameter(budget=budget, noise_handling=True, suppression=True, non_update_allowed=200, resample_times=50, balance_rate=0.5) # parameter = Parameter(budget=budget, noise_handling=True, resampling=True, resample_times=10) parameter.set_positive_size(5) sol = Opt.min(objective, parameter) assert sol.get_value() < 4
def test_racos_performance2(self): # continuous dim = 100 # dimension one_dim = (ValueType.CONTINUOUS, [-1, 1], 1e-6) dim_list = [(one_dim)] * dim objective = Objective(ackley, Dimension2(dim_list)) # setup objective parameter = Parameter(budget=100 * dim, sequential=False, seed=1) solution = ExpOpt.min(objective, parameter)[0] assert solution.get_value() < 0.2 dim = 500 dim_list = [(one_dim)] * dim objective = Objective(ackley, Dimension2(dim_list)) # setup objective parameter = Parameter(budget=10000, sequential=False, seed=1) sol = Opt.min(objective, parameter) sol.print_solution() assert solution.get_value() < 2 # discrete # setcover problem = SetCover() dim_size = 20 one_dim = (ValueType.DISCRETE, [0, 1], False) dim_list = [(one_dim)] * dim_size dim = Dimension2(dim_list) # the dim is prepared by the class objective = Objective(problem.fx, dim) # form up the objective function budget = 100 * dim.get_size( ) # number of calls to the objective function parameter = Parameter(budget=budget, sequential=False, seed=777) sol = Opt.min(objective, parameter) sol.print_solution() assert sol.get_value() < 2 # sphere dim_size = 100 # dimensions one_dim = (ValueType.DISCRETE, [-10, 10], True) dim_list = [(one_dim)] * dim_size dim = Dimension2(dim_list) # form up the dimension object objective = Objective(sphere_discrete_order, dim) # form up the objective function parameter = Parameter(budget=10000, sequential=False, seed=77) sol = Opt.min(objective, parameter) sol.print_solution() assert sol.get_value() < 200
def __init__(self, algo="asracos", budget=None, dim_dict=None, max_concurrent=10, metric="episode_reward_mean", mode="min", **kwargs): assert budget is not None, "`budget` should not be None!" assert dim_dict is not None, "`dim_list` should not be None!" assert type(max_concurrent) is int and max_concurrent > 0 assert mode in ["min", "max"], "`mode` must be 'min' or 'max'!" _algo = algo.lower() assert _algo in ["asracos", "sracos" ], "`algo` must be in ['asracos', 'sracos'] currently" self._max_concurrent = max_concurrent self._metric = metric if mode == "max": self._metric_op = -1. elif mode == "min": self._metric_op = 1. self._live_trial_mapping = {} self._dim_keys = [] _dim_list = [] for k in dim_dict: self._dim_keys.append(k) _dim_list.append(dim_dict[k]) dim = Dimension2(_dim_list) par = Parameter(budget=budget) if _algo == "sracos" or _algo == "asracos": self.optimizer = SRacosTune(dimension=dim, parameter=par) self.solution_dict = {} self.best_solution_list = [] super(ZOOptSearch, self).__init__( metric=self._metric, mode=mode, **kwargs)
'objective_function/config/low_dim.ini') config.read(config_name, encoding='utf-8') print(config.sections()) optimal_position_address = os.path.join( project_dir, config.get(obj_name, 'optimal_position_address')) dim_size = config.getint(obj_name, 'dim_size') dim_regs = eval(config.get(obj_name, 'dim_regs')) budget = config.getint(obj_name, 'budget') repeat = 30 set_optimal_position(optimal_position_address) seed = 0 gl.set_seed(seed) for i in range(repeat): dim = Dimension2([(ValueType.CONTINUOUS, dim_regs, 1e-6)] * dim_size) # dim = Dimension(dim_size, [dim_regs] * dim_size, [True] * dim_size) objective = Objective(lambda sol: function_dict[obj_name](sol.get_x()), dim) # form up the objective function parameter = Parameter(budget=budget, intermediate_result=True, intermediate_freq=100) sol = Opt.min(objective, parameter) append_all_epoch() all_epoch = np.array(get_all_epoch()) log_address = os.path.join(project_dir, 'ZOOpt_exp/log/low_dim/') file_name = os.path.join(log_address, '{}_{}.txt'.format(obj_name, dim_size)) os.makedirs(log_address, exist_ok=True) np.savetxt(file_name, all_epoch) print(all_epoch.shape)
def test_is_discrete(self): dim1 = Dimension2([(ValueType.CONTINUOUS, [-1, 1], 1e-6), (ValueType.DISCRETE, [-1, 1], False)]) assert dim1.is_discrete() is False
def test_copy_region(self): dim1 = Dimension2([(ValueType.CONTINUOUS, [-1, 1], 1e-6), (ValueType.CONTINUOUS, [-1, 1], 1e-6)]) region = dim1.copy_region() assert region == [[-1, 1], [-1, 1]]
def test_deep_copy(self): dim1 = Dimension2([(ValueType.CONTINUOUS, [-1, 1], 1e-6), (ValueType.CONTINUOUS, [-1, 1], 1e-6)]) dim2 = dim1.deep_copy() assert dim1.equal(dim2)