Skip to main content

Pimp your objective function for faster, robust optimization

Project description

embarrassingly

Embarrassingly obvious (in retrospect) ways to hack objective functions before you send them to optimization routines. See blog article for motivation and explanation

Install

pip install embarrassingly 

Example 1 : Parallel objective computation

See optuna_parallel.py

from embarrassingly.parallel import Parallel
import optuna

def pre_objective(worker, trial):
    print('Hi this is worker ' + str(worker))
    x = [trial.suggest_float('x' + str(i), 0, 1) for i in range(3)]
    return x[0] + x[1] * x[2]

def test_optuna():
    objective = Parallel(pre_objective, num_workers=7)
    study = optuna.create_study()
    study.optimize(objective, n_trials=15, n_jobs=7)

Example 2 : Plateau finding

See underpromoted_shgo.py

from scipy.optimize import shgo
from embarrassingly.underpromoted import plateaudinous, Underpromoted2d

bounds = [(-1 ,1) ,(-1 ,1)]
f = plateaudinous
res1 = shgo(func=f, bounds=bounds, n=8, iters=4, options={'minimize_every_iter': True, 'ftol': 0.1})
print('Minimum at '+str(res1.x))

f_tilde = Underpromoted2d(f, bounds=bounds, radius=0.05)
res1 = shgo(func=f_tilde, bounds=bounds, n=8, iters=4, options={'minimize_every_iter': True, 'ftol': 0.1})
print('Landed at '+str(res1.x))

Example 3 : Expensive functions

See shy_shgo.py

def slow_and_pointless(x):
""" Example of a function with varying computation time """
    r = np.linalg.norm(x)
    quad = (0.5*0.5-r*r)/(0.5*0.5)
    compute_time = max(0,0.5*quad+x[0])
    time.sleep(compute_time)
    return schwefel([1000*x[0],980*x[1]])[0]

# Save time by making it a "shy" objective function
bounds = [(-0.5, 0.5), (-0.5, 0.5)]
SAP = Shy(slow_and_pointless, bounds=bounds, t_unit=0.01, d_unit=0.3)
from scipy.optimize import minimize
res = scipy.optimize.shgo(func=SAP, bounds=bounds, n=8, iters=4, options={'minimize_every_iter': True, 'ftol': 0.1})

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

embarrassingly-0.0.5.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

embarrassingly-0.0.5-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file embarrassingly-0.0.5.tar.gz.

File metadata

  • Download URL: embarrassingly-0.0.5.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for embarrassingly-0.0.5.tar.gz
Algorithm Hash digest
SHA256 486e43d3c7a59ba5e3710005a57aaea2fec31b9747b82ea77f501a86d0686c23
MD5 f2ce5ed50b2bbe9b9c8e2d03811eaecb
BLAKE2b-256 aaa2bd959b4bac256f9066acc591745a37c5ff9b8413fadd78eb0062e142a9d6

See more details on using hashes here.

File details

Details for the file embarrassingly-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: embarrassingly-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for embarrassingly-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e0674a5e92b9c6d2f7b56cd764f798cc3e490494ed303ffe2cc88a863c0476bc
MD5 d7a3635979d87b063f143f277cad9e61
BLAKE2b-256 56236ceb20c21c463777e0ac90cba5f7cd098df13a032bf699e0dad12fdba504

See more details on using hashes here.

Supported by

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