Skip to content

This feature is currently in Preview.

relationalai.experimental.solvers.Operators

Signature
class Operators

Implements the context manager protocol to override Python infix operators so that they create SolverExpression objects instead dsl.Expression objects that filter entities and properties.

Use with solvers.operators() to create an Operators context:

import relationalai as rai
from relationalai.experimental import solvers
# Define a RAI model.
model = rai.Model("SupportTicketRouting")
41 collapsed lines
# Declare support pipeline stage type.
Stage = model.Type("Stage")
# Declare route type to connect stages.
Route = model.Type("Route")
Route.source.declare()
Route.destination.declare()
Route.capacity.declare()
# Define sample data for the model.
# Define support teams and endpoints.
for name in [
"Incoming Tickets",
"US Tier 1",
"EU Tier 1",
"US Tier 2",
"EU Tier 2",
"Escalations",
"Resolved Tickets",
]:
with model.rule():
Stage.add(name=name)
# Define routes with capacities.
for src_name, dst_name, cap in [
("Incoming Tickets", "US Tier 1", 50),
("Incoming Tickets", "EU Tier 1", 40),
("US Tier 1", "Resolved Tickets", 20),
("US Tier 1", "US Tier 2", 30),
("EU Tier 1", "Resolved Tickets", 10),
("EU Tier 1", "EU Tier 2", 25),
("US Tier 2", "Resolved Tickets", 15),
("US Tier 2", "Escalations", 20),
("EU Tier 2", "Resolved Tickets", 15),
("EU Tier 2", "Escalations", 20),
("Escalations", "Resolved Tickets", 25),
]:
with model.rule():
src = Stage(name=src_name)
dst = Stage(name=dst_name)
Route.add(source=src, destination=dst).set(capacity=cap)
# Create a SolverModel instance from the model.
solver_model = solvers.SolverModel(model)
11 collapsed lines
# Specify variables, constraints, and the objective.
# Define solver variables for each route.
with model.rule():
route = Route()
solver_model.variable(
route,
name_args=["route", route.source.name, route.destination.name],
type="integer",
lower=0,
upper=route.capacity,
)
# Define a constraint to ensure the total number of tickets routed to
# US Tier 1 and US Tier 2 is between 10 and 30.
with solvers.operators():
us_tier_1 = Route(source=Stage(name="US Tier 1"))
us_tier_2 = Route(source=Stage(name="US Tier 2"))
solver_model.constraint(
solvers.integer_interval(us_tier_1 + us_tier_2, 10, 30),
name_args=["us_tier_total"]
)