bayesian-optimization / BayesianOptimization

A Python implementation of global optimization with gaussian processes.
https://bayesian-optimization.github.io/BayesianOptimization/index.html
MIT License
7.95k stars 1.55k forks source link

`ScreenLogger.format_number` does not account for minus sign, potentially truncating exponents #515

Open till-m opened 3 months ago

till-m commented 3 months ago

c.f. #514

Demo

import numpy as np
from bayes_opt import BayesianOptimization

x = np.array([
    0.00869858, 0.01304788, 0.02174646, 0.03044504, 0.03914363,
    0.04784221, 0.05654079, 0.06523938, 0.07393796, 0.08263655,
    0.09133513, 0.10003371, 0.1087323 , 0.11743088, 0.12612946,
    0.13482805, 0.14352663, 0.15222522, 0.1609238 , 0.16962238,
    0.17832097, 0.18701955, 0.19571813, 0.20441672, 0.2131153 ,
    0.22181388, 0.23051247, 0.23921105, 0.24790964, 0.25660822,
    0.2653068 , 0.27400539, 0.28270397, 0.29140255, 0.30010114,
    0.30879972, 0.31749831, 0.32619689, 0.33489547, 0.34359406,
    0.35229264, 0.36099122, 0.36968981, 0.37838839, 0.38708698,
    0.39578556, 0.40448414, 0.41318273, 0.42188131, 0.43057989
])
y = np.array([
    54688.54691276, 56090.81734642, 49640.37335158, 54688.54691276,
    50481.73561177, 59660.23299573, 54364.94604345, 54408.09282602,
    50729.19510007, 52695.84682282, 55089.19560809, 52859.49852102,
    49472.10089954, 43470.38344347, 40907.61334058, 41525.29864839,
    37478.86431783, 34255.46345085, 37406.51129791, 34948.89388507,
    33449.28010048, 29545.51192782, 29728.1331936 , 31327.3181988 ,
    26356.96060002, 28045.40867321, 28733.31492368, 23405.1683291 ,
    26052.70858327, 23672.22630383, 22620.23125773, 25574.74171866,
    25370.30815361, 23985.10323843, 22192.45381967, 22989.33499691,
    23396.7861397 , 20921.87487021, 20269.18172291, 22365.32590395,
    20722.44085298, 19614.89124674, 22271.35394637, 18471.28640201,
    21553.99947469, 20063.25389699, 19631.78607125, 21609.72541978,
    20773.84137289, 19674.27911469
])

rt_max = 2*x[-1]
fd0 = 19896.45379

def distance(cd, rc, rt, fd):
    if rc > rt:
        return -1e10
    kdens = cd * (
        (1. / np.sqrt(1. + (x / rc) ** 2)) - (1. / np.sqrt(1. + (rt / rc) ** 2))
    ) ** 2 + fd
    model = np.where(x < rt, kdens, fd)
    # Return negative sum of squared diffs
    return -np.sum((y - model) ** 2)

pbounds = {
    "cd": [fd0, 10 * max(y)],
    "rc": [x[0], rt_max],
    "rt": [x[0], rt_max],
    "fd": [fd0 * .1, max(y)],
}

optimizer = BayesianOptimization(
    f=distance,
    pbounds=pbounds,
    random_state=1,
)

optimizer.maximize(n_iter=0, init_points=15,)

model = {}
for k, v in optimizer.max['params'].items():
    print(f"{k}: {v:.3f}")
    model[k] = v
lkl = optimizer.max['target']
print(f"Lkl: {lkl}")

Output

|   iter    |  target   |    cd     |    fd     |    rc     |    rt     |
-------------------------------------------------------------------------
| 1         | -2.882e+1 | 2.604e+05 | 4.353e+04 | 0.008796  | 0.2664    |
| 2         | -1.957e+1 | 1.045e+05 | 7.315e+03 | 0.1675    | 0.3033    |
| 3         | -4.322e+1 | 2.487e+05 | 3.306e+04 | 0.366     | 0.5928    |
| 4         | -6.689e+1 | 1.378e+05 | 5.263e+04 | 0.03205   | 0.5802    |
| 5         | -6.483e+0 | 2.606e+05 | 3.421e+04 | 0.1284    | 0.1776    |
| 6         | -5.783e+1 | 4.817e+05 | 5.783e+04 | 0.2759    | 0.5989    |
| 7         | -1e+10    | 5.253e+05 | 5.358e+04 | 0.0812    | 0.04199   |
| 8         | -5.865e+1 | 1.178e+05 | 5.263e+04 | 0.09254   | 0.3677    |
| 9         | -1e+10    | 5.723e+05 | 3.274e+04 | 0.5985    | 0.2777    |
| 10        | -3.039e+1 | 4.158e+05 | 5.012e+04 | 0.02429   | 0.6482    |
| 11        | -1.073e+1 | 5.902e+05 | 4.514e+04 | 0.2478    | 0.6815    |
| 12        | -1e+10    | 7.943e+04 | 2.782e+04 | 0.7832    | 0.259     |
| 13        | -3.974e+1 | 1.859e+05 | 9.488e+03 | 0.02521   | 0.5874    |
| 14        | -1e+10    | 1.419e+05 | 1.73e+04  | 0.4277    | 0.05419   |
| 15        | -1.592e+0 | 3.51e+05  | 1.045e+04 | 0.5111    | 0.6052    |
=========================================================================

Correct Output:

|      iter       |     target      |       cd        |       fd        |       rc        |       rt        |
-------------------------------------------------------------------------------------------------------------
| 1               | -2.882e+10      | 2.604e+05       | 4.353e+04       | 0.008796        | 0.2664          |
| 2               | -1.957e+10      | 1.045e+05       | 7.315e+03       | 0.1675          | 0.3033          |
| 3               | -4.322e+10      | 2.487e+05       | 3.306e+04       | 0.366           | 0.5928          |
| 4               | -6.689e+10      | 1.378e+05       | 5.263e+04       | 0.03205         | 0.5802          |
| 5               | -6.483e+09      | 2.606e+05       | 3.421e+04       | 0.1284          | 0.1776          |
| 6               | -5.783e+11      | 4.817e+05       | 5.783e+04       | 0.2759          | 0.5989          |
| 7               | -1e+10          | 5.253e+05       | 5.358e+04       | 0.0812          | 0.04199         |
| 8               | -5.865e+10      | 1.178e+05       | 5.263e+04       | 0.09254         | 0.3677          |
| 9               | -1e+10          | 5.723e+05       | 3.274e+04       | 0.5985          | 0.2777          |
| 10              | -3.039e+11      | 4.158e+05       | 5.012e+04       | 0.02429         | 0.6482          |
| 11              | -1.073e+12      | 5.902e+05       | 4.514e+04       | 0.2478          | 0.6815          |
| 12              | -1e+10          | 7.943e+04       | 2.782e+04       | 0.7832          | 0.259           |
| 13              | -3.974e+10      | 1.859e+05       | 9.488e+03       | 0.02521         | 0.5874          |
| 14              | -1e+10          | 1.419e+05       | 1.73e+04        | 0.4277          | 0.05419         |
| 15              | -1.592e+09      | 3.51e+05        | 1.045e+04       | 0.5111          | 0.6052          |
=============================================================================================================

Expected behavior format_number should reserve a character for the sign of the number.

Screenshots If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):