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 8.309078 3.706616 3.500790 0.524982 59.660281 24.298252
1 7.939510 19.908522 2.666762 2.071766 31.394437 36.019003
2 17.493902 13.277727 3.064160 1.616012 58.747188 5.801012
3 11.501995 13.698737 3.027329 1.602968 39.594953 30.574018
4 17.631381 12.498848 0.165053 0.974796 66.820085 1.909837
5 7.380052 9.732399 3.961521 0.026002 57.494737 21.405289
6 12.938738 8.964099 1.682177 0.817066 63.407423 12.190497
7 13.451324 17.626533 0.205119 1.650050 58.594942 8.472032
8 4.968339 12.203777 0.287920 0.355050 45.350703 36.834210
9 11.602596 17.745640 2.672427 1.448160 59.640909 6.890268
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 12.015695 4.025773
1 100.0 27.848033 4.738528
2 100.0 30.771629 4.680172
3 100.0 25.200732 4.630297
4 100.0 30.130229 1.139849
5 100.0 17.112451 3.987523
6 100.0 21.902836 2.499243
7 100.0 31.077857 1.855169
8 100.0 17.172116 0.642970
9 100.0 29.348236 4.120587
assert np.allclose(stats["Total"], 100)
assert (stats["Total_Bases"] >= 10).all()
assert (stats["Total_Phase_Agents"] <= 5).all()