# 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 12.158280 2.424926 4.065169 0.075817 12.246850 69.028958 1 18.940268 11.641766 1.109448 3.829272 9.583285 54.895960 2 0.724021 10.144032 0.243514 3.594519 38.685346 46.608568 3 16.141489 6.387647 0.479003 1.356669 18.462448 57.172743 4 14.249560 12.050866 3.599255 0.834637 50.847926 18.417755 5 15.844264 13.953622 1.537541 2.434133 19.212167 47.018272 6 6.841373 18.946671 2.232447 1.806170 33.187585 36.985754 7 7.520816 11.305938 2.319142 0.119671 21.028364 57.706069 8 6.881406 4.323141 1.788458 2.729690 62.082466 22.194839 9 17.673949 5.896106 0.799980 3.900362 37.819294 33.910309 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 14.583206 4.140986 1 100.0 30.582034 4.938720 2 100.0 10.868053 3.838033 3 100.0 22.529137 1.835672 4 100.0 26.300427 4.433892 5 100.0 29.797886 3.971674 6 100.0 25.788043 4.038617 7 100.0 18.826754 2.438813 8 100.0 11.204547 4.518148 9 100.0 23.570055 4.700342 ```python assert np.allclose(stats["Total"], 100) assert (stats["Total_Bases"] >= 10).all() assert (stats["Total_Phase_Agents"] <= 5).all() ```