<jupyter_start><jupyter_text>OPTaaS Constraints Note: To run this notebook, you need an API Key. You can get one here.Constraints allow you to specify relationships between the parameters you want to optimize, or just constrain the values that a parameter can take. For example, you may want an optional parameter to always be enabled if another parameter is set to `True`, or you may want an IntParameter to be between 0 and 10, but never 3.  Numeric constraintsYou can use all the familiar numeric and comparison operators.Constraints are automatically converted to string expressions in a format that can be parsed by OPTaaS.<jupyter_code>from mindfoundry.optaas.client.parameter import IntParameter
from mindfoundry.optaas.client.constraint import Constraint

x = IntParameter("x", id="id_x", minimum=0, maximum=20)
y = IntParameter("y", id="id_y", minimum=0, maximum=20)

Constraint(x<y)
Constraint(x+y == 25)
Constraint(x**2 < y%2)<jupyter_output><empty_output><jupyter_text>If you don't specify an `id` for a parameter, the object id will be used instead - but it's not very readable!<jupyter_code>no_id = IntParameter("no_id", minimum=0, maximum=20)

Constraint(x // y <= no_id)<jupyter_output><empty_output><jupyter_text>Logical operatorsUse the binary operators `|` and `&` instead of `or` and `and` (this is because `or` and `and` cannot be overridden).Enclose the operands in brackets to avoid any errors due to the precedence of the binary operators.<jupyter_code>Constraint((x<3) | (x>5) & (y>=7))<jupyter_output><empty_output><jupyter_text>Optional parametersUse `is_present` and `is_absent` to create constraints based on whether a parameter is present in a configuration.<jupyter_code>from mindfoundry.optaas.client.parameter import FloatParameter

z = FloatParameter("z", id="id_z", minimum=0, maximum=1, optional=True)<jupyter_output><empty_output><jupyter_text>Note that you can create conditional constraints that only apply when a condition is true (in this case, if `z` is present).<jupyter_code>Constraint(when=z.is_present(), then=z >= x/y)
Constraint(when=(x==0) & (y==0), then=z.is_absent())<jupyter_output><empty_output><jupyter_text>ChoicesYou can use `is_present` and `is_absent` with parameters that are part of a choice.<jupyter_code>from mindfoundry.optaas.client.parameter import ChoiceParameter

choice = ChoiceParameter("xy", [x, y], id="id_choice", default=y)
Constraint(when=x.is_present(), then=z<0.5)<jupyter_output><empty_output><jupyter_text>You can also use `==` and `!=` with choices:<jupyter_code>Constraint(when=choice==x, then=z<0.5)
Constraint(when=z==1, then=choice!=x)<jupyter_output><empty_output><jupyter_text>CategoricalsYou can use `==`, `!=`, `is_present` and `is_absent` with categoricals as well.<jupyter_code>from mindfoundry.optaas.client.parameter import CategoricalParameter

abc = CategoricalParameter('abc', ['a', 'b', 'c'], id='id_abc')

Constraint(when=abc == "a", then=x == 0)
Constraint(when=x > 1, then=abc != "c")<jupyter_output><empty_output><jupyter_text>Use them when creating a Task<jupyter_code>from mindfoundry.optaas.client.client import OPTaaSClient

client = OPTaaSClient('https://optaas.mindfoundry.ai', '<Your OPTaaS API key>')

task = client.create_task(
    title='My Task With Constraints', 
    parameters=[abc, choice, z],
    constraints=[
        Constraint(when=(x==0) & (y==0), then=z.is_absent()),
        Constraint(when=choice==x, then=z < 0.5),
        Constraint(when=x==1, then=abc != "c")
    ]
)<jupyter_output><empty_output><jupyter_text>All generated configurations will obey the constraints:<jupyter_code>task.generate_configurations(10)<jupyter_output><empty_output><jupyter_text>If a configuration violates a constraint, it will not be accepted:<jupyter_code>task.add_user_defined_configuration({'xy': {'x': 1}, 'z': 0.5, 'abc': 'c'})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12519399896533887, 'fraction_numerical': 0.010605276771857217, 'mean_word_length': 7.022821576763485, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '427323', 'license': 'MIT', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1247, 'n_words': 352, 'path': 'notebooks/python/03. Constraints.ipynb', 'repo_name': 'MindFoundry/optaas-tutorials', 'size': '3866'}