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

Make ArrayStore use int32 indices #400

Merged
merged 5 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
({pr}`397`)
- **Backwards-incompatible:** Rename `measure_*` columns to `measures_*` in
`as_pandas` ({pr}`396`)
- Add ArrayStore data structure ({pr}`395`, {pr}`398`)
- Add ArrayStore data structure ({pr}`395`, {pr}`398`, {pr}`400`)
- Add GradientOperatorEmitter to support OMG-MEGA and OG-MAP-Elites ({pr}`348`)

#### Improvements
Expand Down
8 changes: 4 additions & 4 deletions ribs/archives/_array_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(self, field_desc, capacity):
"capacity": capacity,
"occupied": np.zeros(capacity, dtype=bool),
"n_occupied": 0,
"occupied_list": np.empty(capacity, dtype=int),
"occupied_list": np.empty(capacity, dtype=np.int32),
"updates": np.array([0, 0]),
}

Expand Down Expand Up @@ -168,7 +168,7 @@ def occupied(self):

@property
def occupied_list(self):
"""numpy.ndarray: Integer array listing all occupied indices in the
"""numpy.ndarray: int32 array listing all occupied indices in the
store."""
return readonly(
self._props["occupied_list"][:self._props["n_occupied"]])
Expand Down Expand Up @@ -207,7 +207,7 @@ def retrieve(self, indices, fields=None):
Raises:
ValueError: Invalid field name provided.
"""
indices = np.asarray(indices)
indices = np.asarray(indices, dtype=np.int32)
occupied = readonly(self._props["occupied"][indices])

data = {}
Expand Down Expand Up @@ -363,7 +363,7 @@ def resize(self, capacity):
self._props["occupied"][:cur_capacity] = cur_occupied

cur_occupied_list = self._props["occupied_list"]
self._props["occupied_list"] = np.empty(capacity, dtype=int)
self._props["occupied_list"] = np.empty(capacity, dtype=np.int32)
self._props["occupied_list"][:cur_capacity] = cur_occupied_list

for name, cur_arr in self._fields.items():
Expand Down
24 changes: 23 additions & 1 deletion tests/archives/array_store_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ def test_add_duplicate_indices(store):
assert np.all(store.occupied_list == [3])


def test_dtypes(store):
store.add(
[3, 5],
{
"objective": [1.0, 2.0],
"measures": [[1.0, 2.0], [3.0, 4.0]],
"solution": [np.zeros(10), np.ones(10)],
},
{}, # Empty extra_args.
[], # Empty transforms.
)

_, data = store.retrieve([5, 3])

# Index is always int32, and other fields were defined as float32 in the
# `store` fixture.
assert data["index"].dtype == np.int32
assert data["objective"].dtype == np.float32
assert data["measures"].dtype == np.float32
assert data["solution"].dtype == np.float32


def test_retrieve_duplicate_indices(store):
store.add(
[3],
Expand Down Expand Up @@ -400,7 +422,7 @@ def test_as_pandas(store):
"solution_8",
"solution_9",
]).all()
assert (df.dtypes == [int] + [np.float32] * 13).all()
assert (df.dtypes == [np.int32] + [np.float32] * 13).all()
assert len(df) == 2

row0 = np.concatenate(([3, 1.0, 1.0, 2.0], np.zeros(10)))
Expand Down