Skip to content

Commit 0884e25

Browse files
AdrianSosicScienfitz
authored andcommitted
Simplify from_simplex loop by seeding with empty configuration
1 parent 8b72c5c commit 0884e25

1 file changed

Lines changed: 12 additions & 33 deletions

File tree

baybe/searchspace/discrete.py

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -430,45 +430,24 @@ def from_simplex(
430430
# broadcasting to compute the validity mask in 2D (n_old, n_new) and only
431431
# materialize the surviving combinations. This avoids allocating large
432432
# intermediate arrays that are mostly discarded.
433-
arr: np.ndarray
434-
partial_sums: np.ndarray
435-
nz_counts: np.ndarray
436-
for i, (
437-
param,
438-
min_sum_to_go,
439-
min_nonzero_to_go,
440-
max_nonzero_to_go,
441-
) in enumerate(
442-
zip(
443-
simplex_parameters,
444-
np.append(min_sum_upcoming, 0),
445-
np.append(min_nonzero_upcoming, 0),
446-
np.append(max_nonzero_upcoming, 0),
447-
)
433+
arr = np.empty((1, 0), dtype=active_settings.DTypeFloatNumpy)
434+
partial_sums = np.zeros(1, dtype=active_settings.DTypeFloatNumpy)
435+
nz_counts = np.zeros(1, dtype=np.intp)
436+
437+
for coeff, param, min_sum_to_go, min_nonzero_to_go, max_nonzero_to_go in zip(
438+
coeffs,
439+
simplex_parameters,
440+
np.append(min_sum_upcoming, 0.0),
441+
np.append(min_nonzero_upcoming, 0),
442+
np.append(max_nonzero_upcoming, 0),
448443
):
449444
values = np.asarray(param.values, dtype=active_settings.DTypeFloatNumpy)
450445
threshold = (max_sum - min_sum_to_go) + tolerance
451446
effective_min = min_nonzero - max_nonzero_to_go
452447
effective_max = max_nonzero - min_nonzero_to_go
453448

454-
if i == 0:
455-
partial_sums = values * coeffs[0]
456-
nz_counts = (values != 0.0).astype(np.intp)
457-
458-
# Apply constraints directly on first parameter
459-
mask = partial_sums <= threshold
460-
if effective_min > 0:
461-
mask &= nz_counts >= effective_min
462-
if effective_max < len(simplex_parameters):
463-
mask &= nz_counts <= effective_max
464-
465-
arr = values[mask].reshape(-1, 1)
466-
partial_sums = partial_sums[mask]
467-
nz_counts = nz_counts[mask]
468-
continue
469-
470449
# Compute weighted sums via broadcasting: (n_old, n_new)
471-
new_contributions = values * coeffs[i]
450+
new_contributions = values * coeff
472451
total_sums = partial_sums[:, None] + new_contributions[None, :]
473452

474453
# Build 2D validity mask from sum constraint
@@ -484,7 +463,7 @@ def from_simplex(
484463

485464
# Extract surviving indices and materialize only those rows
486465
old_idx, new_idx = np.where(mask_2d)
487-
arr = np.column_stack([arr[old_idx], values[new_idx].reshape(-1, 1)])
466+
arr = np.column_stack([arr[old_idx], values[new_idx]])
488467
partial_sums = total_sums[old_idx, new_idx]
489468
nz_counts = total_nz[old_idx, new_idx]
490469

0 commit comments

Comments
 (0)