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

Split Array and Float structures #1036

Merged
merged 5 commits into from
Feb 9, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- :code:`MultiobjectiveFunction` does not exist anymore [#1034](https://github.com/facebookresearch/nevergrad/pull/1034).
- the new `nevergrad.errors` module gathers errors and warnings used throughout the package (WIP) [#1031](https://github.com/facebookresearch/nevergrad/pull/1031).
- `Parameter` classes are undergoing heavy changes ([#1029](https://github.com/facebookresearch/nevergrad/pull/1029) [#1036](https://github.com/facebookresearch/nevergrad/pull/1036) and more to come), please open an issue if you encounter any problem. The midterm aim is to allow for simpler constraint management.

## 0.4.3 (2021-01-28)

Expand Down
1 change: 1 addition & 0 deletions nevergrad/common/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
PathLike = Union[str, Path]
FloatLoss = float
Loss = Union[float, ArrayLike]
BoundValue = Optional[Union[float, int, _np.int, _np.float, _np.ndarray]]


# %% Protocol definitions for executor typing
Expand Down
4 changes: 2 additions & 2 deletions nevergrad/functions/test_functionlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ def test_far_optimum_function(independent_sigma: bool, mutable_sigma: bool) -> N
param = func.parametrization.spawn_child()
assert isinstance(param, p.Array)
assert isinstance(param.sigma, p.Array) == mutable_sigma
assert param.sigma.value.size == (1 + independent_sigma)
assert param.sigma.value.size == (1 + independent_sigma) # type: ignore
param.mutate()
new_val = param.sigma.value
assert bool(np.sum(np.abs(new_val - func.parametrization.sigma.value))) == mutable_sigma # type: ignore
if independent_sigma and mutable_sigma:
assert new_val[0] != new_val[1]
assert new_val[0] != new_val[1] # type: ignore


def test_far_optimum_function_cases() -> None:
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/optimization/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __call__(self, optimizer: base.Optimizer, candidate: p.Parameter, loss: tp.F
if inspect.ismethod(val):
val = repr(val.__self__) # show mutation class
data[name if name else "0"] = val.tolist() if isinstance(val, np.ndarray) else val
if isinstance(param, p.Array):
if isinstance(param, p.Data):
val = param.sigma.value
data[(name if name else "0") + "#sigma"] = (
val.tolist() if isinstance(val, np.ndarray) else val
Expand Down
8 changes: 4 additions & 4 deletions nevergrad/optimization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,17 +380,17 @@ def _warn(self) -> None:
)

@classmethod
def list_arrays(cls, parameter: p.Parameter) -> tp.List[p.Array]:
"""Computes a list of data (Array) parameters in the same order as in
def list_arrays(cls, parameter: p.Parameter) -> tp.List[p.Data]:
"""Computes a list of Data (Array/Scalar) parameters in the same order as in
the standardized data space.
"""
if isinstance(parameter, p.Array):
if isinstance(parameter, p.Data):
return [parameter]
elif isinstance(parameter, p.Constant):
return []
if not isinstance(parameter, p.Container):
raise RuntimeError(f"Unsupported parameter {parameter}")
output: tp.List[p.Array] = []
output: tp.List[p.Data] = []
for _, subpar in sorted(parameter._content.items()):
output += cls.list_arrays(subpar)
return output
Expand Down
26 changes: 15 additions & 11 deletions nevergrad/parametrization/choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def as_tag(cls, param: core.Parameter) -> "ChoiceTag":
return cls(type(param), arity)


class BaseChoice(core.Dict):
class BaseChoice(core.Container):

ChoiceTag = ChoiceTag

Expand All @@ -43,8 +43,7 @@ def __init__(
) -> None:
assert repetitions is None or isinstance(repetitions, int) # avoid silent issues
self._repetitions = repetitions
assert not isinstance(choices, Tuple)
lchoices = list(choices) # for iterables
lchoices = list(choices) # unroll iterables (includig Tuple instances
if not lchoices:
raise ValueError("{self._class__.__name__} received an empty list of options.")
super().__init__(choices=Tuple(*lchoices), **kwargs)
Expand All @@ -61,6 +60,14 @@ def __len__(self) -> int:
"""Number of choices"""
return len(self.choices)

def _get_parameters_str(self) -> str:
params = sorted(
(k, p.name)
for k, p in self._content.items()
if p.name != self._ignore_in_repr.get(k, "#ignoredrepr#")
)
return ",".join(f"{k}={n}" for k, n in params)

@property
def index(self) -> int: # delayed choice
"""Index of the chosen option"""
Expand Down Expand Up @@ -97,11 +104,9 @@ def _find_and_set_value(self, values: tp.List[tp.Any]) -> np.ndarray:
values = [values] if self._repetitions is None else values
self._check_frozen()
indices: np.ndarray = -1 * np.ones(len(values), dtype=int)
nums = sorted(int(k) for k in self.choices._content)
# try to find where to put this
for i, value in enumerate(values):
for k in nums:
choice = self.choices[k]
for k, choice in enumerate(self.choices):
try:
choice.value = value
indices[i] = k
Expand Down Expand Up @@ -162,7 +167,6 @@ def __init__(
repetitions: tp.Optional[int] = None,
deterministic: bool = False,
) -> None:
assert not isinstance(choices, Tuple)
lchoices = list(choices)
rep = 1 if repetitions is None else repetitions
super().__init__(
Expand Down Expand Up @@ -233,9 +237,10 @@ def mutate(self) -> None:
self.choices[ind].mutate()

def _internal_spawn_child(self: C) -> C:
choices = (y for x, y in sorted(self.choices.spawn_child()._content.items()))
child = self.__class__(
choices=choices, deterministic=self._deterministic, repetitions=self._repetitions
choices=self.choices.spawn_child(),
deterministic=self._deterministic,
repetitions=self._repetitions,
)
child._content["weights"] = self.weights.spawn_child()
return child
Expand Down Expand Up @@ -330,8 +335,7 @@ def mutate(self) -> None:
self.choices[ind].mutate()

def _internal_spawn_child(self: T) -> T:
choices = (y for x, y in sorted(self.choices.spawn_child()._content.items()))
child = self.__class__(choices=choices, repetitions=self._repetitions)
child = self.__class__(choices=self.choices.spawn_child(), repetitions=self._repetitions)
child._content["positions"] = self.positions.spawn_child()
child._content["transitions"] = self.transitions.spawn_child()
return child
Loading