Skip to content

How to Optimize a Function with the Mixed (Continuous and Discrete) Search Space

Yu-Ren Liu edited this page Jan 1, 2018 · 10 revisions

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

We define a variant of Sphere function in fx.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.

Finally, use ZOOpt to optimize.

from fx import sphere_mixed
from zoopt import Dimension, 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
    """

    # 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)

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

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

Expeirment results

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