Skip to content

Commit

Permalink
Fix bugs in discrete optimization
Browse files Browse the repository at this point in the history
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(), …]“
  • Loading branch information
AlexLiuyuren committed Nov 20, 2018
1 parent 5779574 commit b404480
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/Explanation-of-Parameters-in-ZOOpt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
2 changes: 1 addition & 1 deletion example/sequential_random_embedding/continuous_sre_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
4 changes: 2 additions & 2 deletions example/simple_functions/quick_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions example/test.py
Original file line number Diff line number Diff line change
@@ -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())
2 changes: 2 additions & 0 deletions zoopt/algos/opt_algorithms/racos/racos_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions zoopt/algos/opt_algorithms/racos/racos_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions zoopt/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion zoopt/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b404480

Please sign in to comment.