Skip to content

Commit

Permalink
Fix for step == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
not522 committed Oct 18, 2021
1 parent 7f7c143 commit 18a1a28
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 4 additions & 4 deletions optuna/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,25 +502,25 @@ def json_to_distribution(json_str: str) -> BaseDistribution:

if json_dict["type"] == "float":
if log:
if step:
if step is not None:
raise ValueError(
"The parameter `step` is not supported when `log` is true."
)
else:
return LogUniformDistribution(low, high)
else:
if step:
if step is not None:
return DiscreteUniformDistribution(low, high, step)
else:
return UniformDistribution(low, high)
else:
if log:
if step:
if step is not None:
return IntLogUniformDistribution(low, high, step)
else:
return IntLogUniformDistribution(low, high)
else:
if step:
if step is not None:
return IntUniformDistribution(low, high, step)
else:
return IntUniformDistribution(low, high)
Expand Down
17 changes: 14 additions & 3 deletions tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,21 @@ def test_abbreviated_json_to_distribution() -> None:
unknown_json = '{"type": "unknown", "low": 1.0, "high": 2.0}'
pytest.raises(ValueError, lambda: distributions.json_to_distribution(unknown_json))

unsupported_distribution = (
'{"type": "float", "low": 1.0, "high": 9.0, "step": 2.0, "log": true}'
invalid_distribution = (
'{"type": "float", "low": 0.0, "high": -100.0}',
'{"type": "float", "low": 7.3, "high": 7.2, "log": true}',
'{"type": "float", "low": -30.0, "high": -40.0, "step": 3.0}',
'{"type": "float", "low": 1.0, "high": 100.0, "step": 0.0}',
'{"type": "float", "low": 1.0, "high": 100.0, "step": -1.0}',
'{"type": "int", "low": 123, "high": 100}',
'{"type": "int", "low": 123, "high": 100, "step": 2}',
'{"type": "int", "low": 123, "high": 100, "log": true}',
'{"type": "int", "low": 1, "high": 100, "step": 0}',
'{"type": "int", "low": 1, "high": 100, "step": -1}',
'{"type": "categorical", "choices": []}',
)
pytest.raises(ValueError, lambda: distributions.json_to_distribution(unsupported_distribution))
for distribution in invalid_distribution:
pytest.raises(ValueError, lambda: distributions.json_to_distribution(distribution))


def test_backward_compatibility_int_uniform_distribution() -> None:
Expand Down

0 comments on commit 18a1a28

Please sign in to comment.