Modeling a Mixture in Traditional Representation¶
When modeling mixtures, we are often faced with a large set of ingredients to choose from. A common way to formalize this type of selection problem is to assign each ingredient its own numerical parameter representing the amount of the ingredient in the mixture. A sum constraint imposed on all parameters then ensures that the total amount of ingredients in the mix is always 100%. In addition, there could be other constraints, for instance, to impose further restrictions on individual subgroups of ingredients. In BayBE’s language, we call this the traditional mixture representation.
In this example, we demonstrate how to create a search space in this representation, using a simple mixture of up to six components, which are divided into three subgroups: solvents, bases and phase agents.
Slot-based Representation
For an alternative way to describe mixtures, see our slot-based representation.
Imports¶
import numpy as np
import pandas as pd
from baybe.constraints import ContinuousLinearConstraint
from baybe.parameters import NumericalContinuousParameter
from baybe.recommenders import RandomRecommender
from baybe.searchspace import SearchSpace
Parameter Setup¶
We start by creating lists containing our substance labels according to their subgroups:
g1 = ["Solvent1", "Solvent2"]
g2 = ["Base1", "Base2"]
g3 = ["PhaseAgent1", "PhaseAgent2"]
Next, we create continuous parameters describing the substance amounts for each group. Here, the maximum amount for each substance depends on its group, i.e. we allow adding more of a solvent compared to a base or a phase agent:
p_g1_amounts = [
NumericalContinuousParameter(name=f"{name}", bounds=(0, 80)) for name in g1
]
p_g2_amounts = [
NumericalContinuousParameter(name=f"{name}", bounds=(0, 20)) for name in g2
]
p_g3_amounts = [
NumericalContinuousParameter(name=f"{name}", bounds=(0, 5)) for name in g3
]
Constraints Setup¶
Now, we set up our constraints. We start with the overall mixture constraint, ensuring the total of all ingredients is 100%:
c_total_sum = ContinuousLinearConstraint(
parameters=g1 + g2 + g3,
operator="=",
coefficients=(1,) * len(g1 + g2 + g3),
rhs=100,
)
Additionally, we require bases make up at least 10% of the mixture:
c_g2_min = ContinuousLinearConstraint(
parameters=g2,
operator=">=",
coefficients=(1,) * len(g2),
rhs=10,
)
By contrast, phase agents should make up no more than 5%:
c_g3_max = ContinuousLinearConstraint(
parameters=g3,
operator="<=",
coefficients=(1,) * len(g3),
rhs=5,
)
Search Space Creation¶
Having both parameter and constraint definitions at hand, we can create our search space:
searchspace = SearchSpace.from_product(
parameters=[*p_g1_amounts, *p_g2_amounts, *p_g3_amounts],
constraints=[c_total_sum, c_g2_min, c_g3_max],
)
Verification of Constraints¶
To verify that the constraints imposed above are fulfilled, let us draw some random points from the search space:
recommendations = RandomRecommender().recommend(batch_size=10, searchspace=searchspace)
print(recommendations)
Base1 Base2 PhaseAgent1 PhaseAgent2 Solvent1 Solvent2
0 6.261535 14.714485 1.810055 1.188253 7.478261 68.547411
1 2.840960 14.049600 0.711420 2.542172 72.727668 7.128181
2 4.871185 7.500217 2.985567 0.936355 71.517142 12.189534
3 9.343429 7.385013 0.849096 3.215430 44.598672 34.608359
4 3.513514 8.312730 3.392068 0.113211 34.434178 50.234300
5 8.575406 10.779183 0.231547 0.980025 10.825913 68.607925
6 10.152319 17.071793 0.040427 4.901453 22.980152 44.853857
7 10.315086 9.580879 3.340331 0.757545 47.950825 28.055334
8 15.728454 10.873805 2.360362 0.630722 11.303118 59.103538
9 5.307898 16.347221 2.938837 1.242727 51.309268 22.854048
Computing the respective row sums reveals the expected result:
stats = pd.DataFrame(
{
"Total": recommendations.sum(axis=1),
"Total_Bases": recommendations[g2].sum(axis=1),
"Total_Phase_Agents": recommendations[g3].sum(axis=1),
}
)
print(stats)
Total Total_Bases Total_Phase_Agents
0 100.0 20.976019 2.998308
1 100.0 16.890560 3.253591
2 100.0 12.371402 3.921922
3 100.0 16.728442 4.064526
4 100.0 11.826244 3.505278
5 100.0 19.354590 1.211572
6 100.0 27.224111 4.941881
7 100.0 19.895965 4.097877
8 100.0 26.602259 2.991084
9 100.0 21.655119 4.181564
assert np.allclose(stats["Total"], 100)
assert (stats["Total_Bases"] >= 10).all()
assert (stats["Total_Phase_Agents"] <= 5).all()