Skip to content

This feature is currently in Preview.

relationalai.experimental.solvers.zero_one()

Signature
zero_one(arg: dsl.Instance) -> SolverExpression

Returns a solver expression that constrains the solver variables for entities produced by arg to binary values (0 or 1). Equivalent to setting type="zero_one" in the SolverModel.variable() method. Must be called in a Model.rule() or solvers.operators() context.

NameTypeDescription
argdsl.InstanceAn instance of an entity type corresponding to a solver variable.

A SolverExpression object.

Use zero_one() with the SolverModel.constraint() method to constrain solver variables to binary values:

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,
name_args=["assigned", scenario.employee.name, scenario.day.name, scenario.shift.name],
)
with solvers.operators():
assignment = Assignment()
solver_model.constraint(solvers.zero_one(assignment))

Alternatively, you may set type="zero_one" in the SolverModel.variable() method to achieve the same effect:

import relationalai as rai
from relationalai.experimental import solvers
67 collapsed lines
# 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()
# 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],
)