Skip to content
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

Improve performance in parallel case #9

Merged
merged 10 commits into from
Jan 19, 2024
30 changes: 22 additions & 8 deletions src/tranquilo/acceptance_decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
from tranquilo.options import AcceptanceOptions


def get_acceptance_decider(acceptance_decider, acceptance_options):
def get_acceptance_decider(
acceptance_decider,
acceptance_options,
):
func_dict = {
"classic": _accept_classic,
"naive_noisy": accept_naive_noisy,
Expand Down Expand Up @@ -92,11 +95,11 @@
state,
history,
*,
speculative_sampling_radius_factor,
wrapped_criterion,
min_improvement,
batch_size,
sample_points,
search_radius_factor,
rng,
):
# ==================================================================================
Expand Down Expand Up @@ -144,11 +147,12 @@
if n_unallocated_evals > 0:
speculative_xs = _generate_speculative_sample(
new_center=candidate_x,
search_radius_factor=search_radius_factor,
radius_factor=speculative_sampling_radius_factor,
trustregion=state.trustregion,
sample_points=sample_points,
n_points=n_unallocated_evals,
history=history,
line_search_xs=line_search_xs,
rng=rng,
)
else:
Expand Down Expand Up @@ -427,7 +431,14 @@


def _generate_speculative_sample(
new_center, trustregion, sample_points, n_points, history, search_radius_factor, rng
new_center,
trustregion,
sample_points,
n_points,
history,
line_search_xs,
radius_factor,
rng,
):
"""Generative a speculative sample.

Expand All @@ -437,23 +448,26 @@
sample_points (callable): Function to sample points.
n_points (int): Number of points to sample.
history (History): Tranquilo history.
search_radius_factor (float): Factor to multiply the trust region radius by to
get the search radius.
radius_factor (float): Factor to multiply the trust region radius by to get the
radius of the region from which to draw the speculative sample.
rng (np.random.Generator): Random number generator.

Returns:
np.ndarray: Speculative sample.

"""
search_region = trustregion._replace(
center=new_center, radius=search_radius_factor * trustregion.radius
center=new_center, radius=radius_factor * trustregion.radius
)

old_indices = history.get_x_indices_in_region(search_region)

old_xs = history.get_xs(old_indices)

model_xs = old_xs
if line_search_xs is not None:
model_xs = np.row_stack([old_xs, line_search_xs])

Check warning on line 468 in src/tranquilo/acceptance_decision.py

View check run for this annotation

Codecov / codecov/patch

src/tranquilo/acceptance_decision.py#L468

Added line #L468 was not covered by tests
else:
model_xs = old_xs

new_xs = sample_points(
search_region,
Expand Down
6 changes: 4 additions & 2 deletions src/tranquilo/filter_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
return xs, indices


def drop_excess(xs, indices, state, target_size):
n_to_drop = max(0, len(xs) - target_size)
def drop_excess(xs, indices, state, target_size, n_max_factor):
filter_target_size = int(np.floor(target_size * n_max_factor))

Check warning on line 54 in src/tranquilo/filter_points.py

View check run for this annotation

Codecov / codecov/patch

src/tranquilo/filter_points.py#L54

Added line #L54 was not covered by tests

n_to_drop = max(0, len(xs) - filter_target_size)

Check warning on line 56 in src/tranquilo/filter_points.py

View check run for this annotation

Codecov / codecov/patch

src/tranquilo/filter_points.py#L56

Added line #L56 was not covered by tests

if n_to_drop:
xs, indices = drop_worst_points(xs, indices, state, n_to_drop)
Expand Down
2 changes: 2 additions & 0 deletions src/tranquilo/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class AcceptanceOptions(NamedTuple):
n_min: int = 4
n_max: int = 50
min_improvement: float = 0.0
speculative_sampling_radius_factor: float = 0.75


class StagnationOptions(NamedTuple):
Expand Down Expand Up @@ -179,6 +180,7 @@ class VarianceEstimatorOptions(NamedTuple):
class FilterOptions(NamedTuple):
strictness: float = 1e-10
shape: str = "sphere"
n_max_factor: int = 3


class SamplerOptions(NamedTuple):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_acceptance_decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def test_generate_speculative_sample():
sample_points=get_sampler("random_hull"),
n_points=3,
history=history,
search_radius_factor=1.0,
radius_factor=1.0,
line_search_xs=None,
rng=np.random.default_rng(1234),
)

Expand Down
Loading