Skip to content

Commit

Permalink
Merge pull request #282 from novonordisk-research/281-sum_constraints…
Browse files Browse the repository at this point in the history
…-with-later-dimensions

Fixes sum constraints with later dimensions
  • Loading branch information
sqbl authored Nov 6, 2024
2 parents 092cf4d + 713dfc1 commit aa36a79
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

### Bugfixes

-
- Fixed a bug where sum_equals constraints would break if used with dimensions other than
an increasing list from 0. E.g. constraining dimensions [0, 1, 2] would work, but
constraining [1, 2, 3] would not.


## Version 1.0.1 (October 2024)

Expand Down
6 changes: 3 additions & 3 deletions ProcessOptimizer/space/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ def phi(d):
sample_candidate = sample_candidate[0]
# Check that the candidate is inside the original parameter space
inspace = [
(sample_candidate[i] >= self.space.bounds[i][0]) and
(sample_candidate[i] <= self.space.bounds[i][1])
for i in range(d)
(sample_candidate[i] >= self.space.bounds[dim][0]) and
(sample_candidate[i] <= self.space.bounds[dim][1])
for i, dim in enumerate(self.sum_equals[0].dimensions)
]
# Only accept the candidate if it is in our space
if all(inspace):
Expand Down
42 changes: 42 additions & 0 deletions ProcessOptimizer/tests/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,48 @@ def test_SumEquals():
# Check that the other dimensions have correct type
assert isinstance(samples[0][3], str)
assert not isinstance(samples[0][2], str)

# Test that the constraint works, irrespective of which dimensions we use
dimensions = [
(200.0, 450.0),
(50.0, 450.0),
(0.0, 450.0),
(10.0, 60.0),
]
# Build optimizer
opt = Optimizer(
dimensions=dimensions,
lhs=False,
acq_func="EI",
n_initial_points=5,
random_state=42,
)
# Constrain the first three dimensions, and leave out the fourth
constraints = [SumEquals(dimensions=[0, 1, 2], value=450)]
opt.set_constraints(constraints)
x1 = opt.ask(1)
# Change the order of the dimensions
dimensions = [
(10.0, 60.0),
(200.0, 450.0),
(50.0, 450.0),
(0.0, 450.0),
]
# Build optimizer
opt = Optimizer(
dimensions=dimensions,
lhs=False,
acq_func="EI",
n_initial_points=5,
random_state=42,
)
# Constrain the same dimensions, that are now second to fourth
constraints = [SumEquals(dimensions=[1, 2, 3], value=450)]
opt.set_constraints(constraints)
x2 = opt.ask()
# The values suggested for the constrained dimensions should be the same
# irrespective of their order in the dimension list
assert x1[:3] == x2[1:]

@pytest.mark.fast_test
def test_Conditional():
Expand Down

0 comments on commit aa36a79

Please sign in to comment.