From 19063d5b578a32ebbfbdaf0c5cdb626f967ed84f Mon Sep 17 00:00:00 2001 From: Morten Bormann Nielsen Date: Mon, 22 Apr 2024 13:47:33 +0200 Subject: [PATCH 1/4] Allows opt.ask() to get more than 1 point when constraints exist, as long as we are operating on the initial points --- ProcessOptimizer/optimizer/optimizer.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ProcessOptimizer/optimizer/optimizer.py b/ProcessOptimizer/optimizer/optimizer.py index 6d173948..97377af4 100644 --- a/ProcessOptimizer/optimizer/optimizer.py +++ b/ProcessOptimizer/optimizer/optimizer.py @@ -467,11 +467,13 @@ def ask(self, n_points=None, strategy="stbr_fill"): + "got %s" % strategy ) - if strategy in ["stbr_fill", "stbr_full"] and self.get_constraints() is not None: - raise ValueError( - "Steinerberger (default setting) sampling can not be used with constraints,\ - try using another strategy like 'opt.ask(n,strategy='cl_min')'" - ) + if (n_points > self._n_initial_points + and strategy in ["stbr_fill", "stbr_full"] + and self.get_constraints() is not None): + raise ValueError( + "Steinerberger (default setting) sampling can not be used with constraints,\ + try using another strategy like 'opt.ask(n,strategy='cl_min')'" + ) # Caching the result with n_points not None. If some new parameters # are provided to the ask, the cache_ is not used. if (n_points, strategy) in self.cache_: From b78f176ba99e21198e501cb9f5cc9fe801330e8f Mon Sep 17 00:00:00 2001 From: Morten Bormann Nielsen Date: Mon, 22 Apr 2024 15:02:27 +0200 Subject: [PATCH 2/4] Adds tests that we can ask for multiple points when working with less than n_initial_points of data in a constrained space --- ProcessOptimizer/tests/test_constraints.py | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ProcessOptimizer/tests/test_constraints.py b/ProcessOptimizer/tests/test_constraints.py index 648b0978..120aa2cb 100644 --- a/ProcessOptimizer/tests/test_constraints.py +++ b/ProcessOptimizer/tests/test_constraints.py @@ -234,6 +234,31 @@ 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 we can ask for multiple points at a time, as long as we have + # less than n_initial_points of data + space = Space([[0.0, 10.0], [0.0, 10.0]]) + cons = Constraints([SumEquals((0, 1), 5)], space) + opt = Optimizer( + dimensions=space, + lhs=False, + n_initial_points=10, + ) + opt.set_constraints(cons) + assert opt.ask(10) + # We should not be able to ask for 11 + with raises(ValueError): + opt.ask(11) + # Check that we can ask for multiple experiments piecemeal up to + # n_initial_points (here 10), but not after + for i in range(5): + x = opt.ask(2) + y = [2, 2] + opt.tell(x, y) + with raises(ValueError): + opt.ask(2) + + @pytest.mark.fast_test def test_Conditional(): From 0c88b6d770d6c81a85d146a640429a87e8abea4b Mon Sep 17 00:00:00 2001 From: Morten Bormann Nielsen Date: Fri, 9 Aug 2024 14:16:20 +0200 Subject: [PATCH 3/4] Adds a warning about asking for more than one point at a time with the default Steinerberger strategy when constraints are present --- ProcessOptimizer/optimizer/optimizer.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ProcessOptimizer/optimizer/optimizer.py b/ProcessOptimizer/optimizer/optimizer.py index 97377af4..2740748b 100644 --- a/ProcessOptimizer/optimizer/optimizer.py +++ b/ProcessOptimizer/optimizer/optimizer.py @@ -471,9 +471,17 @@ def ask(self, n_points=None, strategy="stbr_fill"): and strategy in ["stbr_fill", "stbr_full"] and self.get_constraints() is not None): raise ValueError( - "Steinerberger (default setting) sampling can not be used with constraints,\ - try using another strategy like 'opt.ask(n,strategy='cl_min')'" + "Steinerberger (default setting) sampling can not be used with constraints, " + + "try using another strategy like 'opt.ask(n,strategy='cl_min')'" ) + elif (strategy in ["stbr_fill", "stbr_full"] + and self.get_constraints() is not None): + warnings.warn( + "Steinerberger (default setting) sampling can not be used with constraints, " + +f"and you will get an error once you ask for more than {self._n_initial_points} new suggestions. " + +"Try using another strategy like 'opt.ask(n,strategy='cl_min')'" + ) + # Caching the result with n_points not None. If some new parameters # are provided to the ask, the cache_ is not used. if (n_points, strategy) in self.cache_: From 1ee80e9e3d0ff96d5340ce88750615e626442fa8 Mon Sep 17 00:00:00 2001 From: Morten Bormann Nielsen Date: Fri, 9 Aug 2024 14:16:37 +0200 Subject: [PATCH 4/4] Updates constraint tests --- ProcessOptimizer/tests/test_constraints.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ProcessOptimizer/tests/test_constraints.py b/ProcessOptimizer/tests/test_constraints.py index 120aa2cb..40a0c89b 100644 --- a/ProcessOptimizer/tests/test_constraints.py +++ b/ProcessOptimizer/tests/test_constraints.py @@ -245,6 +245,7 @@ def test_SumEquals(): n_initial_points=10, ) opt.set_constraints(cons) + # Check that we can ask for 10 points assert opt.ask(10) # We should not be able to ask for 11 with raises(ValueError):