Skip to content

Quick Start

Yang Yu edited this page Jan 7, 2018 · 15 revisions

ZOOpt is a python package of Zeroth-Order Optimization.

Required packages

This package requires the following packages:

The easiest way to get these is to use pip or conda environment manager. Typing the following command in your terminal will install all required packages in your Python environment.

$ conda install numpy matplotlib

or

$ pip install numpy matplotlib

Getting and installing ZOOpt

The easiest way to get ZOOpt is to type pip install zoopt in you terminal/command line.

If you want to install ZOOpt by source code, download this project and sequentially run following commands in your terminal/command line.

$ python setup.py build
$ python setup.py install

A quick example

We define the Ackley function for minimization (note that this function is for arbitrary dimensions, determined by the solution)

def ackley(solution):
    """Ackley function for continuous optimization"""
    x = solution.get_x()
    bias = 0.2
    ave_seq = sum([(i - bias) * (i - bias) for i in x]) / len(x)
    ave_cos = sum([np.cos(2.0*np.pi*(i-bias)) for i in x]) / len(x)
    value = -20 * np.exp(-0.2 * np.sqrt(ave_seq)) - np.exp(ave_cos) + 20.0 + np.e
    return value

Ackley function is a classical function with many local minima. In 2-dimension, it looks like (from wikipedia)

Expeirment results

We then use ZOOpt to optimize a 100-dimension Ackley function:

  1. import components from zoopt, where ExpOpt is the experiment-purpose replacement of Opt
from zoopt import Dimension, Objective, Parameter, ExpOpt
  1. set the dimension
dim = 100  # dimension
  1. setup the Dimension object
dimobj = Dimension(dim, [[-1, 1]] * dim, [True] * dim)
  1. setup the Objective object defining the function to be minimized
objective = Objective(ackley, dimobj) 
  1. setup the parameters of the optimization algorithms. The minimum variable is the budget size
parameter = Parameter(budget=100 * dim)
  1. perform the optimization by
solution = Opt.min(objective, parameter)

or using the ExpOpt (from v0.2) for experiments, supporting multi-repeat and ploting,

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

Finally the whole piece of the code for optimization is:

from zoopt import Dimension, Objective, Parameter, ExpOpt

if __name__ == '__main__':
    dim = 100  # dimension
    objective = Objective(ackley, Dimension(dim, [[-1, 1]] * dim, [True] * dim))  # setup objective
    parameter = Parameter(budget=100 * dim)
    solution_list = ExpOpt.min(objective, parameter, repeat=1, plot=True)  

For a few seconds, the optimization is done. The terminal/command line will show optimization result

Expeirment results
Visualized optimization progress looks like
Expeirment results

The rest of the tutorial introduces more functions of ZOOpt.