relationalai.experimental.solvers.SolverExpression
class SolverExpressionRepresents a symbolic expression used in the SolverModel API to define solver constraints and objectives.
Example
Section titled “Example”Use operator functions like solvers.sum() or infix operators like == to create SolverExpression objects.
Combine expressions to form complex logic and use them with SolverModel methods like .constraint() to define constraints and objectives in a solver model:
import relationalai as raifrom 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 in# and out of each stage is balanced.with solvers.operators(): stage = Stage() stage.name != "Incoming Tickets" # Exclude the source stage stage.name != "Resolved Tickets" # Exclude the sink stage incoming_tickets = solvers.sum(Route(destination=stage), per=[stage]) outgoing_tickets = solvers.sum(Route(source=stage), per=[stage]) solver_model.constraint( incoming_tickets == outgoing_tickets, name_args=["max_outgoing_tickets", stage.name] )