Optimize a Function with Mixed Search SpaceΒΆ

In some cases, the search space of the problem consists of both continuous subspace and discrete subspace. ZOOpt can solve this kind of problem easily.

We define the Sphere function in simple_function.py for minimization.

def sphere_mixed(solution):
    """
    Sphere function for mixed optimization
    """
    x = solution.get_x()
    value = sum([i*i for i in x])
    return value

Then, define corresponding objective and parameter.

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)
# dim = Dimension2([(ValueType.CONTINUOUS, [0, 1], 1e-6), (ValueType.DISCRETE, [0, 100], False)] * (dim_size/2))
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)

Finally, use ZOOpt to optimize.

solution_list = ExpOpt.min(objective, parameter, repeat=1, plot=True)

The whole process lists below.

from simple_function import sphere_mixed
from zoopt import Dimension, ValueType, Dimension2, Objective, Parameter, ExpOpt


# mixed optimization
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)
    # dim = Dimension2([(ValueType.CONTINUOUS, [0, 1], 1e-6), (ValueType.DISCRETE, [0, 100], False)] * (dim_size/2)
    objective = Objective(sphere_mixed, dim)  # form up the objective function
    budget = 100 * dim_size  # the number of calls to the objective function
    parameter = Parameter(budget=budget)

    solution_list = ExpOpt.min(objective, parameter, repeat=1, plot=True)

if __name__ == '__main__':
    minimize_sphere_mixed()

For a few seconds, the optimization is done. Visualized optimization progress looks like

https://github.com/eyounx/ZOOpt/blob/dev/img/sphere_mixed_figure.png?raw=true

More concrete examples are available in the example/simple_functions/mixed_opt.py file .