Skip to content

Typed Optimization #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cf963b1
WIP
till-m Nov 9, 2022
81321f3
Add ML example
till-m Nov 9, 2022
4106850
Save for merge
till-m May 23, 2023
5a3f2de
Merge remote-tracking branch 'origin/master' into parameter-types
till-m May 23, 2023
ac7f253
Merge remote-tracking branch 'origin/master' into parameter-types
till-m May 25, 2023
0ff88fc
Merge branch 'master' into parameter-types
till-m Oct 1, 2024
5d34efa
Update
till-m Oct 6, 2024
2b64ff0
Parameter types more (#13)
phi-friday Oct 9, 2024
3920e0f
Use `.masks` not `._masks`
till-m Oct 9, 2024
241e5c7
User `super` to call kernel
till-m Oct 9, 2024
68909ad
Update logging for parameters
till-m Oct 12, 2024
1a03b05
Disable SDR when non-float parameters are present
till-m Oct 12, 2024
f17c96a
Add demo script for typed optimization
till-m Oct 12, 2024
3c4c298
Update parameters, testing
till-m Oct 15, 2024
264b79e
Remove sorting, gradient optimize only continuous params
till-m Oct 29, 2024
b97c11e
Go back to `wrap_kernel`
till-m Oct 29, 2024
9543fb8
Update code
till-m Oct 30, 2024
7c84390
Remove `tqdm` dependency, use EI acq
till-m Nov 1, 2024
f1e4493
Add more text to typed optimization notebook.
till-m Nov 1, 2024
b765b5d
Merge branch 'master' into parameter-types
till-m Nov 1, 2024
187fd08
Save files while moving device
till-m Nov 15, 2024
31223a9
Update with custom parameter type example
till-m Dec 10, 2024
4476271
Merge branch 'master' into parameter-types
till-m Dec 18, 2024
9b1fbc1
Mention that parameters are not sorted
till-m Dec 18, 2024
1a54e1b
Change array reg warning
till-m Dec 18, 2024
05fbbcd
Update Citations, parameter notebook
till-m Dec 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/master' into parameter-types
  • Loading branch information
till-m committed May 23, 2023
commit 5a3f2de81ee314822b0e6d47325e406e0c3327cb
2 changes: 1 addition & 1 deletion bayes_opt/bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def suggest(self, utility_function):
suggestion = acq_max(ac=utility_function.utility,
gp=self._gp,
constraint=self.constraint,
y_max=self._space.target.max(),
y_max=self._space._target_max(),
bounds=self._space.float_bounds,
random_state=self._random_state)
return self._space.array_to_params(suggestion)
Expand Down
93 changes: 46 additions & 47 deletions bayes_opt/target_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .util import ensure_rng, NotUniqueError
from .parameter import FloatParameter, IntParameter, is_numeric, CategoricalParameter
from .constraint import ConstraintModel

from .util import Colours

def _hashable(x):
""" ensure that an point is hashable by a python dict """
Expand All @@ -25,11 +25,8 @@ class TargetSpace(object):
>>> assert self.max_point()['max_val'] == y
"""

def __init__(self,
target_func,
pbounds,
constraint=None,
random_state=None):
def __init__(self, target_func, pbounds, constraint=None, random_state=None,
allow_duplicate_points=False):
"""
Parameters
----------
Expand Down Expand Up @@ -70,6 +67,8 @@ def __init__(self,
# keep track of unique points we have seen so far
self._cache = {}

self._constraint = constraint

if constraint is not None:
self._constraint = ConstraintModel(constraint.fun,
constraint.lb,
Expand Down Expand Up @@ -260,7 +259,13 @@ def register(self, params, target, constraint_value=None):
x = self.params_to_array(params)

if x in self:
raise NotUniqueError('Data point {} is not unique'.format(x))
if self._allow_duplicate_points:
self.n_duplicate_points = self.n_duplicate_points + 1
print(f'{Colours.RED}Data point {x} is not unique. {self.n_duplicate_points} duplicates registered.'
f' Continuing ...{Colours.END}')
else:
raise NotUniqueError(f'Data point {x} is not unique. You can set "allow_duplicate_points=True" to '
f'avoid this error')

self._params = np.concatenate([self._params, x.reshape(1, -1)])
self._target = np.concatenate([self._target, np.atleast_1d(target)])
Expand All @@ -270,9 +275,8 @@ def register(self, params, target, constraint_value=None):
self._cache[_hashable(x.ravel())] = target
else:
if constraint_value is None:
msg = (
"When registering a point to a constrained TargetSpace" +
" a constraint value needs to be present.")
msg = ("When registering a point to a constrained TargetSpace" +
" a constraint value needs to be present.")
raise ValueError(msg)
# Insert data into unique dictionary
self._cache[_hashable(x.ravel())] = (target, constraint_value)
Expand Down Expand Up @@ -305,7 +309,6 @@ def probe(self, params):
else:
assert type(params) == dict
x = self.params_to_array(params)

try:
return self._cache[_hashable(x)]
except KeyError:
Expand Down Expand Up @@ -363,33 +366,22 @@ def max(self):

If there is a constraint present, the maximum value that fulfills the
constraint is returned."""
if self._constraint is None:
try:
res = {
'target':
self.target.max(),
'params':
self.array_to_params(self.params[self.target.argmax()])
}
except ValueError:
res = {}
return res
else:
allowed = self._constraint.allowed(self._constraint_values)
if allowed.any():
# Getting of all points that fulfill the constraints, find the
# one with the maximum value for the target function.
sorted = np.argsort(self.target)
idx = sorted[allowed[sorted]][-1]
# there must be a better way to do this, right?
res = {
'target': self.target[idx],
'params': dict(zip(self.keys, self.params[idx])),
'constraint': self._constraint_values[idx]
}
else:
res = {'target': None, 'params': None, 'constraint': None}
return res
target_max = self._target_max()

if target_max is None:
return None

target_max_idx = np.where(self.target == target_max)[0][0]

res = {
'target': target_max,
'params': self.array_to_params(self.params[self.target.argmax()])
}

if self._constraint is not None:
res['constraint'] = self._constraint_values[target_max_idx]

return res

def res(self):
"""Get all target values and constraint fulfillment for all parameters.
Expand All @@ -402,15 +394,22 @@ def res(self):
"params": param
} for target, param in zip(self.target, params)]
else:
params = params = [self.array_to_params(p) for p in self.params]
return [{
"target": target,
"constraint": constraint_value,
"params": param,
"allowed": allowed
} for target, constraint_value, param, allowed in zip(
self.target, self._constraint_values, params,
self._constraint.allowed(self._constraint_values))]
params = [self.array_to_params(p) for p in self.params]

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

def set_bounds(self, new_bounds):
"""
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.