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 9.837536 8.007480 0.192434 3.058730 69.493667 9.410153
1 8.530500 19.743483 0.399126 1.367197 45.528765 24.430929
2 5.472229 17.010304 0.815895 1.904619 0.432304 74.364648
3 12.339324 12.908301 3.845904 0.458208 48.163775 22.284487
4 19.852292 2.131286 3.862405 1.035812 70.750908 2.367296
5 13.171085 0.070706 2.974410 0.179814 13.233338 70.370647
6 19.576935 3.759646 1.478154 1.523826 22.971400 50.690039
7 14.213668 4.627964 1.541864 0.327166 59.245583 20.043755
8 12.299511 11.864803 4.570997 0.037630 7.187152 64.039907
9 6.162872 17.339747 0.141482 1.450026 21.727393 53.178480
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 17.845016 3.251164
1 100.0 28.273982 1.766323
2 100.0 22.482534 2.720514
3 100.0 25.247625 4.304112
4 100.0 21.983578 4.898217
5 100.0 13.241791 3.154224
6 100.0 23.336581 3.001980
7 100.0 18.841632 1.869030
8 100.0 24.164313 4.608628
9 100.0 23.502619 1.591508
assert np.allclose(stats["Total"], 100)
assert (stats["Total_Bases"] >= 10).all()
assert (stats["Total_Phase_Agents"] <= 5).all()