Constraints

๐Ÿ“œ Constraints: The Rules of the Game

Constraints are the rules that your solution must follow. They are the heart of your model, telling Qaekwy what a valid solution looks like.

What are Constraints?

Think of constraints as the rules of a puzzle. For example, in a Sudoku puzzle, the constraints are:

  • Each row must have the digits 1 through 9, with no repeats.
  • Each column must have the digits 1 through 9, with no repeats.
  • Each 3x3 square must have the digits 1 through 9, with no repeats.

Why are Constraints Important?

Constraints give your model its structure. The solver uses them to eliminate possibilities and find a solution. This process is called constraint propagation. The progapators prune variables’ domain until a viable/optimal solution has been found.

Types of Constraints in Qaekwy

Qaekwy has a whole toolbox of constraints.

Relational Constraints

These are your basic building blocks, letting you compare variables using standard Python operators:

# x must be bigger than y
modeller.add_constraint(x > y)

# The sum of x and y must be 10
modeller.add_constraint(x + y == 10)

Global Constraints

These are special constraints for common patterns. They are often more efficient than building the same logic from scratch.

  • ConstraintDistinctArray: Makes sure all elements in an array are different.
  • ConstraintElement: Links a variable to an element in an array.

Logical Constraints

You can also combine constraints using logical operators to create more complex rules.

  • ConstraintIfThenElse: For “if this, then that” logic.
  • Logical OR: (x > 5) | (y < 2)
  • Logical AND: (x > 5) & (y < 2)

Explore the Constraint Library

This is just a taste of the constraints available in Qaekwy. To see the full menu, check out the API Reference for Constraints.