# 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 7.826150 9.664139 4.111226 0.049114 28.263612 50.085759 1 16.372840 8.665653 1.500823 0.404543 4.688349 68.367793 2 14.764970 6.121903 3.272498 1.254950 52.590565 21.995115 3 17.328610 8.834524 0.017167 4.090511 26.034289 43.694898 4 13.591892 7.805506 1.755549 2.968704 54.546085 19.332264 5 11.080952 11.349343 0.568864 0.702124 69.934693 6.364025 6 3.793684 7.553825 0.850550 1.357266 58.025315 28.419360 7 1.086770 14.860695 2.543335 0.990185 42.413798 38.105217 8 2.679680 8.185367 0.331007 0.891739 78.171939 9.740268 9 6.403760 17.121686 0.116725 2.770738 35.658776 37.928316 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 17.490289 4.160340 1 100.0 25.038493 1.905365 2 100.0 20.886873 4.527447 3 100.0 26.163134 4.107679 4 100.0 21.397398 4.724253 5 100.0 22.430294 1.270988 6 100.0 11.347509 2.207816 7 100.0 15.947466 3.533520 8 100.0 10.865047 1.222746 9 100.0 23.525446 2.887462 ```python assert np.allclose(stats["Total"], 100) assert (stats["Total_Bases"] >= 10).all() assert (stats["Total_Phase_Agents"] <= 5).all() ```