Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
btjanaka committed Jun 25, 2024
1 parent 7dd8f84 commit 73d04a2
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions ribs/archives/_unstructured_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def index_of(self, measures) -> np.ndarray:
_, indices = self._cur_kd_tree.query(measures)
return indices.astype(np.int32)

# TODO: API for diversity optimization?
# TODO: Update docstring, comment on how objectives are not considered.
def add(self, solution, objective, measures, **fields) -> Union[Any, Dict]:
"""Inserts a batch of solutions into the archive.
Expand Down Expand Up @@ -313,6 +314,11 @@ def add(self, solution, objective, measures, **fields) -> Union[Any, Dict]:
kdt = self._cur_kd_tree

if len(reference_measures) == 0:
novelty = np.full(
len(measures),
np.inf,
dtype=self.dtypes["measures"],
)
eligible = np.ones(len(measures), dtype=bool)
else:
dists, _ = kdt.query(measures, k=k_neighbors)
Expand All @@ -336,8 +342,10 @@ def add(self, solution, objective, measures, **fields) -> Union[Any, Dict]:
# the capacity multiple times. The log2 below indicates how many
# times we would need to double the capacity. We obtain the final
# capacity by raising to a power of 2.
new_capacity = 2**np.ceil(np.log2(new_size / self.capacity))
self._store.resize(new_capacity)
multiplier = 2**int(np.ceil(np.log2(new_size / self.capacity)))
self._store.resize(multiplier * self.capacity)

# todo: non-eligible solutions?

add_info = self._store.add(
np.arange(len(self), new_size),
Expand Down Expand Up @@ -371,6 +379,13 @@ def add(self, solution, objective, measures, **fields) -> Union[Any, Dict]:
if "lower_bounds" in self.__dict__:
del self.__dict__["lower_bounds"]

# TODO: Clean up.
status = np.zeros(len(measures), dtype=np.int32)
status[eligible] = add_info["status"]
add_info["status"] = status

add_info["value"] = novelty

return add_info

def add_single(self, solution, objective, measures,
Expand Down

0 comments on commit 73d04a2

Please sign in to comment.