Skip to content

all_different

relationalai.semantics.reasoners.prescriptive.problem
all_different(*args: b.Value) -> b.Aggregate

Return an all-different constraint.

Use this inside Model.require and pass the resulting fragment to Problem.satisfy. Arguments must reference decision variables declared via Problem.solve_for. For grouped constraints (for example, “all values are distinct per row”), scope it with Aggregate.per.

  • *args

    (Value, default: ()) - One or more decision-variable expressions that must take distinct values.
  • Aggregate - A solver aggregate representing the all-different constraint.

Constrain values to be distinct per row:

>>> from relationalai.semantics import Integer, Model
>>> from relationalai.semantics.reasoners.prescriptive import Problem, all_different
>>> m = Model("all_different_demo")
>>> Cell = m.Concept("Cell", identify_by={"row": Integer, "col": Integer})
>>> Cell.val = m.Property(f"{Cell} has {Integer:val}")
>>> m.define(
... Cell.new(row=0, col=0),
... Cell.new(row=0, col=1),
... Cell.new(row=1, col=0),
... Cell.new(row=1, col=1),
... )
>>> p = Problem(m, Integer)
>>> p.solve_for(Cell.val, name=["x", Cell.row, Cell.col], lower=1, upper=4)
>>> p.satisfy(m.require(all_different(Cell.val).per(Cell.row)).where(Cell))
RelationalAI Documentation
└──  Build With RelationalAI
    └──  Understand how PyRel works > Use advanced reasoning > Prescriptive reasoning
        ├──  Choose a backend
        │   └──  Overview
        │       └──  Which backend should you use?
        └──  Solve a decision problem
            └──  Add constraints