optunity.solvers.util module

class optunity.solvers.util.Solver[source]

Bases: abc.SolverBase

Base class of all Optunity solvers.

maximize(f, pmap=<built-in function map>)[source]

Maximizes f.

Parameters:
  • f (callable) – the objective function
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

minimize(f, pmap=<built-in function map>)[source]

Minimizes f.

Parameters:
  • f (callable) – the objective function
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

optimize(f, maximize=True, pmap=<built-in function map>)[source]

Optimizes f.

Parameters:
  • f (callable) – the objective function
  • maximize (boolean) – do we want to maximizes?
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

class optunity.solvers.util.ThreadSafeQueue(lst=None)[source]

Bases: object

Initializes a new object.

Parameters:lst (list or None) – initial content
append(value)[source]

Acquires lock and appends value to the content.

>>> q1 = ThreadSafeQueue()
>>> q1
[]
>>> q1.append(1)
[1]
content
copy()[source]

Makes a deep copy of this ThreadSafeQueue.

>>> q1 = ThreadSafeQueue([1,2,3])
>>> q2 = q1.copy()
>>> q2.append(4)
>>> q1
[1, 2, 3]
>>> q2
[1, 2, 3, 4]
lock
optunity.solvers.util.scale_unit_to_bounds(seq, bounds)[source]

Scales all elements in seq (unit hypercube) to the box constraints in bounds.

Parameters:
  • seq (iterable of [lb, ub] pairs) – the sequence in the unit hypercube to scale
  • bounds – bounds to scale to
Returns:

a list of scaled elements of seq

>>> scale_unit_to_bounds([0.0, 0.5, 0.5, 1.0], [[-1.0, 2.0], [-2.0, 0.0], [0.0, 3.0], [0.0, 2.0]])
[-1.0, -1.0, 1.5, 2.0]
optunity.solvers.util.score(value)[source]

General wrapper around objective function evaluations to get the score.

Parameters:value – output of the objective function
Returns:the score

If value is a scalar, it is returned immediately. If value is iterable, its first element is returned.

optunity.solvers.util.shrink_bounds(bounds, coverage=0.99)[source]

Shrinks the bounds. The new bounds will cover the fraction coverage.

>>> [round(x, 3) for x in shrink_bounds([0, 1], coverage=0.99)]
[0.005, 0.995]
optunity.solvers.util.uniform_in_bounds(bounds)[source]

Generates a random uniform sample between bounds.

Parameters:bounds (dict {“name”: [lb ub], ...}) – the bounds we must adhere to