Skip to content

Commit

Permalink
Debug ensemble_designer to handle with Active Trials.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 564466082
  • Loading branch information
qiuyiz authored and copybara-github committed Sep 11, 2023
1 parent 7f5ee12 commit f82144b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vizier/_src/algorithms/ensemble/ensemble_designer.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ def suggest(self, num_suggestions: int) -> Sequence[vz.TrialSuggestion]:
def update(
self, completed: vza.CompletedTrials, all_active: vza.ActiveTrials
) -> None:
# With no completed Trials, simply update with reward calcuations.
if not completed.trials:
for designer in self._designers.values():
designer.update(completed, all_active)
return

if self._reward_generator is None:
rewards = []
for t in completed.trials:
Expand Down
68 changes: 68 additions & 0 deletions vizier/_src/algorithms/ensemble/ensemble_designer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

from __future__ import annotations

import jax.numpy as jnp
from vizier import benchmarks
from vizier import pyvizier as vz
from vizier._src.algorithms.designers import random
from vizier._src.algorithms.designers import scalarization
from vizier._src.algorithms.designers import scalarizing_designer
from vizier._src.algorithms.designers.eagle_strategy import eagle_strategy
from vizier._src.algorithms.ensemble import ensemble_design
from vizier._src.algorithms.ensemble import ensemble_designer
Expand Down Expand Up @@ -96,6 +99,71 @@ def ensemble_designer_factory(config: vz.ProblemStatement, seed: int):
num_eagle_trials += 1
self.assertEqual(num_random_trials + num_eagle_trials, 250)

@parameterized.parameters(
(ensemble_design.EXP3UniformEnsembleDesign),
(ensemble_design.EXP3IXEnsembleDesign),
(
lambda ind: ensemble_design.AdaptiveEnsembleDesign(
indices=ind, max_lengths=[50, 100, 200]
)
),
)
def testPendingMulitobjectiveUpdate(self, ensemble_design_factory):
dim = 2
func1 = experimenters.bbob.Sphere
func2 = experimenters.bbob.Rastrigin
exptr1 = experimenters.NumpyExperimenter(
func1, experimenters.bbob.DefaultBBOBProblemStatement(dim)
)
exptr2 = experimenters.NumpyExperimenter(
func2, experimenters.bbob.DefaultBBOBProblemStatement(dim)
)
exptr = experimenters.MultiObjectiveExperimenter(
{'m1': exptr1, 'm2': exptr2}
)

def ensemble_designer_factory(config: vz.ProblemStatement, seed: int):
random_designer = random.RandomDesigner(config.search_space, seed=seed)

def eagle_designer_factory(ps, seed):
return eagle_strategy.EagleStrategyDesigner(
problem_statement=ps, seed=seed
)

scalarized_eagle = scalarizing_designer.ScalarizingDesigner(
config,
eagle_designer_factory,
scalarizer=scalarization.HyperVolumeScalarization(
weights=jnp.ones(len(config.metric_information))
),
)

reward_generator = ensemble_designer.ObjectiveRewardGenerator(
config, reward_regularization=0.1
)
return ensemble_designer.EnsembleDesigner(
{'random': random_designer, 'eagle': scalarized_eagle},
ensemble_design_factory=ensemble_design_factory,
reward_generator=reward_generator,
)

benchmark_state_factory = benchmarks.DesignerBenchmarkStateFactory(
designer_factory=ensemble_designer_factory,
experimenter=exptr,
)
bench_state = benchmark_state_factory()
runner = benchmarks.BenchmarkRunner(
benchmark_subroutines=[benchmarks.GenerateSuggestions(1)],
num_repeats=5,
)
runner.run(bench_state)
self.assertLen(
bench_state.algorithm.supporter.GetTrials(
status_matches=vz.TrialStatus.ACTIVE
),
5,
)


if __name__ == '__main__':
absltest.main()
6 changes: 6 additions & 0 deletions vizier/_src/benchmarks/analyzers/convergence_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ class ConvergenceCurveConverter:

def convert(self, trials: Sequence[pyvizier.Trial]) -> ConvergenceCurve:
"""Returns ConvergenceCurve with a single curve."""
if not trials:
raise ValueError(f'No trials provided {trials}')

yvals = [np.nan]
xvals = [0]
candidates = []
Expand Down Expand Up @@ -351,6 +354,9 @@ def create_metric_converter(mc):
def convert(self, trials: Sequence[pyvizier.Trial]) -> ConvergenceCurve:
"""Returns ConvergenceCurve with a curve of shape 1 x len(trials)."""
# Returns a len(trials) x number of metrics np.ndarray.
if not trials:
raise ValueError(f'No trials provided {trials}')

metrics = self._converter.to_labels_array(trials)
if self._origin_value is None:
origin = np.nanmin(metrics, axis=0)
Expand Down

0 comments on commit f82144b

Please sign in to comment.