Skip to content

This feature is currently in Preview.

relationalai.experimental.solvers.implies()

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

Returns a solver expression representing a logical implication: if left is true, then right must also be 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
leftSolverExpressionThe condition that riggers the implication. Must evaluate to a binary value (True/False or 1/0), such as a binary variable or a logical comparison.
rightSolverExpressionThe outcome enforced when left is true. Must also evaluate to a binary value.

A SolverExpression object.

Use implies() with the SolverModel.constraint() method to encode a dependency between two conditions:

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.
# If an employee is assigned the morning shift, they are not assigned the evening shift on the same day.
with solvers.operators():
works_morning = Scenario(shift=Shift(name="Morning"))
works_evening = Scenario(
shift=Shift(name="Evening"),
employee=works_morning.employee,
day=works_morning.day
)
solver_model.constraint(
solvers.implies(works_morning, solvers.not_(works_evening))
)