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   0.217452  14.391432     2.347612     0.862987  73.166132   9.014385
1   9.229085   1.406121     4.495025     0.034283  24.909720  59.925766
2  11.021948   9.904929     1.568387     2.011141  11.103711  64.389884
3   5.433962   4.961600     1.334881     1.394926  34.927203  51.947428
4   0.773747  10.355730     0.166094     3.189809  78.377525   7.137095
5  16.649873   2.973732     2.639588     1.430022  40.389255  35.917530
6   9.548610  10.473466     2.529448     2.139184  45.367128  29.942164
7   4.064696  10.447398     2.923283     1.343645  70.020947  11.200031
8  15.705649   6.275671     2.343651     2.620916  66.521133   6.532981
9   0.684477  15.390664     1.060405     3.836151  40.009956  39.018348

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    14.608884            3.210599
1  100.0    10.635207            4.529308
2  100.0    20.926876            3.579528
3  100.0    10.395561            2.729807
4  100.0    11.129477            3.355903
5  100.0    19.623604            4.069610
6  100.0    20.022076            4.668631
7  100.0    14.512095            4.266928
8  100.0    21.981320            4.964566
9  100.0    16.075141            4.896555
assert np.allclose(stats["Total"], 100)
assert (stats["Total_Bases"] >= 10).all()
assert (stats["Total_Phase_Agents"] <= 5).all()