Skip to main content

Optimize both discrete and continuous variables using just a continuous optimizer such as in scipy.optimize

Project description

wrapdisc

wrapdisc is a Python 3.10 package to wrap a discrete optimization objective such that it can be optimized by a continuous optimizer such as in scipy.optimize. It maps the discrete variables into a continuous space, and uses an in-memory cache over the discrete space. Both discrete and continuous variables are supported, and are motivated by Ray Tune's search spaces.

cicd badge

Limitations

  • The use of an unbounded in-memory cache over the original objective function imposes a memory requirement. If multiple workers are used, each worker has its own such cache, thereby using additional memory for each worker. This cache prevents duplicated calls to the original objective function in a worker.
  • The ability to support constraints such as scipy.optimize.NonlinearConstraint or scipy.optimize.LinearConstraint is unclear. A constraint can however be modeled by returning inf upon its violation in the original objective function.

Links

Caption Link
Repo https://github.com/impredicative/wrapdisc/
Changelog https://github.com/impredicative/wrapdisc/releases
Package https://pypi.org/project/wrapdisc/

Installation

Python ≥3.10 is required. To install, run:

pip install wrapdisc

No additional third-party packages are required or installed.

Variables

The following classes of variables are available:

Space Usage Description Decoder Examples
Discrete ChoiceVar(items) Nominal (unordered categorical) one-hot via max • fn(["USA", "Panama", "Cayman"])
Discrete GridVar(values) Ordinal (ordered categorical) round • fn([2, 4, 8, 16])
• fn(["good", "better", "best"])
Discrete RandintVar(lower, upper) Integer from lower to upper, both inclusive round • fn(0, 6)
• fn(3, 9)
• fn(-10, 10)
Discrete QrandintVar(lower, upper, q) Quantized integer from lower to upper in multiples of q round to a multiple • fn(0, 12, 3)
• fn(1, 10, 2)
• fn(-10, 10, 4)
Continuous UniformVar(lower, upper) Float from lower to upper passthrough • fn(0.0, 5.11)
• fn(0.2, 4.6)
• fn(-10.0, 10.0)
Continuous QuniformVar(lower, upper, q) Quantized float from lower to upper in multiples of q round to a multiple • fn(0.0, 5.1, 0.3)
• fn(-5.1, -0.2, 0.3)

Usage

Example:

import operator
from typing import Any

import scipy.optimize

from wrapdisc import Objective
from wrapdisc.var import ChoiceVar, GridVar, QrandintVar, QuniformVar, RandintVar, UniformVar

def your_mixed_optimization_objective(x: tuple, *args: Any) -> float:
    return float(sum(x_i if isinstance(x_i, (int, float)) else len(str(x_i)) for x_i in (*x, *args)))

wrapped_objective = Objective(
            your_mixed_optimization_objective,
            variables=[
                ChoiceVar(["foobar", "baz"]),
                ChoiceVar([operator.index, abs, operator.invert]),
                GridVar([0.01, 0.1, 1, 10, 100]),
                GridVar(["good", "better", "best"]),
                RandintVar(-8, 10),
                QrandintVar(1, 10, 2),
                UniformVar(1.2, 3.4),
                QuniformVar(-11.1, 9.99, 0.22),
            ],
        )

optional_fixed_args = ("arg1", 2, 3.0)
result = scipy.optimize.differential_evolution(wrapped_objective, wrapped_objective.bounds, args=optional_fixed_args, seed=0)
cache_usage = wrapped_objective.cache_info
encoded_solution = result.x
decoded_solution = wrapped_objective[encoded_solution]
assert result.fun == wrapped_objective(encoded_solution, *optional_fixed_args)
assert result.fun == your_mixed_optimization_objective(decoded_solution, *optional_fixed_args)

Output:

>>> wrapped_objective.bounds
((0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (-0.49999999999999994, 4.499999999999999), (-0.49999999999999994, 2.4999999999999996), (-8.499999999999998, 10.499999999999998), (1.0000000000000002, 10.999999999999998), (1.2, 3.4), (-11.109999999999998, 10.009999999999998))

>>> result
     fun: 23.21
     jac: array([0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 1.00000009,
       0.        ])
 message: 'Optimization terminated successfully.'
    nfev: 7119
     nit: 42
 success: True
       x: array([  0.58467199,   0.72299406,   0.35864397,   0.65881822,
         0.12084548,  -0.46336082,  -0.22147362,  -8.01661046,
         2.32989067,   1.2       , -10.91142129])

>>> decoded_solution
('baz', <built-in function abs>, 0.01, 'good', -8, 2, 1.2, -11.0)

>>> your_mixed_optimization_objective(decoded_solution, *optional_fixed_args)
23.21

>>> cache_usage
CacheInfo(hits=153, misses=6966, maxsize=None, currsize=6966)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wrapdisc-1.3.3.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wrapdisc-1.3.3-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file wrapdisc-1.3.3.tar.gz.

File metadata

  • Download URL: wrapdisc-1.3.3.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for wrapdisc-1.3.3.tar.gz
Algorithm Hash digest
SHA256 f825de19a3943158fd2205a31687865d67405e5d172bb03294b33c12614fdfea
MD5 d6d5c5f0092f989ac5e0ed5b4fd02cb3
BLAKE2b-256 42c091b7c0dbc97dfd62b1017b7e8b0447313d6cd38ed36eb5cde2ab03ec3c66

See more details on using hashes here.

File details

Details for the file wrapdisc-1.3.3-py3-none-any.whl.

File metadata

  • Download URL: wrapdisc-1.3.3-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for wrapdisc-1.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a5cdaaa268f0492ce8a862e837baf448962589e5fca488cddcc4d8770b0d1f15
MD5 41b7a88656ed0d71e5f5a24a63fbe6a7
BLAKE2b-256 355cdf7dd07787c458ddfa23243658c2b3e7d853802ca605cc88286b34417334

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page