From b4044807ecbc31e6f4ad3b1b41a1d4b303266777 Mon Sep 17 00:00:00 2001 From: Yuren Liu Date: Tue, 20 Nov 2018 22:47:59 +0800 Subject: [PATCH] Fix bugs in discrete optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Fix bugs in the initialization step during the discrete optimization 2. Fix the bugs that index_set size equals zero during the discrete optimization 3. Now users can initialize sample set using “init_samples = [Solution(), …]“ --- docs/Explanation-of-Parameters-in-ZOOpt.rst | 2 +- .../continuous_sre_opt.py | 2 +- example/simple_functions/quick_start.py | 4 ++-- example/test.py | 18 ++++++++++++++++++ .../racos/racos_classification.py | 2 ++ .../algos/opt_algorithms/racos/racos_common.py | 11 ++++++++--- zoopt/dimension.py | 5 +---- zoopt/solution.py | 2 +- 8 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 example/test.py diff --git a/docs/Explanation-of-Parameters-in-ZOOpt.rst b/docs/Explanation-of-Parameters-in-ZOOpt.rst index 5396107..e79f822 100644 --- a/docs/Explanation-of-Parameters-in-ZOOpt.rst +++ b/docs/Explanation-of-Parameters-in-ZOOpt.rst @@ -234,7 +234,7 @@ be set to ``True``. ``num_sre``, ``low_dimension`` and ``withdraw_alpha`` are parameters used in ``SRE`` and should be provided by users. ``num_sre`` means the number of sequential random embedding. ``low_dimension`` stands for the low dimension ``SRE`` projects to. -``withdraw_alpha`` and ``variance_A`` are optimal parameters. +``withdraw_alpha`` and ``variance_A`` are optional parameters. ``withdraw_alpha``, a withdraw variable to the previous solution, is a ``Dimension`` object with only one dimension. ``variance_A`` specifies the variance of the projection matrix A. By default, ``withdraw_alpha`` diff --git a/example/sequential_random_embedding/continuous_sre_opt.py b/example/sequential_random_embedding/continuous_sre_opt.py index 574af7d..208936d 100644 --- a/example/sequential_random_embedding/continuous_sre_opt.py +++ b/example/sequential_random_embedding/continuous_sre_opt.py @@ -26,7 +26,7 @@ def minimize_sphere_sre(): budget = 2000 # number of calls to the objective function parameter = Parameter(budget=budget, high_dim_handling=True, reducedim=True, num_sre=5, low_dimension=Dimension(10, [[-1, 1]] * 10, [True] * 10)) - solution_list = ExpOpt.min(objective, parameter, repeat=5, plot=True, plot_file="img/minimize_sphere_sre.png") + solution_list = ExpOpt.min(objective, parameter, repeat=5, plot=False, plot_file="img/minimize_sphere_sre.png") if __name__ == "__main__": diff --git a/example/simple_functions/quick_start.py b/example/simple_functions/quick_start.py index 2aa0c13..b463a2c 100644 --- a/example/simple_functions/quick_start.py +++ b/example/simple_functions/quick_start.py @@ -5,13 +5,13 @@ Yu-Ren Liu, Xiong-Hui Chen """ -from zoopt import Dimension, Objective, Parameter, ExpOpt +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, sequential=False) + 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() diff --git a/example/test.py b/example/test.py new file mode 100644 index 0000000..f19d73e --- /dev/null +++ b/example/test.py @@ -0,0 +1,18 @@ +import numpy as np +from zoopt import Dimension, Objective, Parameter, ExpOpt, Solution, Opt +from zoopt.utils.zoo_global import gl + +def fn(solution): + x = solution.get_x() + x1 = [0,3,1,0,0] + # print(x) + val = sum([abs(v-x1[i]) for i,v in enumerate(x)])+ ((np.sum([i for i in x[0:3]])-1.0)**2) + return val + +dim = 5 +gl.set_seed(0) +dimobj = Dimension(dim, regs=[[0,5]]*dim, tys=[False]*dim) +obj = Objective(fn, dimobj) +param = Parameter(budget=1000) +solution = Opt.min(obj, param) +print(solution.get_x(), solution.get_value()) \ No newline at end of file diff --git a/zoopt/algos/opt_algorithms/racos/racos_classification.py b/zoopt/algos/opt_algorithms/racos/racos_classification.py index 3c2017e..d687f29 100644 --- a/zoopt/algos/opt_algorithms/racos/racos_classification.py +++ b/zoopt/algos/opt_algorithms/racos/racos_classification.py @@ -149,6 +149,8 @@ def mixed_classification(self): i += 1 if delete != 0: index_set.remove(k) + if len(index_set) == 0: + index_set.append(k) self.set_uncertain_bit(index_set) return diff --git a/zoopt/algos/opt_algorithms/racos/racos_common.py b/zoopt/algos/opt_algorithms/racos/racos_common.py index 765c782..7a6a507 100755 --- a/zoopt/algos/opt_algorithms/racos/racos_common.py +++ b/zoopt/algos/opt_algorithms/racos/racos_common.py @@ -5,8 +5,9 @@ Yu-Ren Liu """ -import copy +import copy, math from zoopt.utils.tool_function import ToolFunction +from zoopt.solution import Solution class RacosCommon: @@ -62,8 +63,12 @@ def init_attribute(self): if iteration_num < size: size = iteration_num for j in range(size): - x = self._objective.construct_solution(data_temp[j]) - self._objective.eval(x) + if isinstance(data_temp[j], Solution) is False: + x = self._objective.construct_solution(data_temp[j]) + else: + x = data_temp[j] + if math.isnan(x.get_value()): + self._objective.eval(x) self._data.append(x) ToolFunction.log("init solution %s, value: %s" % (i, x.get_value())) i += 1 diff --git a/zoopt/dimension.py b/zoopt/dimension.py index bd05c4b..a936cc3 100644 --- a/zoopt/dimension.py +++ b/zoopt/dimension.py @@ -113,11 +113,8 @@ def rand_sample(self): if self._types[i] is True: value = gl.rand.uniform( self._regions[i][0], self._regions[i][1]) - elif self._order is True: - value = gl.rand.randint(self._regions[i][0], self._regions[i][1]) else: - rand_index = gl.rand.randint(0, len(self._regions[i]) - 1) - value = self._regions[i][rand_index] + value = gl.rand.randint(self._regions[i][0], self._regions[i][1]) x.append(value) return x diff --git a/zoopt/solution.py b/zoopt/solution.py index e4d0577..0f24059 100644 --- a/zoopt/solution.py +++ b/zoopt/solution.py @@ -4,11 +4,11 @@ Author: Yu-Ren Liu, Xiong-Hui Chen """ - from zoopt.utils.zoo_global import pos_inf, neg_inf, nan, gl from zoopt.utils.tool_function import ToolFunction import copy + class Solution: """ A solution encapsulates a solution vector with attached properties, including dimension information, objective value,