def test_theta_weak_dominate(self): sol1 = Solution(value=[1, 2]) sol2 = Solution(value=[4, 2]) assert PONSS.theta_weak_dominate(3, sol1, sol2) is True assert PONSS.theta_weak_dominate(3.1, sol1, sol2) is False # def test_performance(self): # # load data file # mse = SparseMSE('example/sparse_regression/sonar.arff') # mse.set_sparsity(8) # # # setup objective # objective = Objective(func=mse.loss, dim=mse.get_dim(), constraint=mse.constraint) # # ponss_theta and ponss_b are parameters used in PONSS algorithm and should be provided by users. ponss_theta stands # # for the threshold. ponss_b limits the number of solutions in the population set. # budget = int(2 * exp(1) * (mse.get_sparsity() ** 2) * mse.get_dim().get_size()) # parameter = Parameter(algorithm='poss', noise_handling=True, ponss=True, ponss_theta=0.5, ponss_b=mse.get_k(), # budget=budget) # # perform sparse regression with constraint |w|_0 <= k # solution = Opt.min(objective, parameter) # assert solution.get_value()[0] < 0.7 # # # if __name__ == '__main__': # tp = TestPONSS() # tp.test_performance()
def test_racos_common_is_distinct(self): a = Solution(x=[1, 2, 3]) b = Solution(x=[2, 3, 4]) c = Solution(x=[3, 4, 5]) seti = [a, b] assert RacosCommon.is_distinct( seti, a) is False and RacosCommon.is_distinct(seti, c) is True
def test_sracos_strategy_lm(self): s0 = Solution(x=[1, 1, 1], value=0) s1 = Solution(x=[2.2, 2.2, 2.2], value=1) s2 = Solution(x=[3, 3, 3], value=2) iset = [s0, s1, s2] sracos = SRacos() test_s1 = Solution(x=[2.1, 2.1, 2.1], value=2.1) sracos.strategy_lm(iset, s0, test_s1) assert iset[2].get_value() == 2.1
def test_theta_dominate(self): sol1 = Solution(value=[1, 2]) sol2 = Solution(value=[4, 2]) assert PONSS.theta_dominate(2.9, sol1, sol2) is True and PONSS.theta_dominate( 3, sol1, sol2) is False sol3 = Solution(value=[2, 3]) sol4 = Solution(value=[3, 2]) assert PONSS.theta_dominate(1, sol1, sol2) is True
def test_find_maximum_and_minimum(self): sol1 = Solution(x=[1, 2, 3], value=1) sol2 = Solution(x=[1, 3, 4], value=2) sol3 = Solution(x=[1, 5, 6], value=3) sol_set = [sol1, sol2, sol3] assert sol1.is_equal(Solution.find_minimum(sol_set)[0]) assert sol3.is_equal(Solution.find_maximum(sol_set)[0])
def test_sracos_strategy_rr(self): s0 = Solution(value=0) s1 = Solution(value=1) s2 = Solution(value=2) iset = [s0, s1, s2] sracos = SRacos() test_s1 = Solution(value=2.1) sracos.strategy_rr(iset, test_s1) assert len(iset) == 3 and (iset[0].get_value() == 2.1 or iset[1].get_value() == 2.1 or iset[2].get_value() == 2.1)
def test_deep_copy_set(self): sol1 = Solution(x=[1, 2, 3]) sol2 = Solution(x=[1, 3, 4]) sol3 = Solution(x=[1, 5, 6]) sol_set_1 = [sol1, sol2, sol3] sol_set_2 = Solution.deep_copy_set(sol_set_1) if len(sol_set_1) != len(sol_set_2): assert 0 for i in range(len(sol_set_1)): if sol_set_1[i].is_equal(sol_set_2[i]) is False: assert 0 assert 1
def _setup_zoopt(self): if self._metric is None and self._mode: # If only a mode was passed, use anonymous metric self._metric = DEFAULT_METRIC _dim_list = [] for k in self._dim_dict: self._dim_keys.append(k) _dim_list.append(self._dim_dict[k]) init_samples = None if self._points_to_evaluate: logger.warning( "`points_to_evaluate` seems to be ignored by ZOOpt.") init_samples = [ Solution(x=tuple(point[dim] for dim in self._dim_keys)) for point in self._points_to_evaluate ] dim = zoopt.Dimension2(_dim_list) par = zoopt.Parameter(budget=self._budget, init_samples=init_samples) if self._algo == "sracos" or self._algo == "asracos": from zoopt.algos.opt_algorithms.racos.sracos import SRacosTune self.optimizer = SRacosTune(dimension=dim, parameter=par, parallel_num=self.parallel_num, **self.kwargs) if init_samples: self.optimizer.init_attribute()
def test_eval(self): dim = 100 obj = Objective(func=ackley, dim=Dimension(dim, [[-1, 1]] * dim, [True] * dim)) sol = Solution(x=[0.2] * dim) res = obj.eval(sol) assert abs(res) <= 1e-7
def test_exist_equal(self): sol1 = Solution(x=[1, 2, 3]) sol2 = Solution(x=[1, 3, 4]) sol3 = Solution(x=[1, 5, 6]) sol_set = [sol1, sol2] assert sol1.exist_equal(sol_set) is True assert sol3.exist_equal(sol_set) is False
def test_sracos_binary_search(self): s0 = Solution(value=0) s1 = Solution(value=1) s2 = Solution(value=2) s3 = Solution(value=3) s4 = Solution(value=4) # 1 3 0 2 4 test_s1 = Solution(value=2.1) test_s2 = Solution(value=4.5) test_s3 = Solution(value=-1) test_s4 = Solution(value=2) set = [s0, s1, s2, s3, s4] sracos = SRacos() assert sracos.binary_search(set, test_s1, 0, 4) == 3 assert sracos.binary_search(set, test_s1, 0, 2) == 3 assert sracos.binary_search(set, test_s2, 0, 4) == 5 assert sracos.binary_search(set, test_s3, 0, 4) == 0 assert sracos.binary_search(set, test_s4, 0, 4) == 3
def test_resample(self): dim = 100 obj = Objective(func=ackley, dim=Dimension(dim, [[-1, 1]] * dim, [True] * dim)) sol = Solution(x=[0.2] * dim) res = obj.eval(sol) obj.resample(sol, 3) assert abs(sol.get_value()) <= 1e-7 sol.set_value(0) obj.resample_func(sol, 3) assert abs(sol.get_value()) <= 1e-7
def test_sracos_replace(self): s0 = Solution(x=[0, 0, 0], value=0.5) s1 = Solution(x=[1, 1, 1], value=1) s2 = Solution(x=[2, 2, 2], value=2) s3 = Solution(x=[3, 3, 3], value=3) s4 = Solution(x=[4, 4, 4], value=4) pos_set = [s0, s1, s2, s3, s4] neg_set = [s2, s3, s1, s4, s0] x = Solution(x=[2.1, 2.1, 2.1], value=0.1) sracos = SRacos() sracos.replace(pos_set, x, 'pos', 'WR') assert pos_set[4].get_value() == 3 and pos_set[0].get_value() == 0.1 sracos.replace(neg_set, x, 'neg', 'LM') assert neg_set[3].get_value() == 0.1
def test_sracos_strategy_wr(self): s0 = Solution(value=0) s1 = Solution(value=1) s2 = Solution(value=2) s3 = Solution(value=3) s4 = Solution(value=4) iset = [s0, s1, s2, s3, s4] sracos = SRacos() test_s1 = Solution(value=2.1) sracos.strategy_wr(iset, test_s1, 'pos') assert len(iset) == 5 and iset[0].get_value() == 0 and iset[1].get_value() == 1 and iset[2].get_value() == 2 \ and iset[3].get_value() == 2.1 and iset[4].get_value() == 3 iset2 = [s1, s3, s0, s2, s4] sracos.strategy_wr(iset2, test_s1, 'neg') assert len(iset2) == 5 and iset2[4].get_value() == 2.1
def test_theta_weak_dominate(self): sol1 = Solution(value=[1, 2]) sol2 = Solution(value=[4, 2]) assert PONSS.theta_weak_dominate(3, sol1, sol2) is True assert PONSS.theta_weak_dominate(3.1, sol1, sol2) is False
""" This file contains an example of how to optimize continuous ackley function. Author: Yu-Ren Liu, Xiong-Hui Chen """ from zoopt import Dimension, Objective, Parameter, ExpOpt, Solution from simple_function import ackley if __name__ == '__main__': dim = 100 # dimension objective = Objective(ackley, Dimension(dim, [[-1, 1]] * dim, [True] * dim)) # setup objective parameter = Parameter(budget=100 * dim, init_samples=[Solution([0] * 100)]) # init with init_samples solution_list = ExpOpt.min(objective, parameter, repeat=5, plot=False, plot_file="img/quick_start.png") for solution in solution_list: x = solution.get_x() value = solution.get_value() print(x, value)
def test_is_equal(self): sol1 = Solution(x=[1, 2, 3]) sol2 = Solution(x=[1, 3, 4]) assert sol1.is_equal(sol2) is False assert sol1.is_equal(sol1) is True
def test_deep_copy(self): sol1 = Solution(x=[1, 2, 3]) sol2 = sol1.deep_copy() assert sol1.is_equal(sol2)