Skip to content

This feature is currently in Preview.

relationalai.experimental.solvers.or_()

Signature
or_(*args: SolverExpression) -> SolverExpression

Returns a solver expression representing a logical disjunction: the result is true if any of the input expressions are true. Each argument must be a binary-valued solver expression, such as a binary variable or a logical comparison. Must be called in a Model.rule() or solvers.operators() context.

NameTypeDescription
*argsSolverExpressionOne or more expressions to be joined by logical OR. Each must evaluate to a binary value (True/False or 1/0), such as a binary variable or a logical comparison.

A SolverExpression object.

Use or_() with the SolverModel.constraint() method to require that at least one of several expressions is true:

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.
# At least one of Alice or Bob works Morning on Monday.
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.or_(alice_works, bob_works))