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 11.534882 14.218223 0.845752 1.662588 15.031705 56.706851
1 12.028754 13.061593 3.889590 0.831391 30.060928 40.127743
2 11.018261 0.270592 0.015237 0.340667 14.319395 74.035849
3 18.157374 5.613780 3.017416 1.002660 6.461814 65.746956
4 9.513237 11.492129 2.463490 1.572625 67.708761 7.249758
5 9.664683 10.627252 2.664077 0.865758 68.110405 8.067827
6 7.264503 13.047440 0.267366 3.425715 74.370649 1.624326
7 10.121903 6.517026 3.689490 0.036563 32.028783 47.606236
8 11.486718 8.706487 2.087085 1.485669 31.853800 44.380240
9 2.415593 14.304398 3.496223 1.068836 0.312149 78.402802
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 25.753105 2.508340
1 100.0 25.090347 4.720981
2 100.0 11.288853 0.355903
3 100.0 23.771154 4.020076
4 100.0 21.005366 4.036115
5 100.0 20.291934 3.529834
6 100.0 20.311943 3.693081
7 100.0 16.638929 3.726052
8 100.0 20.193206 3.572754
9 100.0 16.719991 4.565058
assert np.allclose(stats["Total"], 100)
assert (stats["Total_Bases"] >= 10).all()
assert (stats["Total_Phase_Agents"] <= 5).all()