Usage#

We provide below basic usage information on how to use COBYQA. For more details on the minimize function, please refer to the API documentation.

How to use COBYQA#

COBYQA provides a minimize function. It solves unconstrained, bound-constrained, linearly constrained, and nonlinearly constrained optimization problems. For details on the signature of the minimize function, please refer to the API documentation.

We provide below simple examples on how to use COBYQA.

Examples#

Let us first minimize the Rosenbrock function implemented in scipy.optimize, defined as

\[f(x) = \sum_{i = 1}^{n - 1} 100 (x_{i + 1} - x_i^2)^2 + (x_i - 1)^2\]

for \(x \in \R^n\). To solve the problem using COBYQA, run:

from scipy.optimize import rosen
from cobyqa import minimize

x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
res = minimize(rosen, x0)
print(res.x)

This should display the desired output [1. 1. 1. 1. 1.].

To see how bound and linear constraints are handled using minimize, let us solve Example 16.4 of [UA1], defined as

\[\begin{split}\begin{aligned} \min_{x \in \R^2} & \quad (x_1 - 1)^2 + (x_2 - 2.5)^2\\ \text{s.t.} & \quad -x_1 + 2x_2 \le 2,\\ & \quad x_1 + 2x_2 \le 6,\\ & \quad x_1 - 2x_2 \le 2,\\ & \quad x_1 \ge 0,\\ & \quad x_2 \ge 0. \end{aligned}\end{split}\]

To solve the problem using COBYQA, run:

from cobyqa import minimize

def fun(x):
    return (x[0] - 1.0) ** 2.0 + (x[1] - 2.5) ** 2.0

x0 = [2.0, 0.0]
xl = [0.0, 0.0]
aub = [[-1.0, 2.0], [1.0, 2.0], [1.0, -2.0]]
bub = [2.0, 6.0, 2.0]
res = minimize(fun, x0, xl=xl, aub=aub, bub=bub)
print(res.x)

This should display the desired output [1.4 1.7].

Finally, to see how nonlinear constraints are handled, we solve Problem (F) of [UA2], defined as

\[\begin{split}\begin{aligned} \min_{x \in \R^2} & \quad -x_1 - x_2\\ \text{s.t.} & \quad x_1^2 - x_2 \le 0,\\ & \quad x_1^2 + x_2^2 \le 1. \end{aligned}\end{split}\]

To solve the problem using COBYQA, run:

from cobyqa import minimize

def fun(x):
    return -x[0] - x[1]

def cub(x):
    return [x[0] ** 2.0 - x[1], x[0] ** 2.0 + x[1] ** 2.0 - 1.0]

x0 = [1.0, 1.0]
res = minimize(fun, x0, cub=cub)
print(res.x)

This should display the desired output [0.7071 0.7071].

References

[UA1]

J. Nocedal and S. J. Wright. Numerical Optimization. Springer Series in Operations Research and Financial Engineering. Springer, New York, NY, USA, second edition, 2006.

[UA2]

M. J. D. Powell. A direct search optimization method that models the objective and constraint functions by linear interpolation. In S. Gomez and J. P. Hennart, editors, Advances in Optimization and Numerical Analysis, volume 275 of Mathematics and Its Applications, pages 51–67. Springer, Dordrecht, The Netherlands, 1994.