Skip to content

This feature is currently in Preview.

relationalai.experimental.solvers.interval()

Signature
interval(
arg: dsl.Instance|SolverExpression,
low: int|float|dsl.Producer,
high: int|float|dsl.Producer
) -> SolverExpression

Returns a solver expression that constrains the solver variables or expressions produced by arg to values between low and high, inclusive. For solver variables, this is equivalent to setting lower=low and upper=high in the SolverModel.variable() method. Must be called in a Model.rule() or solvers.operators() context.

NameTypeDescription
argdsl.Instance, SolverExpressionThe solver variable or solver expression to bound within the interval.
lowint, float, dsl.ProducerThe inclusive lower bound of the interval.
highint, float, dsl.ProducerThe inclusive upper bound of the interval.

A SolverExpression object.

Use interval() with the SolverModel.constraint() method to constrain solver variables to a continuous range:

import relationalai as rai
from relationalai.experimental import solvers
# Create a model.
model = rai.Model("DietaryConstraints")
# Declare entity types.
Food = model.Type("Food")
Day = model.Type("Day")
Portion = model.Type("Portion")
23 collapsed lines
# Declare properties.
Food.calories.declare()
Food.fat.declare()
Food.salt.declare()
Food.carbs.declare()
Portion.food.declare()
Portion.day.declare()
# Define foods.
with model.rule():
Food.add(name="Oatmeal").set(calories=150, fat=3, salt=0.1, carbs=27)
Food.add(name="Egg").set(calories=70, fat=5, salt=0.2, carbs=1)
Food.add(name="Banana").set(calories=100, fat=0.5, salt=0.0, carbs=26)
Food.add(name="Toast").set(calories=80, fat=1, salt=0.3, carbs=15)
# Define days.
with model.rule():
for name in ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]:
Day.add(name=name)
# Define all possible food portions for each day.
with model.rule():
Portion.add(food=Food(), day=Day())
# Create a solver model.
solver_model = solvers.SolverModel(model)
# Define solver variables for portions.
with model.rule():
portion = Portion()
solver_model.variable(
portion,
name_args=["portion", portion.food.name, portion.day.name],
)
# Constrain all portions to be between 0 and 5 units.
with solvers.operators():
portion = Portion()
solver_model.constraint(solvers.interval(portion, 0, 5))

Note that for solver variables, this is equivalent to setting lower=... and upper=... in the SolverModel.variable() method to achieve the same effect:

import relationalai as rai
from relationalai.experimental import solvers
34 collapsed lines
# Create a model.
model = rai.Model("DietaryConstraints")
# Declare entity types.
Food = model.Type("Food")
Day = model.Type("Day")
Portion = model.Type("Portion")
# Declare properties.
Food.calories.declare()
Food.fat.declare()
Food.salt.declare()
Food.carbs.declare()
Portion.food.declare()
Portion.day.declare()
# Define foods.
with model.rule():
Food.add(name="Oatmeal").set(calories=150, fat=3, salt=0.1, carbs=27)
Food.add(name="Egg").set(calories=70, fat=5, salt=0.2, carbs=1)
Food.add(name="Banana").set(calories=100, fat=0.5, salt=0.0, carbs=26)
Food.add(name="Toast").set(calories=80, fat=1, salt=0.3, carbs=15)
# Define days.
with model.rule():
for name in ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]:
Day.add(name=name)
# Define all possible food portions for each day.
with model.rule():
Portion.add(food=Food(), day=Day())
# Create a solver model.
solver_model = solvers.SolverModel(model)
# Define solver variables for portions.
with model.rule():
portion = Portion()
solver_model.variable(
portion,
name_args=["portion", portion.food.name, portion.day.name],
lower=0,
upper=5,
)