Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

# Compatibility issue between CONTEXT variable and parameter space CONSTRAINT

See original GitHub issue

Hi there!

Python 3.8.8 Emukit 0.4.9

I’ve come across an issue when trying to get_next_points with a context variable for a constrained parameter space. My understanding is that the fact to use context variables passes an array of length n-x (for x context variable) to the constraint function. This function, which is expecting an array of length n, in turns causes it to bug.

Below is an exemple that will cause this issue (modifed from the “Emukit - Bayesian Optimization with Non-Linear Constraints” tutorial)

`FIG_SIZE = (12, 8)

from emukit.test_functions import branin_function fcn, space = branin_function()

import numpy as np constraint_radius = 4 constraint_fcn = lambda x: 10 * (-(x[0] - 3)**2 - (x[1] - 7)**2 + constraint_radius ** 2)

optimum = np.array([[-np.pi, 12.275], [np.pi, 2.275], [9.42478, 2.475]])

# evaluate objective on grid to plot

x_1 = np.linspace(-5, 10, 50) x_2 = np.linspace(0, 15, 51) x_1_grid, x_2_grid = np.meshgrid(x_1, x_2) x_all = np.stack([x_1_grid.flatten(), x_2_grid.flatten()], axis=1) y_all = fcn(x_all) y_reshape = np.reshape(y_all, x_1_grid.shape)

# evaluate constraint to plot

theta_constraint = np.linspace(0, 2*np.pi) x_0_constraint = 3 + np.sin(theta_constraint) * constraint_radius x_1_constraint = 7 + np.cos(theta_constraint) * constraint_radius

import matplotlib.pyplot as plt plt.figure(figsize=FIG_SIZE) plt.contourf(x_1, x_2, y_reshape) plt.title(‘Branin Function’) plt.plot(x_0_constraint, x_1_constraint, linewidth=3, color=‘k’) plt.plot(optimum[:, 0], optimum[:, 1], marker=‘x’, color=‘r’, linestyle=‘’) plt.legend([‘Constraint boundary’, ‘Unconstrained optima’]);

import GPy from emukit.model_wrappers import GPyModelWrapper

x_init = np.array([[0, 7], [1, 9], [6, 8]]) y_init = fcn(x_init)

model = GPy.models.GPRegression(x_init, y_init) emukit_model = GPyModelWrapper(model)

from emukit.core.acquisition import Acquisition from emukit.core.constraints import NonlinearInequalityConstraint from scipy.special import expit # expit is scipy’s sigmoid function

constraints = [NonlinearInequalityConstraint(constraint_fcn, 0, np.inf)] space.constraints = constraints

from emukit.bayesian_optimization.acquisitions import ExpectedImprovement ei = ExpectedImprovement(model)

from emukit.bayesian_optimization.loops import BayesianOptimizationLoop from emukit.core.optimization import GradientAcquisitionOptimizer

# Make BO loop

bo_loop = BayesianOptimizationLoop(space, emukit_model, ei, acquisition_optimizer=acquisition_optimizer)

# append plot_progress function to iteration end event

bo_loop.get_next_points(results=None,context={‘x2’: 3})`

BTW: I love this package! Good work 😃

### Issue Analytics

• State:
• Created 2 years ago

1reaction
apaleyescommented, Nov 11, 2021

@AlbanMor that’s the thing, context variables aren’t available inside constraints at the moment. So the best you can do is to have a global variable that you refer to. In the example:

``````X2_VALUE = 3
...
constraint_fcn = lambda x: 10 * (-(x[0] - 3)**2 - (X2_VALUE - 7)**2 + constraint_radius ** 2)
...
bo_loop.get_next_points(results=None,context={'x2': X2_VALUE})`
``````

0reactions
apaleyescommented, Nov 11, 2021

@ekalosak that is a possibility, but I’d rather explore ways to solve it first. We just need to find a good way of passing full variables in constraints instead of a context-free ones. It is probably not too hard, and I think can be done by context manager.

#### Top Results From Across the Web

Using contexts and variables - 8.0 - Talend Help Center
A context is characterized by parameters. These parameters are mostly context-sensitive variables which will be added to the list of variables for reuse...
API Connect context variables - IBM
Name Description Permission api.catalog.path The path segment that represents this Catalog. Read/write api.endpoint.address The address of the API Gateway endpoint. Read/write api.id The identifier of the...
Thoughts on Context and Capabilities in Rust
One issue that makes Trait s and callbacks annoyingly rigid when it comes to forwards compatibilities is that they often have to choose...
5.6 Variable Types vs.Constraints and Sizes - Stan
Sizes are determined dynamically (at run time) and thus cannot be type-checked statically when the program is compiled. As a result, any conformance...
5 Compatibility and Interoperability - Oracle Help Center
Compatibility and Interoperability Issues Between Release 9.2 and Release ... of OUT parameters that are record variables to be used in expression contexts...

#### Troubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free