Skip to content

Commit

Permalink
Raise error messages when ask is called without ask_dqd (#338)
Browse files Browse the repository at this point in the history
## Description

<!-- Provide a brief description of the PR's purpose here. -->

## TODO

<!-- Notable points that this PR has either accomplished or will
accomplish. -->

## Questions

<!-- Any concerns or points of confusion? -->

## Status

- [x] I have read the guidelines in
[CONTRIBUTING.md](https://github.com/icaros-usc/pyribs/blob/master/CONTRIBUTING.md)
- [x] I have formatted my code using `yapf`
- [x] I have tested my code by running `pytest`
- [x] I have linted my code with `pylint`
- [x] I have added a one-line description of my change to the changelog
in `HISTORY.md`
- [x] This PR is ready to go
  • Loading branch information
itsdawei committed Jul 14, 2023
1 parent 7067159 commit bbb1344
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
7 changes: 6 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

### Changelog

#### API

- Raise error messages when `ask` is called without `ask_dqd` (#338)

#### Documentation
- Add BibTex citation for GECCO 2023 (#337)

- Add BibTex citation for GECCO 2023 (#337)

## 0.5.1

Expand Down
13 changes: 12 additions & 1 deletion ribs/emitters/_gradient_arborescence_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,14 @@ def ask(self):
Returns:
(:attr:`batch_size`, :attr:`solution_dim`) array -- a batch of new
solutions to evaluate.
Raises:
RuntimeError: This method was called without first passing gradients
with calls to ask_dqd() and tell_dqd().
"""
if self._jacobian_batch is None:
raise RuntimeError("Please call ask_dqd() and tell_dqd() "
"before calling ask().")

coeff_lower_bounds = np.full(
self._num_coefficients,
-np.inf,
Expand Down Expand Up @@ -382,6 +389,9 @@ def tell(self,
floats represent, refer to :meth:`ribs.archives.add()`.
metadata_batch (array-like): 1d object array containing a metadata
object for each solution.
Raises:
RuntimeError: This method was called without first passing gradients
with calls to ask_dqd() and tell_dqd().
"""
# Preprocessing arguments.
solution_batch = np.asarray(solution_batch)
Expand All @@ -403,7 +413,8 @@ def tell(self,
metadata_batch=metadata_batch)

if self._jacobian_batch is None:
raise RuntimeError("tell() was called without calling tell_dqd().")
raise RuntimeError("Please call ask_dqd(), tell_dqd(), and ask() "
"before calling tell().")

# Increase iteration counter.
self._itrs += 1
Expand Down
22 changes: 22 additions & 0 deletions tests/emitters/gradient_arborescence_emitter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,25 @@ def test_bounds_must_be_none():
normalize_grad=False,
bounds=bound,
batch_size=batch_size)


def test_ask_dqd_must_be_called_before_ask():
archive = GridArchive(solution_dim=1, dims=[10], ranges=[(-1.0, 1.0)])
with pytest.raises(RuntimeError):
emitter = GradientArborescenceEmitter(archive,
x0=np.array([0]),
sigma0=1.0,
lr=1.0)
# Must call ask_dqd() before calling ask() to set the jacobian.
emitter.ask()


def test_tell_dqd_must_be_called_before_tell():
archive = GridArchive(solution_dim=1, dims=[10], ranges=[(-1.0, 1.0)])
with pytest.raises(RuntimeError):
emitter = GradientArborescenceEmitter(archive,
x0=np.array([0]),
sigma0=1.0,
lr=1.0)
# Must call ask_dqd() before calling ask() to set the jacobian.
emitter.tell([[0]], [0], [[0]], [0], [0])

0 comments on commit bbb1344

Please sign in to comment.