Skip to content

Commit

Permalink
Fix the "center" of CMAES to be 1-dimensional
Browse files Browse the repository at this point in the history
Addressed issue:
#110

During the initialization of CMAES, the center
point of the search was generated not as a vector
of length `n`, but as a tensor of shape `(1, n)`.
Because of this, the reported "center" solution
in the status dictionary also ended up with an
unexpected leftmost dimension with size 1.

This fix introduces a `squeeze()` operation on
the initial center tensor, and also a shape
verification, ensuring that the center tensor is
1-dimensional and has a correct length.
With these changes, the reported "center" in the
status dictionary becomes 1-dimensional.
  • Loading branch information
engintoklu committed Jul 30, 2024
1 parent dcd66dc commit 47a8618
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/evotorch/algorithms/cmaes.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ def __init__(
center_init = center_init.values.clone()

# Store the center
self.m = self._problem.make_tensor(center_init)
self.m = self._problem.make_tensor(center_init).squeeze()
valid_shaped_m = (self.m.ndim == 1) and (len(self.m) == self._problem.solution_length)
if not valid_shaped_m:
raise ValueError(
f"The initial center point was expected as a vector of length {self._problem.solution_length}."
" However, the provided `center_init` has (or implies) a different shape."
)

# Store the initial step size
self.sigma = self._problem.make_tensor(stdev_init)
Expand Down

0 comments on commit 47a8618

Please sign in to comment.