Skip to content

Commit 11b4771

Browse files
authored
fix: Ensure the "center" of CMAES is 1-dimensional (#111)
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.
1 parent dcd66dc commit 11b4771

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/evotorch/algorithms/cmaes.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,13 @@ def __init__(
251251
center_init = center_init.values.clone()
252252

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

256262
# Store the initial step size
257263
self.sigma = self._problem.make_tensor(stdev_init)

0 commit comments

Comments
 (0)