Skip to content

This feature is currently in Preview.

relationalai.experimental.solvers.iff()

Signature
iff(left: SolverExpression, right: SolverExpression) -> SolverExpression

Returns a solver expression representing a logical biconditional: left is true if and only if right is true. Both left and right must be binary-valued solver expressions, such as binary variables or logical comparisons. Must be called in a Model.rule() or solvers.operators() context.

NameTypeDescription
leftSolverExpressionOne side of the biconditional. Must evaluate to a binary value (True/False or 1/0), such as a binary variable or a logical comparison.
rightSolverExpressionThe other side of the biconditional. Must also evaluate to a binary value.

A SolverExpression object.

Use iff() with the SolverModel.constraint() method to enforce that two expressions are logically equivalent:

import relationalai as rai
from relationalai.experimental import solvers
# Create a RAI model.
model = rai.Model("WeeklyShiftAssignment")
# Declare entity types and properties.
# Entity types.
Employee = model.Type("Employee")
Shift = model.Type("Shift")
Day = model.Type("Day")
Available = model.Type("Available")
Scenario = model.Type("Scenario") # All possible employee–shift–day combinations.
Assignment = model.Type("Assignment") # A Scenario that is assigned.
# Properties.
Employee.name.declare()
Shift.name.declare()
Shift.capacity.declare()
Available.employee.declare()
Available.day.declare()
Scenario.employee.declare()
Scenario.shift.declare()
Scenario.day.declare()
38 collapsed lines
# Define sample data.
# Employees.
with model.rule(dynamic=True):
for name in ["Alice", "Bob", "Carol", "Dave", "Eve"]:
Employee.add(name=name)
# Shifts.
with model.rule():
Shift.add(name="Morning").set(capacity=2)
Shift.add(name="Evening").set(capacity=3)
# Days of the week.
with model.rule(dynamic=True):
for name in ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]:
Day.add(name=name)
# Employee-day availability.
with model.rule(dynamic=True):
# Alice works weekdays only
for name in ["Mon", "Tue", "Wed", "Thu", "Fri"]:
Available.add(employee=Employee(name="Alice"), day=Day(name=name))
# Bob works all days
Available.add(employee=Employee(name="Bob"), day=Day())
# Carol works weekends only
for name in ["Sat", "Sun"]:
Available.add(employee=Employee(name="Carol"), day=Day(name=name))
# Dave works Mon/Wed/Fri
for name in ["Mon", "Wed", "Fri"]:
Available.add(employee=Employee(name="Dave"), day=Day(name=name))
# Eve works Tue/Thu/Sat
for name in ["Tue", "Thu", "Sat"]:
Available.add(employee=Employee(name="Eve"), day=Day(name=name))
# All possible employee–shift–day combinations
with model.rule():
Scenario.add(employee=Employee(), shift=Shift(), day=Day())
# Create a SolverModel instance from the model.
solver_model = solvers.SolverModel(model)
# Define solver variables.
# Scenario assignment: binary variable for each employee–shift–day combination.
with model.rule():
scenario = Scenario()
solver_model.variable(
scenario,
type="zero_one",
name_args=["assigned", scenario.employee.name, scenario.day.name, scenario.shift.name],
)
# Define solver constraints.
# Bob works Monday morning if and only if Alice does not.
with solvers.operators():
morning, monday = Shift(name="Morning"), Day(name="Mon")
alice_works = Scenario(employee=Employee(name="Alice"), shift=morning, day=monday)
bob_works = Scenario(employee=Employee(name="Bob"), shift=morning, day=monday)
solver_model.constraint(solvers.iff(bob_works, solvers.not_(alice_works)))