pyObjective!

pyObjective is a python package to complement scipy’s optimize toolbox.

Introduction

pyObjective is a tool to define complex models and solve them using scipy’s optimize module.

Scipy however works by asking for a function, and the bounds and returns a vector that optimizes the function. Scipy does not have any understanding of what the variables are and how they are arranged. This makes using it sometimes tricky: the user is required to know and keep track of the variables and the respective variable bounds.

Instead, pyObjective allows a user to define a variable on the fly, defining the variable bounds at the time the variable is created. From there the variable is added to the model, and the model’s solve method is called to optimize the cost function.

The simplest possible example is below, but more in-depth examples are available on the left.

Quick Start

Here is a minimal example on finding the minimum of \(x^2\).

from pyObjective import Variable, Model

# define a variable, with an initial guess and the (lower, upper) bounds.
x = Variable("x", 1, (-2,2))

# define a model
m = Model()

# add the variable to the model
m.add_var(x)

# define a cost function
def cost():
    return x()**2

#define the cost function as the function to optimize.
m.objective = cost

# solve the problem
m.solve()

m.display_results()

and this gives the formatted output

+--------+----------------------+
| Cost:  | 6.15615700998697e-18 |
+--------+----------------------+
+--------+----------------------+
+----------+-----------------------+-------------+
| Variable |         Value         | Description |
+----------+-----------------------+-------------+
|    x     | 2.481160416012429e-09 |     None    |
+----------+-----------------------+-------------+

which is very close to the true optimal, \(x=0\). The small deviation occurs because the underlying method used is scipy.optimize.dual_annealing. This method is effective for problems which have many local minima, but is fundamentally a stochastic method, and does is not guaranteed to get the numerically exact result.

Look at the examples to see examples of how to use the package.

Future Developments

The next few steps include:

  • adding linear and non-linear constraints

  • support for units

  • create model instances with variables fully defined, so they can be inspected, or used in the future.

  • adding more representative examples.