novonordisk-research / ProcessOptimizer

A tool to optimize real world problems
https://github.com/novonordisk-research/ProcessOptimizer
Other
69 stars 15 forks source link

Constraint optimizers can't ask for the full list of initial points #254

Open dk-teknologisk-mon opened 5 months ago

dk-teknologisk-mon commented 5 months ago

To reproduce:

from ProcessOptimizer import Optimizer
from ProcessOptimizer.space.constraints import SumEquals

dimensions = [
    (0.0, 1.0),
    (0.0, 1.0),
    (0.0, 1.0),
    ("A", "B", "C"),
]
seed = 42
# Build optimizer
opt = Optimizer(
    dimensions=dimensions,
    lhs=False,
    acq_func="EI",
    n_initial_points=5,
    random_state=seed,
)

# Create relevant constraint
constraints = [SumEquals(dimensions=[0, 1, 2], value=1.5)]
opt.set_constraints(constraints)

# Ask for the first 5 space filling points - this will fail
x = opt.ask(5)

This throws the following error: ValueError: Steinerberger (default setting) sampling can not be used with constraints, try using another strategy like 'opt.ask(n,strategy='cl_min')'

It is caused by line 471 in optimizer.py, which was introduced a couple of months ago:

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')'"
            )

This needs to be changed to skip the check when we are still operating with less than n_initial_points of data.