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 10.104661 19.427060 2.241295 0.242669 65.804354 2.179961
1 15.141106 13.945034 2.773874 1.899317 60.957498 5.283170
2 11.730640 3.372892 3.050675 0.525058 35.549480 45.771255
3 10.090107 17.170254 3.644838 0.929718 36.896369 31.268714
4 8.536555 12.755410 1.709659 2.340027 0.923684 73.734665
5 8.784565 19.239996 2.401693 0.589845 21.050586 47.933316
6 19.676049 14.077549 2.487186 0.374218 47.837757 15.547240
7 10.473393 6.902174 0.172693 2.661915 51.990726 27.799098
8 6.073289 19.673697 3.229320 0.091644 62.992953 7.939096
9 19.473845 4.785847 1.978213 2.514094 60.431383 10.816617
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 29.531722 2.483964
1 100.0 29.086141 4.673191
2 100.0 15.103532 3.575733
3 100.0 27.260360 4.574556
4 100.0 21.291966 4.049686
5 100.0 28.024560 2.991538
6 100.0 33.753599 2.861405
7 100.0 17.375567 2.834608
8 100.0 25.746986 3.320965
9 100.0 24.259693 4.492307
assert np.allclose(stats["Total"], 100)
assert (stats["Total_Bases"] >= 10).all()
assert (stats["Total_Phase_Agents"] <= 5).all()