Set outcome constraints#

This guide will show you how to set outcome constraints. These indicate preferences for specific objectives or metrics to satsify some condition during optimization.

A surrogate model is built per-constraint to approximate violation of the constraint, so this is considered a soft constraint.

For guaranteed constraints, you will have to constrain the DOFs directly using DOF constraints.

For more information, check out these references:

Create DOFs and multiple objectives#

from blop.ax import RangeDOF, Objective

motor_x = MovableSignal(name="motor_x")
motor_y = MovableSignal(name="motor_y")
motor_z = MovableSignal(name="motor_z")


dofs = [
    RangeDOF(movable=motor_x, bounds=(0, 1000), parameter_type="float"),
    RangeDOF(movable=motor_y, bounds=(0, 1000), parameter_type="float"),
    RangeDOF(movable=motor_z, bounds=(0, 1000), parameter_type="float"),
]

objectives = [
    Objective(name="objective1", minimize=False),
    Objective(name="objective2", minimize=False),
]

def evaluation_function(uid: str, suggestions: list[dict]) -> list[dict]:
    """Replace this with your own evaluation function."""
    outcomes = []
    for suggestion in suggestions:
        outcome = {
            "_id": suggestion["_id"],
            "objective1": 0.1,
            "objective2": 0.2,
        }
        outcomes.append(outcome)
    return outcomes

Set an objective threshold#

Since this is a multi-objective optimization, we can set a preference for the first objective to be greater than or equal to 0.5.

from blop.ax import OutcomeConstraint

constraint = OutcomeConstraint("x >= 0.5", x=objectives[0])

Configure an agent with outcome constraints#

from blop.ax import Agent

agent = Agent(
    readables=[],
    dofs=dofs,
    objectives=objectives,
    evaluation=evaluation_function,
    outcome_constraints=[constraint],
)