Skip to content

This feature is currently in Preview.

relationalai.experimental.solvers.integer_interval()

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

Returns a solver expression that constrains the solver variables or expressions produced by arg to integer values between low and high, inclusive. For solver variables, this is equivalent to setting type="integer", 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 constrain.
lowint, dsl.ProducerThe inclusive lower bound of the interval.
highint, dsl.ProducerThe inclusive upper bound of the interval.

A SolverExpression object.

Use integer_interval() with the SolverModel.constraint() method to constrain solver variables to integer values within a 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],
lower=0,
upper=5,
)
# Constrain egg portions to be integer values between 0 and 2.
with solvers.operators():
portion = Portion(food=Food(name="Egg"))
solver_model.constraint(solvers.integer_interval(portion, 0, 2))