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

Logging for multiple constraints to be fixed #478

Closed valentinafarrelli closed 5 months ago

valentinafarrelli commented 5 months ago

Describe the bug Using 'scipy == 1.11.1' with 'bayesian-optimization == 1.4.0' I see that I cannot log if multiple constraints are defined:

def constraint_function_2_dim(x, y):
    return np.array([
        - np.cos(x) * np.cos(y) + np.sin(x) * np.sin(y),
        - np.cos(x) * np.cos(-y) + np.sin(x) * np.sin(-y)]).tolist()

constraint_lower = np.array([-np.inf, -np.inf]).tolist()
constraint_upper = np.array([0., 0.]).tolist()

constraint = NonlinearConstraint(constraint_function_2_dim, constraint_lower, constraint_upper)
optimizer = BayesianOptimization(
    f=target_function,
    constraint=constraint,
    pbounds=pbounds,
    verbose=0, # verbose = 1 prints only when a maximum is observed, verbose = 0 is silent
    random_state=1,
)

logger = JSONLogger(path="./log_multiple_constraints")  # writes logs.json
optimizer.subscribe(Events.OPTIMIZATION_STEP, logger)

optimizer.maximize(
    init_points=2,
    n_iter=10,
)

getting the error: Object of type ndarray is not JSON serializable

We suggest to fix this way: modify target_space.py this way:

def res(self):

        if self._constraint is None:
            params = [dict(zip(self.keys, p)) for p in self.params]

            return [
                {"target": target, "params": param}
                for target, param in zip(self.target, params)
            ]
        else:
            params = [dict(zip(self.keys, p)) for p in self.params]

            return [
                {
                    "target": target,
                    "constraint": constraint_value.tolist(),
                    "params": param,
                    "allowed": allowed
                }
                for target, constraint_value, param, allowed in zip(
                    self.target,
                    self._constraint_values,
                    params,
                    self._constraint.allowed(self._constraint_values)
                )
            ]

(basically, adding "tolist()" to constraint_value in the returned list)

Thanks a lot!

till-m commented 5 months ago

Thanks for reporting this bug @valentinafarrelli. :)

I've pushed a fix. It might take some time to get it merged and a release pushed. If you need help installing from the branch with the patch let me know.

valentinafarrelli commented 5 months ago

Hi, Thanks a lot for the prompt reply! Yes, some help to install from the branch with the patch would be appreciated! Best regards, Valentina

From: till-m @.> Sent: Thursday, June 20, 2024 4:34 PM To: bayesian-optimization/BayesianOptimization @.> Cc: Gori, Valentina @.>; Mention @.> Subject: Re: [bayesian-optimization/BayesianOptimization] Logging for multiple constraints to be fixed (Issue #478)

EXTERNAL EMAIL: This email originated outside of our organization. Do not click on any links or open attachments from unexpected or unknown senders unless you can verify the content is safe.

Thanks for reporting this bug @valentinafarrellihttps://github.com/valentinafarrelli. :)

I've pushed a fix. It might take some time to get it merged and a release pushed. If you need help installing from the branch with the patch let me know.

- Reply to this email directly, view it on GitHubhttps://github.com/bayesian-optimization/BayesianOptimization/issues/478#issuecomment-2180865736, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AVI63WIBJGE57TLHLFSYGWLZILR6NAVCNFSM6AAAAABJTVOPT2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBQHA3DKNZTGY. You are receiving this because you were mentioned.Message ID: @.**@.>>

till-m commented 5 months ago

Assuming that you're using pip, you can just run

pip uninstall bayesian-optimization; pip install git+https://github.com/till-m/BayesianOptimization.git@fix-478

If you're using conda, the easiest way is probably to install pip via conda and then install the package via pip.

valentinafarrelli commented 5 months ago

Thank you!

From: till-m @.> Sent: Thursday, June 20, 2024 4:49 PM To: bayesian-optimization/BayesianOptimization @.> Cc: Gori, Valentina @.>; Mention @.> Subject: Re: [bayesian-optimization/BayesianOptimization] Logging for multiple constraints to be fixed (Issue #478)

EXTERNAL EMAIL: This email originated outside of our organization. Do not click on any links or open attachments from unexpected or unknown senders unless you can verify the content is safe.

Assuming that you're using pip, you can just run

pip uninstall bayesian-optimization; pip install @.***

If you're using conda, the easiest way is probably to install pip via conda and then install the package via pip.

- Reply to this email directly, view it on GitHubhttps://github.com/bayesian-optimization/BayesianOptimization/issues/478#issuecomment-2180899088, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AVI63WOMJR3LNELIQ5MIEPLZILTVTAVCNFSM6AAAAABJTVOPT2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBQHA4TSMBYHA. You are receiving this because you were mentioned.Message ID: @.**@.>>

till-m commented 5 months ago

This fix will be published with the next release :)