# 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. ```{admonition} Slot-based Representation :class: seealso For an alternative way to describe mixtures, see our [slot-based representation](/examples/Mixtures/slot_based.md). ``` ## Imports ```python import numpy as np import pandas as pd ``` ```python 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: ```python 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: ```python 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%: ```python 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: ```python c_g2_min = ContinuousLinearConstraint( parameters=g2, operator=">=", coefficients=(1,) * len(g2), rhs=10, ) ``` By contrast, phase agents should make up no more than 5%: ```python 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: ```python 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: ```python recommendations = RandomRecommender().recommend(batch_size=10, searchspace=searchspace) print(recommendations) ``` Base1 Base2 PhaseAgent1 PhaseAgent2 Solvent1 Solvent2 0 19.542717 5.019270 0.182286 1.960549 36.602915 36.692262 1 15.485634 6.128595 2.576529 0.502368 42.495808 32.811065 2 14.354389 15.141054 3.666410 0.756397 53.813552 12.268198 3 4.352097 9.300732 0.888062 3.569103 70.891517 10.998489 4 18.032785 5.759935 1.163208 0.920006 29.265800 44.858265 5 15.269953 3.507303 1.236494 3.225151 1.084534 75.676565 6 8.362881 8.620476 0.775989 1.667507 38.549566 42.023581 7 9.957281 0.433617 1.875889 2.777426 61.549724 23.406062 8 19.084488 7.147289 2.699324 0.043361 31.910442 39.115096 9 9.187469 8.453864 0.318665 0.281254 55.863131 25.895617 Computing the respective row sums reveals the expected result: ```python 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 24.561988 2.142835 1 100.0 21.614229 3.078898 2 100.0 29.495443 4.422807 3 100.0 13.652829 4.457165 4 100.0 23.792721 2.083214 5 100.0 18.777256 4.461645 6 100.0 16.983358 2.443496 7 100.0 10.390898 4.653315 8 100.0 26.231777 2.742685 9 100.0 17.641334 0.599919 ```python assert np.allclose(stats["Total"], 100) assert (stats["Total_Bases"] >= 10).all() assert (stats["Total_Phase_Agents"] <= 5).all() ```