def test_judge_match(self):
     size = 3
     regs = [[1, 5], [-1, 1], [1, 2]]
     tys = [True, True, True]
     assert Dimension.judge_match(size, regs, tys) == True
     tys = [True, True]
     assert Dimension.judge_match(size, regs, tys) == False
def run_test_handlingnoise(task_name, layers, in_budget, max_step, repeat, terminal_value):
    """
    example of running direct policy search for gym task with noise handling.

    :param task_name: gym task name
    :param layers:
        layer information of the neural network
        e.g., [2, 5, 1] means input layer has 2 neurons, hidden layer(only one) has 5 and output layer has 1
    :param in_budget:  number of calls to the objective function
    :param max_step: max step in gym
    :param repeat:  number of repeatitions for noise handling
    :param terminal_value: early stop, algorithm should stop when such value is reached
    :return: no return value
    """
    gym_task = GymTask(task_name)  # choose a task by name
    gym_task.new_nnmodel(layers)  # construct a neural network
    gym_task.set_max_step(max_step)  # set max step in gym

    budget = in_budget  # number of calls to the objective function
    rand_probability = 0.95  # the probability of sample in model

    # set dimension
    dim_size = gym_task.get_w_size()
    dim_regs = [[-10, 10]] * dim_size
    dim_tys = [True] * dim_size
    dim = Dimension(dim_size, dim_regs, dim_tys)
    # form up the objective function
    objective = Objective(gym_task.sum_reward, dim)
    # by default, the algorithm is sequential RACOS
    parameter = Parameter(budget=budget, autoset=True,
                          suppression=True, terminal_value=terminal_value)
    parameter.set_resample_times(70)
    parameter.set_probability(rand_probability)

    solution_list = ExpOpt.min(objective, parameter, repeat=repeat)
 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
Esempio n. 4
0
def minimize_sphere_mixed():
    """
    Mixed optimization example of minimizing sphere function, which has mixed search search space.

    :return: no return value
    """

    # setup optimization problem
    dim_size = 100
    dim_regs = []
    dim_tys = []
    # In this example, the search space is discrete if this dimension index is odd, Otherwise, the search space
    # is continuous.
    for i in range(dim_size):
        if i % 2 == 0:
            dim_regs.append([0, 1])
            dim_tys.append(True)
        else:
            dim_regs.append([0, 100])
            dim_tys.append(False)
    dim = Dimension(dim_size, dim_regs, dim_tys)
    objective = Objective(sphere_mixed, dim)  # form up the objective function
    budget = 100 * dim_size  # number of calls to the objective function
    parameter = Parameter(budget=budget)

    solution_list = ExpOpt.min(objective, parameter, repeat=1, plot=True, plot_file="img/sphere_mixed_figure.png")
Esempio n. 5
0
def minimize_ackley_continuous_noisy():
    """
    SSRacos example of minimizing ackley function under Gaussian noise

    :return: no return value
    """
    ackley_noise_func = ackley_noise_creator(0, 0.1)
    dim_size = 100  # dimensions
    dim_regs = [[-1, 1]] * dim_size  # dimension range
    dim_tys = [True] * dim_size  # dimension type : real
    dim = Dimension(dim_size, dim_regs,
                    dim_tys)  # 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)

    ExpOpt.min(objective,
               parameter,
               repeat=5,
               plot=False,
               plot_file="img/ackley_continuous_noisy_figure.png")
Esempio n. 6
0
 def get_dim(self):
     """
     Construct a Dimension object of this problem.
     :return: a dimension object of sparse mse.
     """
     dim_regs = [[0, 1]] * self._size
     dim_tys = [False] * self._size
     return Dimension(self._size, dim_regs, dim_tys)
Esempio n. 7
0
 def test_sracos_performance(self):
     dim = 100  # dimension
     objective = Objective(ackley,
                           Dimension(dim, [[-1, 1]] * dim,
                                     [True] * dim))  # setup objective
     parameter = Parameter(budget=100 * dim)
     solution = Opt.min(objective, parameter)
     assert solution.get_value() < 0.2
def minimize_sphere_sre():
    """
    Example of minimizing high-dimensional sphere function with sequential random embedding.

    :return: no return value
    """

    dim_size = 10000  # dimensions
    dim_regs = [[-1, 1]] * dim_size  # dimension range
    dim_tys = [True] * dim_size  # dimension type : real
    dim = Dimension(dim_size, dim_regs, dim_tys)  # form up the dimension object
    objective = Objective(sphere_sre, dim)  # form up the objective function

    # setup algorithm parameters
    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=False, plot_file="img/minimize_sphere_sre.png")
Esempio n. 9
0
    def test_performance(self):
        dim_size = 10000  # dimensions
        dim_regs = [[-1, 1]] * dim_size  # dimension range
        dim_tys = [True] * dim_size  # dimension type : real
        dim = Dimension(dim_size, dim_regs,
                        dim_tys)  # form up the dimension object
        objective = Objective(sphere_sre,
                              dim)  # form up the objective function

        # setup algorithm parameters
        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 = Opt.min(objective, parameter)
        assert solution.get_value() < 0.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_limited_space(self):
     dim1 = Dimension(2, [[-1, 1], [-1, 1]], [True, True])
     limited, number = dim1.limited_space()
     assert limited is False and number == 0
     dim2 = Dimension(2, [[-1, 1], [-1, 1]], [False, False])
     limited, number = dim2.limited_space()
     assert limited is True and number == 9
    def test_resample(self):
        ackley_noise_func = ackley_noise_creator(0, 0.1)
        dim_size = 100  # dimensions
        dim_regs = [[-1, 1]] * dim_size  # dimension range
        dim_tys = [True] * dim_size  # dimension type : real
        dim = Dimension(dim_size, dim_regs, dim_tys)  # 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, resampling=True, resample_times=10)

        # 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 minimize_sphere_continuous():
    """
    Example of minimizing the sphere function

    :return: no return value
    """
    dim_size = 100
    # form up the objective function
    objective = Objective(
        sphere, Dimension(dim_size, [[-1, 1]] * dim_size, [True] * dim_size))

    budget = 100 * dim_size
    # if intermediate_result is True, ZOOpt will output intermediate best solution every intermediate_freq budget
    parameter = Parameter(budget=budget,
                          intermediate_result=True,
                          intermediate_freq=1000)
    ExpOpt.min(objective,
               parameter,
               repeat=1,
               plot=True,
               plot_file="img/sphere_continuous_figure.png")
def minimize_ackley_continuous():
    """
    Continuous optimization example of minimizing the ackley function.

    :return: no return value
    """
    dim_size = 100  # dimensions
    dim_regs = [[-1, 1]] * dim_size  # dimension range
    dim_tys = [True] * dim_size  # dimension type : real
    dim = Dimension(dim_size, dim_regs,
                    dim_tys)  # form up the dimension object

    objective = Objective(ackley, dim)  # form up the objective function

    budget = 100 * dim_size  # number of calls to the objective function
    parameter = Parameter(budget=budget)

    solution_list = ExpOpt.min(objective,
                               parameter,
                               repeat=1,
                               plot=True,
                               plot_file="img/ackley_continuous_figure.png")
Esempio n. 15
0
def minimize_sphere_discrete_order():
    """
    Discrete optimization example of minimizing the sphere function, which has ordered search space.

    :return: no return value
    """
    dim_size = 100  # dimensions
    dim_regs = [[-10, 10]] * dim_size  # dimension range
    dim_tys = [False] * dim_size  # dimension type : integer
    dim_order = [True] * dim_size
    dim = Dimension(dim_size, dim_regs, dim_tys,
                    order=dim_order)  # form up the dimension object
    objective = Objective(sphere_discrete_order,
                          dim)  # form up the objective function

    # setup algorithm parameters
    budget = 10000  # number of calls to the objective function
    parameter = Parameter(budget=budget)

    ExpOpt.min(objective,
               parameter,
               repeat=1,
               plot=True,
               plot_file="img/sphere_discrete_order_figure.png")
 def dim(self):
     """
     Construct dimension of this problem.
     """
     return Dimension(self.__dim_size, [[-10, 10]] * self.__dim_size,
                      [True] * self.__dim_size)
 def test_merge_dim(self):
     dim1 = Dimension(1, [[1, 2]], [True])
     dim2 = Dimension(2, [[1, 2], [2, 3]], [True, True])
     dim3 = Dimension.merge_dim(dim1, dim2)
     assert dim3.equal(
         Dimension(3, [[1, 2], [1, 2], [2, 3]], [True, True, True]))
 def test_set_regions(self):
     dim = Dimension(2, [[1, 2], [2, 3]], [True, True])
     dim.set_regions([[-1, 1], [-1, 1]], [True, True])
     assert dim.equal(Dimension(2, [[-1, 1], [-1, 1]], [True, True]))
 def test_deep_copy(self):
     dim1 = Dimension(2, [[-1, 1], [-1, 1]], [True, True])
     dim2 = dim1.deep_copy()
     assert dim1.equal(dim2)
 def test_is_discrete(self):
     dim1 = Dimension(2, [[-1, 1], [-1, 1]], [True, False])
     assert dim1.is_discrete() is False
 def test_copy_region(self):
     dim1 = Dimension(2, [[-1, 1], [-1, 1]], [True, True])
     region = dim1.copy_region()
     assert region == [[-1, 1], [-1, 1]]
"""
This file contains an example of how to optimize continuous ackley function.

Author:
    Yu-Ren Liu, Xiong-Hui Chen
"""

from zoopt.zoopt import Dimension, Objective, Parameter, ExpOpt, Solution
from zoopt.example.simple_functions.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)