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

Async example using standard library #503

Closed phi-friday closed 2 months ago

phi-friday commented 4 months ago

An async example that does not use tornado. Compatible with #447 , #497 .

output

❯ poetry run python ./examples/async_optimization_using_asyncio.py
optimizer 1 wants to register: {}.
optimizer 1 got -21.100297200767475 as target.
optimizer 1 will to register next: {'params': {'x': 3.077062879511166, 'y': -2.554149861261048}, 'target': -21.100297200767475}.

optimizer 3 wants to register: {}.
optimizer 3 got -10.546430335262063 as target.
optimizer 3 will to register next: {'params': {'x': -3.1987354495358904, 'y': -0.14652599584330717}, 'target': -10.546430335262063}.

optimizer 2 wants to register: {}.
optimizer 2 got -7.52922723314148 as target.
optimizer 2 will to register next: {'params': {'x': -0.6592425110985811, 'y': -1.845105717667081}, 'target': -7.52922723314148}.

optimizer 1 wants to register: {'params': {'x': 3.077062879511166, 'y': -2.554149861261048}, 'target': -21.100297200767475}.
optimizer 1 got -18.62547976566032 as target.
optimizer 1 will to register next: {'params': {'x': -3.99855050239952, 'y': 2.907111335350107}, 'target': -18.62547976566032}.

optimizer 3 wants to register: {'params': {'x': -3.1987354495358904, 'y': -0.14652599584330717}, 'target': -10.546430335262063}.
optimizer 3 got -10.785140892249524 as target.
optimizer 3 will to register next: {'params': {'x': -0.09106196454413151, 'y': -2.4317413379890516}, 'target': -10.785140892249524}.

optimizer 1 wants to register: {'params': {'x': -3.99855050239952, 'y': 2.907111335350107}, 'target': -18.62547976566032}.
optimizer 1 got -6.9513291836534705 as target.
optimizer 1 will to register next: {'params': {'x': -1.7042293053783366, 'y': -1.2465377046342094}, 'target': -6.9513291836534705}.

optimizer 2 wants to register: {'params': {'x': -0.6592425110985811, 'y': -1.845105717667081}, 'target': -7.52922723314148}.
optimizer 2 got -9.252942089955432 as target.
optimizer 2 will to register next: {'params': {'x': -1.9681196432024617, 'y': -1.5257567499654527}, 'target': -9.252942089955432}.

optimizer 3 wants to register: {'params': {'x': -0.09106196454413151, 'y': -2.4317413379890516}, 'target': -10.785140892249524}.
optimizer 3 got -21.57347936525579 as target.
optimizer 3 will to register next: {'params': {'x': -2.7668676351475074, 'y': -2.8623726975563906}, 'target': -21.57347936525579}.

optimizer 3 wants to register: {'params': {'x': -2.7668676351475074, 'y': -2.8623726975563906}, 'target': -21.57347936525579}.
optimizer 3 got 0.7627702981116544 as target.
optimizer 3 will to register next: {'params': {'x': -0.13910576239972006, 'y': 0.5332245842425523}, 'target': 0.7627702981116544}.

optimizer 1 wants to register: {'params': {'x': -1.7042293053783366, 'y': -1.2465377046342094}, 'target': -6.9513291836534705}.
optimizer 1 got -30.41332924795998 as target.
optimizer 1 will to register next: {'params': {'x': -3.955069082531736, 'y': -2.9712413424975255}, 'target': -30.41332924795998}.

optimizer 2 wants to register: {'params': {'x': -1.9681196432024617, 'y': -1.5257567499654527}, 'target': -9.252942089955432}.
optimizer 2 got 0.5094238101556255 as target.
optimizer 2 will to register next: {'params': {'x': -0.33478072822557614, 'y': 0.38477804504949}, 'target': 0.5094238101556255}.

optimizer 3 wants to register: {'params': {'x': -0.13910576239972006, 'y': 0.5332245842425523}, 'target': 0.7627702981116544}.
optimizer 3 got -3.4595513273103347 as target.
optimizer 3 will to register next: {'params': {'x': 1.3141852679277566, 'y': 2.6530179699180483}, 'target': -3.4595513273103347}.

optimizer 1 wants to register: {'params': {'x': -3.955069082531736, 'y': -2.9712413424975255}, 'target': -30.41332924795998}.
optimizer 1 got -7.40063770760273 as target.
optimizer 1 will to register next: {'params': {'x': 2.097769698418473, 'y': 3.0}, 'target': -7.40063770760273}.

optimizer 3 wants to register: {'params': {'x': 1.3141852679277566, 'y': 2.6530179699180483}, 'target': -3.4595513273103347}.
optimizer 3 got -17.80960406315896 as target.
optimizer 3 will to register next: {'params': {'x': 3.99152169675457, 'y': 2.6962778685983255}, 'target': -17.80960406315896}.

optimizer 1 wants to register: {'params': {'x': 2.097769698418473, 'y': 3.0}, 'target': -7.40063770760273}.
optimizer 1 got -1.084409068928497 as target.
optimizer 1 will to register next: {'params': {'x': -0.7688071782577142, 'y': 2.2220247917239275}, 'target': -1.084409068928497}.

optimizer 1 wants to register: {'params': {'x': -0.7688071782577142, 'y': 2.2220247917239275}, 'target': -1.084409068928497}.
optimizer 1 got 0.7992533007528416 as target.
optimizer 1 will to register next: {'params': {'x': -0.27741788732234784, 'y': 1.351832367812803}, 'target': 0.7992533007528416}.

optimizer 2 wants to register: {'params': {'x': -0.33478072822557614, 'y': 0.38477804504949}, 'target': 0.5094238101556255}.
optimizer 2 got -19.0 as target.
optimizer 2 will to register next: {'params': {'x': 4.0, 'y': 3.0}, 'target': -19.0}.

optimizer 2 wants to register: {'params': {'x': 4.0, 'y': 3.0}, 'target': -19.0}.
optimizer 2 got -0.771130632671361 as target.
optimizer 2 will to register next: {'params': {'x': 1.3206718333240124, 'y': 0.8358155265076721}, 'target': -0.771130632671361}.

optimizer 3 wants to register: {'params': {'x': 3.99152169675457, 'y': 2.6962778685983255}, 'target': -17.80960406315896}.
optimizer 3 got -0.9932407833280448 as target.
optimizer 3 will to register next: {'params': {'x': -0.8150099152392469, 'y': 2.1528224587462548}, 'target': -0.9932407833280448}.

optimizer 1 wants to register: {'params': {'x': -0.27741788732234784, 'y': 1.351832367812803}, 'target': 0.7992533007528416}.
optimizer 1 got 0.27660495983220357 as target.
optimizer 1 will to register next: {'params': {'x': 0.8503719180120841, 'y': 1.0162062094348336}, 'target': 0.27660495983220357}.

optimizer 3 wants to register: {'params': {'x': -0.8150099152392469, 'y': 2.1528224587462548}, 'target': -0.9932407833280448}.
optimizer 3 got 0.6641434385719567 as target.
optimizer 3 will to register next: {'params': {'x': 0.5017384428424876, 'y': 1.2900260271114286}, 'target': 0.6641434385719567}.

optimizer 1 wants to register: {'params': {'x': 0.8503719180120841, 'y': 1.0162062094348336}, 'target': 0.27660495983220357}.
optimizer 1 got 0.5494853411369414 as target.
optimizer 1 will to register next: {'params': {'x': -0.6658390406816954, 'y': 0.9153062530812446}, 'target': 0.5494853411369414}.

optimizer 2 wants to register: {'params': {'x': 1.3206718333240124, 'y': 0.8358155265076721}, 'target': -0.771130632671361}.
optimizer 2 got 0.6648264414004357 as target.
optimizer 2 will to register next: {'params': {'x': 0.4292135631688305, 'y': 1.3885219115976353}, 'target': 0.6648264414004357}.

optimizer 3 wants to register: {'params': {'x': 0.5017384428424876, 'y': 1.2900260271114286}, 'target': 0.6641434385719567}.
optimizer 3 got 0.8238072342547169 as target.
optimizer 3 will to register next: {'params': {'x': -0.40623323182659554, 'y': 0.8943244252203824}, 'target': 0.8238072342547169}.

optimizer 1 wants to register: {'params': {'x': -0.6658390406816954, 'y': 0.9153062530812446}, 'target': 0.5494853411369414}.
optimizer 1 got 0.9985878986141388 as target.
optimizer 1 will to register next: {'params': {'x': 0.03755977189755677, 'y': 1.0011682982772876}, 'target': 0.9985878986141388}.

optimizer 1 is done!

optimizer 1 found a maximum value of: 0.9985878986141388
optimizer 2 wants to register: {'params': {'x': 0.4292135631688305, 'y': 1.3885219115976353}, 'target': 0.6648264414004357}.
optimizer 2 got 0.9990561083666321 as target.
optimizer 2 will to register next: {'params': {'x': 0.029861577428955982, 'y': 1.007223422099141}, 'target': 0.9990561083666321}.

optimizer 2 wants to register: {'params': {'x': 0.029861577428955982, 'y': 1.007223422099141}, 'target': 0.9990561083666321}.
optimizer 2 got 0.9716088970685411 as target.
optimizer 2 will to register next: {'params': {'x': 0.1261146869479921, 'y': 0.8882583843527005}, 'target': 0.9716088970685411}.

optimizer 3 wants to register: {'params': {'x': -0.40623323182659554, 'y': 0.8943244252203824}, 'target': 0.8238072342547169}.
optimizer 3 got 0.9931094324110107 as target.
optimizer 3 will to register next: {'params': {'x': 0.08297444751731618, 'y': 1.002410113728728}, 'target': 0.9931094324110107}.

optimizer 3 is done!

optimizer 3 found a maximum value of: 0.9931094324110107
optimizer 2 wants to register: {'params': {'x': 0.1261146869479921, 'y': 0.8882583843527005}, 'target': 0.9716088970685411}.
optimizer 2 got 0.9976925353945171 as target.
optimizer 2 will to register next: {'params': {'x': -0.048008570740855065, 'y': 0.9983746566814108}, 'target': 0.9976925353945171}.

optimizer 2 wants to register: {'params': {'x': -0.048008570740855065, 'y': 0.9983746566814108}, 'target': 0.9976925353945171}.
optimizer 2 got 0.9934994877069779 as target.
optimizer 2 will to register next: {'params': {'x': 0.041313641166422777, 'y': 1.0692365174354843}, 'target': 0.9934994877069779}.

optimizer 2 is done!

optimizer 2 found a maximum value of: 0.9990561083666321
till-m commented 4 months ago

Is this in reference to #138?

phi-friday commented 4 months ago

Is this in reference to #138?

No, it's not related. I was curious about the example(especially async) and read it, but there was some unsupported code in the latest version(master), so I fixed it.

All I did was change tornado to asyncio and http.server, and UtilityFunction to UpperConfidenceBound.

till-m commented 4 months ago

I honestly never had a close look at the async example in the docs. If I understand correctly, the idea is to run multiple optimizers in parallel? Why would that be advantageous, considering that they're operating on limited information?

In any case, if we want to keep an example like this I think it would make sense to replace the other file with this version. For now, I would like to wait and see if someone can explain whether this make sense conceptually before making a decision.

till-m commented 2 months ago

I'm currently leaning towards deleting the async example from the folder entirely, I'm not sure it's really needed and evidently we're not good at maintaining it. What do you think?

phi-friday commented 2 months ago

I'm currently leaning towards deleting the async example from the folder entirely, I'm not sure it's really needed and evidently we're not good at maintaining it. What do you think?

This PR was not rewritten with a full understanding of BayesianOptimization, it simply fixed the point where the error occurred, so if you think it's right to delete it, I agree. When I was fixing this example, I also felt it was very awkward.